def extract_metadata_from_file()

in infra-as-code/modules/audio-data-format-change/function-source-code/lib.py [0:0]


  def extract_metadata_from_file(self):
    """Extracts metadata from the raw filename, from the ffmpeg encoding log and audio metadata
        by opening each file produced by ffmpeg and reading the contents.
    """
    stream = dict()
    with open('/tmp/audio-meta.txt', 'r') as f:
      content = f.read()
      for line in content.splitlines():
        if 'codec_name' in line:
          stream['codec_name'] = line.split('=')[1].strip()
        elif 'sample_rate' in line:
          stream['sample_rate'] = line.split('=')[1].strip()
        elif 'channels' in line:
          stream['channels'] = line.split('=')[1].strip()
        elif 'channel_layout' in line:
          stream['channel_layout'] = line.split('=')[1].strip()
        elif 'start_time' in line:
          stream['start_time'] = line.split('=')[1].strip()
        elif 'duration' in line:
          stream['duration'] = line.split('=')[1].strip()
        elif 'bits_per_raw_sample' in line:
          stream['bits_per_raw_sample'] = line.split('=')[1].strip()

    self.metadata['stream'] = stream

    file_path = '/tmp/format-meta.txt'
    if os.path.exists(file_path):
      with open(file_path, 'r') as f:
        for line in f:
          if 'encoder' in line:
            self.metadata['encoder'] = line.split('=')[1].strip()