def format_audio()

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


  def format_audio(self, filename, trigger_file):
    """Changes the format of the given file to a FLAC file through an ffmpeg command
        system command generates three outputs which are then store in /tmp: 
          .flac, 
          encoding metadata (format-meta.txt)
          log data (ffmpeg-log-data)
        Function then checks for the .flac file, if it exists encoding was successful and calls 
        get_audio_metadata and extract_metadata_from_file. If it doesn't exists encoding failed 
        and calls log_error

    Args:
        filename (string): Name of the downloaded file to encode as flac
    """

    self.formatted_audio_file_name = filename.replace(self.raw_audio_file_extension, 'flac')
    raw_audio_path = '/tmp/' + filename
    formatted_audio_path = '/tmp/' + self.formatted_audio_file_name

    if self.raw_audio_file_extension != 'flac':
      command = f'ffmpeg -hide_banner -i {raw_audio_path} -ac {self.number_of_channels} {formatted_audio_path} -f ffmetadata /tmp/format-meta.txt 2>/tmp/ffmpeg-log-data.txt'
      os.system(command)

    if os.path.isfile(formatted_audio_path) and os.path.getsize(formatted_audio_path) > 0:    
      print(f'Formatted {filename} into {self.formatted_audio_file_name}')
        
      self.get_audio_metadata(formatted_audio_path)
      self.extract_metadata(filename, trigger_file)
      return True

    return False