In a previous post I listed some options for audio data manipulation (conversion of format, sample rate, bitrate, trimming, etc), with SoX, MPlayer and FFmpeg. The obvious question is now: which one is best?

The obvious answer is: it depends. Several factors play a role in defining "the best": what audio formats do you want to convert from/to (e.g. FFmpeg and MPlayer support the typical patent/license encumbered audio and video formats like MP3 out of the box, while SoX is more conservative on this point), do you want more filtering options (SoX offers significantly more than the others), etc.

In this post we'll only look at the execution time as metric to determine "the best". Another metric could be "audio quality" but that was not important for my use case (I couldn't hear differences anyway).

Execution time experiment

I set up a small experiment to compare the three: - Input: normal MP3 file (290 seconds audio, 128 kpbs, 44kHz, stereo) - Desired output: fragment of certain length (at an offset of 60 seconds) in WAV format, mono, 22050 Hz sample frequency. - Which one is the fastest?

The three commands I used were:

# FFmpeg
ffmpeg -i $input_file -y \
    -ac 1 -ar 22050 -ss 60 -t $fragment_length output-ffmpeg.wav

# MPlayer
mplayer -ao pcm:fast:waveheader:file=output-mplayer.wav \
    -af resample=22050,pan=1:0.5:0.5 \
    -vo null -vc null \
    -ss 60 -endpos $fragment_length \

# SoX
sox $input_file -c 1 -r 22050 output-sox.wav trim 60 $fragment_length

I tried this with fragment lengths of 5, 10, 20, 30 and 60 seconds. I ran each command several times for each fragment length, timed the execution (the "user time" reported by the command line tool "time") and calculated the average execution time (the standard deviation was small enough to only consider the average execution time).

The result (lower is better)


MPlayer is clearly fastest option for this use case. For 30 second fragments, FFmpeg is 1.8 times slower and SoX is 2.4 times slower. For smaller fragments, the speed difference is even larger. Also note that the execution time with MPlayer is almost proportional to the desired fragment length. SoX and FFmpeg on the other hand suffer from a non neglectable "startup" time, which disfavors small fragment lengths. However, the proportional part of FFmpeg's "curve" is less inclined than MPlayer's and for larger fragment lengths FFmpeg becomes more interesting. The crossing point is at a fragment length of roughly 2 minutes.

Setup info

Used hardware: dual core Intel Xeon, 3 GHz. Used software: FFmpeg 0.5, SoX v14.2.0 and MPlayer 1.0rc2-4.3.3 (default packages on Ubuntu 9.04).

I also did the same experiment on other computers with different hardware and different software version, and the relative results were pretty much the same. However, one significant deviation happened on a setup with Ubuntu 8.04 (LTS, Hardy Heron): SoX v14.0.0 performed roughly 8 times slower than FFmpeg and even more than 25 slower than MPlayer for the short fragment lengths.