diff --git a/src/echonest/remix/support/ffmpeg.py b/src/echonest/remix/support/ffmpeg.py index 517b632..060db32 100644 --- a/src/echonest/remix/support/ffmpeg.py +++ b/src/echonest/remix/support/ffmpeg.py @@ -77,14 +77,18 @@ def ffmpeg(infile, outfile=None, overwrite=True, bitRate=None, log.info(command) (lin, mac, win) = get_os() - p = subprocess.Popen( - command, - shell=False, - stdin=(None if filename else subprocess.PIPE), - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - close_fds=(not win) - ) + try: + p = subprocess.Popen( + command, + shell=False, + stdin=(None if filename else subprocess.PIPE), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + close_fds=(not win) + ) + except OSError as err: + if err.errno == 2: + raise_not_found_error() if filename: f, e = p.communicate() @@ -199,12 +203,16 @@ def settings_from_ffmpeg(parsestring): chans = 1 return freq, chans -ffmpeg_install_instructions = """ -en-ffmpeg not found! Please make sure ffmpeg is installed and create a link as follows: - sudo ln -s `which ffmpeg` /usr/local/bin/en-ffmpeg -Alternatively, import echonest.remix.support.ffmpeg and modify ffmpeg.FFMPEG to name -the appropriate binary. -""" +def raise_not_found_error(): + ffmpeg_install_instructions = """ + en-ffmpeg not found! Please make sure ffmpeg is installed and create a link as follows: + sudo ln -s `which ffmpeg` /usr/local/bin/en-ffmpeg + Alternatively, import echonest.remix.support.ffmpeg and modify ffmpeg.FFMPEG to name + the appropriate binary. + """ + + raise RuntimeError(ffmpeg_install_instructions) + def ffmpeg_error_check(parsestring): "Looks for known errors in the ffmpeg output" @@ -217,8 +225,6 @@ def ffmpeg_error_check(parsestring): "Could not find codec", # corrupted, incomplete, or otherwise bad file ] for num, line in enumerate(parse): - if "command not found" in line or FFMPEG+": not found" in line: - raise RuntimeError(ffmpeg_install_instructions) for error in error_cases: if error in line: report = "\n\t".join(parse[num:]) diff --git a/tests/test_ffmpeg.py b/tests/test_ffmpeg.py index 4cf7917..25ab51b 100644 --- a/tests/test_ffmpeg.py +++ b/tests/test_ffmpeg.py @@ -5,8 +5,8 @@ Run the tests like this: python test_ffmpeg.py - -If you want to test that your version of ffmpeg can handle a particular file, + +If you want to test that your version of ffmpeg can handle a particular file, (like if you're planning on analyzing OggVorbis files and want to make sure your ffmpeg can decode them), run like this: python test_ffmpeg.py my_crazy_audio_file.mp47 @@ -16,46 +16,41 @@ import sys import tempfile -from echonest import audio +from echonest.remix.support import ffmpeg def main(): """Run some tests""" if len(sys.argv) > 1: input_filename = sys.argv[1] else: - input_filename = 'input_file.mp3' + input_filename = 'input_file.mp3' test_en_ffmpeg_exists(input_filename) test_round_trip(input_filename) def test_en_ffmpeg_exists(input_filename): """Don't do any conversion, just see if en-ffmpeg, the command used by Remix, is installed.""" - result = audio.ffmpeg(input_filename, overwrite=False, verbose=True) - audio.ffmpeg_error_check(result[1]) + ffmpeg.ffmpeg(input_filename, overwrite=False, verbose=True) def test_round_trip(input_filename): """Convert the input file to a wav file, then back to an mp3 file.""" - result = audio.ffmpeg(input_filename, overwrite=False, verbose=True) - audio.ffmpeg_error_check(result[1]) - sampleRate, numChannels = audio.settings_from_ffmpeg(result[1]) - temp_file_handle, temp_filename = tempfile.mkstemp(".wav") - result = audio.ffmpeg(input_filename, temp_filename, overwrite=True, - numChannels=numChannels, sampleRate=sampleRate, verbose=True) - audio.ffmpeg_error_check(result[1]) - + + result = ffmpeg.ffmpeg(input_filename, outfile=temp_filename, overwrite=True, + verbose=True) + sampleRate, numChannels = result + temp_filesize = os.path.getsize(temp_filename) print 'temp file size: %s bytes' % temp_filesize - + output_filename = 'output_file.mp3' - result = audio.ffmpeg(temp_filename, output_filename, overwrite=True, + result = ffmpeg.ffmpeg(temp_filename, output_filename, overwrite=True, numChannels=numChannels, sampleRate=sampleRate, verbose=True) - audio.ffmpeg_error_check(result[1]) - + if temp_file_handle is not None: os.close(temp_file_handle) os.remove(temp_filename) - + input_filesize = os.path.getsize(input_filename) output_filesize = os.path.getsize(output_filename) difference = output_filesize - input_filesize @@ -67,5 +62,5 @@ def test_round_trip(input_filename): print 'Ok!' if __name__ == '__main__': - main() + main()