PageRenderTime 284ms CodeModel.GetById 141ms app.highlight 8ms RepoModel.GetById 133ms app.codeStats 0ms

/tests/test_ffmpeg.py

http://echo-nest-remix.googlecode.com/
Python | 71 lines | 53 code | 0 blank | 18 comment | 1 complexity | 92da86e92b969fa1d75a5735c82e428d MD5 | raw file
 1#!/usr/bin/env python
 2# encoding: utf-8
 3"""
 4Test that an Echo-Nest-Remix-compatible version of ffmpeg is installed.
 5
 6Run the tests like this:
 7    python test_ffmpeg.py
 8    
 9If you want to test that your version of ffmpeg can handle a particular file, 
10(like if you're planning on analyzing OggVorbis files and want to make sure
11your ffmpeg can decode them), run like this:
12    python test_ffmpeg.py my_crazy_audio_file.mp47
13"""
14
15import os
16import sys
17import tempfile
18
19from echonest import audio
20
21def main():
22    """Run some tests"""
23    if len(sys.argv) > 1:
24        input_filename = sys.argv[1]
25    else:
26	    input_filename = 'input_file.mp3'
27    test_en_ffmpeg_exists(input_filename)
28    test_round_trip(input_filename)
29
30def test_en_ffmpeg_exists(input_filename):
31    """Don't do any conversion, just see if en-ffmpeg, the command used
32    by Remix, is installed."""
33    result = audio.ffmpeg(input_filename, overwrite=False, verbose=True)
34    audio.ffmpeg_error_check(result[1])
35
36def test_round_trip(input_filename):
37    """Convert the input file to a wav file, then back to an mp3 file."""
38    result = audio.ffmpeg(input_filename, overwrite=False, verbose=True)
39    audio.ffmpeg_error_check(result[1])
40    sampleRate, numChannels = audio.settings_from_ffmpeg(result[1])
41    
42    temp_file_handle, temp_filename = tempfile.mkstemp(".wav")
43    result = audio.ffmpeg(input_filename, temp_filename, overwrite=True, 
44        numChannels=numChannels, sampleRate=sampleRate, verbose=True)
45    audio.ffmpeg_error_check(result[1])
46    
47    temp_filesize = os.path.getsize(temp_filename)
48    print 'temp file size: %s bytes' % temp_filesize
49    
50    output_filename = 'output_file.mp3'
51    result = audio.ffmpeg(temp_filename, output_filename, overwrite=True, 
52        numChannels=numChannels, sampleRate=sampleRate, verbose=True)
53    audio.ffmpeg_error_check(result[1])
54    
55    if temp_file_handle is not None:
56        os.close(temp_file_handle)
57        os.remove(temp_filename)
58    
59    input_filesize = os.path.getsize(input_filename)
60    output_filesize = os.path.getsize(output_filename)
61    difference = output_filesize - input_filesize
62    args = (input_filesize, output_filesize, difference)
63    print 'input file size: %s bytes | output file size: %s bytes | difference: %s bytes ' % args
64    if abs(int(difference)) > 1000:
65        print 'input and output files are different sizes. something might be wrong with your ffmpeg.'
66    else:
67        print 'Ok!'
68
69if __name__ == '__main__':
70	main()
71