Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close #47 - Show error message when ffmpeg not found, fix ffmpeg test #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/echonest/remix/support/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"
Expand All @@ -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:])
Expand Down
35 changes: 15 additions & 20 deletions tests/test_ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -67,5 +62,5 @@ def test_round_trip(input_filename):
print 'Ok!'

if __name__ == '__main__':
main()
main()