I want to convert a large number of video files in various formats into .mp4 files (container MPEG-4, codec H.264). I want to do this on an Ubuntu machine, using only command-line tools and I'm willing to install packages from main, restricted, universe and multiverse.

Ideally I'd like to be able to do ...

for VIDEO_FILE in *; do
  some_conversion_program $VIDEO_FILE $VIDEO_FILE.mp4

... and have all my video files in .mp4 format with container MPEG-4 and codec H.264.

How would you tackle this problem on an Ubuntu machine? What packages do I need to install?

2 Answers 2


You will need to install these :

sudo apt-get install ffmpeg libavcodec-unstripped-52 libavdevice-unstripped-52 libavformat-unstripped-52 libavutil-unstripped-50 libpostproc-unstripped-51 libswsclale-unstripped-0 x264

In Karmic, Lucid and Maverick you should replace "unstripped" by "extra" but since there is transitional packages this work too.

And then you can use a script with :

for i in *.avi; do ffmpeg -i "$i" -f mp4 "`basename "$i" .avi`.mp4";done

And you can use options like these to set up resolution, video codec and audio codec and audio quality :

for i in *.avi; do ffmpeg -i "$i" -s 352x288 -vcodec libx264 -vpre default -acodec libmp3lame -ab 192k -ac 2 -ar 44100 -f mp4 "`basename "$i" .avi`.mp4";done

For Ubuntu 14.04 and newer

You will need avconv from libav-tools and ubuntu-restricted-extras. You can install them if you don't have them already with the command:

sudo apt-get install libav-tools ubuntu-restricted-extras

This script should do the trick however the assumption is that there are only video files in the folder in question, otherwise unintended consequences may occur.

echo "This script will attempt to encode by re-encoding the video stream and copying the audio stream placing all files in the current directory into a mp4 video container of the same name as the sources. The new filename will be derived from a basename (everything before the source file last '.') and an extension (everything after the source file last '.'). Target names will be 'basename'.mp4. Sources matching the target name will be renamed with a .bak extension prior to processing for safety. a CRF of 25 is harcoded in by preference but feel free to adjust as you desire."
echo "You must choose the preset of your choice with a tradeoff of speed vs. quality"
echo  "(veryfast recommended for decent speed and decent quality)"
echo "type  a preset and press enter or bail and enter to quit now. Preset choices are:"
echo "ultrafast superfast veryfast faster fast medium slow slower slowest"
read preset
echo "you chose $preset"
if [ "$preset" != "bail" ]
for f in *.* 
    name=$(echo "$f" | sed 's/\.[^\.]*$//')
    ext=$(echo "$f" | sed 's/^.*\.//')
    echo target = $target
        if  [ "$f" = "$target" ];
            echo "$f=$target so moving source to backup file"
            mv "$f" "$f.bak";
            if [ "$?" != "0" ]; then echo "error renaming $f" && exit
            avconv -i "$f.bak" -c:a copy -c:v libx264 -preset "$preset" -crf 25 "$target"
            if [ "$?" != "0" ]; then echo "error processing $f.bak" && exit
            avconv -i "$f" -c:a copy -c:v libx264 -preset "$preset" -crf 25 "$target"
            if [ "$?" != "0" ]; then echo "error processing $f" && exit

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .