def encode_as_linear16(path, tmp): # From: https://cloud.google.com/speech/support#troubleshooting: # "The LINEAR16 encoding must be 16-bits, signed-integer, # little-endian." # In avconv, this translates to "s16le". See also: # http://stackoverflow.com/a/4854627/64911 and # https://trac.ffmpeg.org/wiki/audio%20types assert isinstance(path, basestring), "path argument is not a str." av_path = get_audio_binary() # fmt: off av_command = [ av_path, '-y', # Assume yes (clobber existing files) '-i', path, # Input file '-f', 's16le', # Force output format '-ac', '1', # Mono '-ar', '16k', # Sample rate of 16000Mhz tmp.name, # Output file ] # fmt: on try: _ = subprocess.check_output(av_command, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: print("%s failed command: %s\n" "error code: %s\n" "output: %s\n" % (av_path, av_command, e.returncode, e.output)) print(traceback.format_exc()) raise e
def process_audio_file(pk): """Given the key to an audio file, extract its content and add the related meta data to the database. """ af = Audio.objects.get(pk=pk) tmp_path = os.path.join("/tmp", "audio_" + uuid.uuid4().hex + ".mp3") av_path = get_audio_binary() av_command = [ av_path, "-i", af.local_path_original_file.path, "-ar", "22050", # sample rate (audio samples/s) of 22050Hz "-ab", "48k", # constant bit rate (sample resolution) of 48kbps tmp_path, ] try: _ = subprocess.check_output(av_command, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: print( "%s failed command: %s\nerror code: %s\noutput: %s\n%s" % ( av_path, av_command, e.returncode, e.output, traceback.format_exc(), ) ) raise set_mp3_meta_data(af, tmp_path) try: with open(tmp_path, "r") as mp3: cf = ContentFile(mp3.read()) file_name = trunc(best_case_name(af).lower(), 72) + "_cl.mp3" af.file_with_date = af.docket.date_argued af.local_path_mp3.save(file_name, cf, save=False) except: msg = ( "Unable to save mp3 to audio_file in scraper.tasks." "process_audio_file for item: %s\n" "Traceback:\n" "%s" % (af.pk, traceback.format_exc()) ) print(msg) ErrorLog.objects.create( log_level="CRITICAL", court=af.docket.court, message=msg ) af.duration = eyed3.load(tmp_path).info.time_secs af.processing_complete = True af.save()
def process_audio_file(pk): """Given the key to an audio file, extract its content and add the related meta data to the database. """ af = Audio.objects.get(pk=pk) tmp_path = os.path.join('/tmp', 'audio_' + uuid.uuid4().hex + '.mp3') av_path = get_audio_binary() av_command = [ av_path, '-i', af.local_path_original_file.path, '-ar', '22050', # sample rate (audio samples/s) of 22050Hz '-ab', '48k', # constant bit rate (sample resolution) of 48kbps tmp_path ] try: _ = subprocess.check_output(av_command, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: print('%s failed command: %s\nerror code: %s\noutput: %s\n%s' % (av_path, av_command, e.returncode, e.output, traceback.format_exc())) raise set_mp3_meta_data(af, tmp_path) try: with open(tmp_path, 'r') as mp3: cf = ContentFile(mp3.read()) file_name = trunc(best_case_name(af).lower(), 72) + '_cl.mp3' af.file_with_date = af.docket.date_argued af.local_path_mp3.save(file_name, cf, save=False) except: msg = ("Unable to save mp3 to audio_file in scraper.tasks." "process_audio_file for item: %s\n" "Traceback:\n" "%s" % (af.pk, traceback.format_exc())) print(msg) ErrorLog.objects.create(log_level='CRITICAL', court=af.docket.court, message=msg) af.duration = eyed3.load(tmp_path).info.time_secs af.processing_complete = True af.save()