HOWTO Convert video files
Converting video formats between common file formats and codecs can easily be done from the command line. Learn to master the two standard command-line tools and you will be able to convert from any video format to any video format in an instant.
- This article is about command-line software for converting video files. Take a look at avidemux if you are looking for a graphical front-end program.
 Why convert
There are many good reasons to convert video files you made yourself - or video files forwarded to you which you plan to distribute. Good reasons to change a video file can be:
This HOWTO will to introduce to you some of the basic commands to use the most common Linux command-line program to convert video files from one format to another.
The modern file-formats you want to use today are .MP4 with MPEG4 AVC video and AAC audio and .OGV with Ogg Theora video and audio. These are the "HTML5 video formats". Both provide better quality than outdated legacy video-files like "divx", "wmv", etc.
 File-size matters
Size is important, specially when publishing video on the web.
- A video-file reduced by 100 MB will save you 100 GB of bandwidth if 1000 people download it;
- The reduction of 100 MB per. file will save you 1 GB of hard-drive space if you host 10 video-files on a web-server.
You will likely want files to be as small as possible without reducing the video quality
HTML5 can use three formats: webm, ogg video and mp4.
The HTML5 video tag allows you to provide the same file in multiple formats. Supplying both ogv and mp4 lets almost everybody play your video file. Those using these things called MSIE and Safari will not be able to play your video if you only provide Ogg Theora! Those users are mostly braindead zombie-like sheep and you may think that these non-thinking visitors do not matter, but they do, all visitors matter regardless of them being very brain-dead.
You will see your shareholder value decrease if you only provide videos in the Ogg Theora format on your website. You must, sadly, use at least two formats.
 The Basic Tools You Need
There are one basic GNU/Linux tools you absolutely must learn, and that is:
There are two other tools you could use. These are:
- mplayers mencoder, and
All are powerful, but ffmpeg is the l33t shit you'll (ab)use and enjoy the most. The others are really not required nor desired.
All of these tools should be included with your local friendly GNU/Linux distribution. They are in your package repository if they are not already installed (type which ffmpeg, it's probably there).
 Learning ffmpeg
The most basic way to use ffmpeg is to use -i yourinputfilename.ext folowed by outputfilename.ext.
That's it. This is, seriously, all you need do remember when it comes to the most basic (ab)use cases:
ffmpeg -i YourAwesomeMovie.dv YourAwesomeMovie.mp4
This basic example will make ffmpeg detect the input file type. The built-in input detection does work perfectly in 99% of all cases.
This basic example will also make ffmpeg assume what audio and video formats you want for your output file based on the file extension. We asked to output the filename YourAwesomeMovie.mp4. It ends with .mp4.
The .mp4 extension will make ffmpeg assume that you want a file with H.264/AVC video and AAC 2.0 audio. This happens to be the correct values for HTML5 web video in the very (as in extremely) evil proprietary HTML5 video format alternative.
 Moving on to more advanced encoding
The secret ffmpeg manual page is very long. It contains lots of detailed information about the numerous options available when using this tool. Whole books can be, and probably have been, written about the advanced options listed in the manual page.
Things you may want to tune when encoding include:
- -r to change the frame rate
- -b:v to set the video bitrate
- -b:a to set the audio bitrate
These options can have a huge impact on the quality and the file-size of the resulting file.
A general rule is: You can have low file-size OR high picture quality, but you can NOT have both. You will have to compromise.
This may give you an acceptable quality video file, depending on resolution:
ffmpeg -i YourAwesomeMovie.dv -r 25 -b:a 128k -b:v 4000k YourAwesomeMovie.mp4
..and this will NOT, because 256k is not enough bandwidth for acceptable quality even at small resolutions:
ffmpeg -i YourAwesomeMovie.dv -r 25 -b:a 64k -b:v 256k YourAwesomeMovie.mp4
 Make a VCD?
ffmpeg can make VCD mpeg video files using -target where the target can be "vcd", "svcd", "dvd", "dv", "pal-vcd", "ntsc-svcd". These switches will set the output format options (bitrate, codecs, buffer sizes) automatically.
The default vcd switch makes a PAL vcd.
ffmpeg -i myfile.avi -target vcd /tmp/vcd.mpg
A ntsc vcd:
ffmpeg -i myfile.avi -hq -target ntsc-vcd /tmp/vcd.mpg
Converting a file for VCD format using a and b frames for MPEG 2:
ffmpeg -i myfile.avi -target ntsc-vcd -bf 2 /home/user/Video/vcd.mpg
This information about CDs is only provided for historical purposes. To those who do not know what a CD or DVD is: They were flat round things used in a few years before and after the turn of the millennium.
 Learning mencoder
mencoder is a now obsolete video converter and encoder which comes bundled with mplayer.
mencoder is not as simple to (ab)use as ffmpeg. It requires that you specify audio and video codecs with the -oac and -ovc options, and you should also tell it what container to use with -of regardless of extension. It is further worth mentioning that the number of extensions available to mplayer is quite limited.
Mplayer does not support the modern .mp4 and .ogv containers!
A typical mencoder command would have the following elements:
- -ovc codec for video output
- -oac codec for audio output
- -o output_filename.avi
This results in something like:
mencoder <filename.avi> -ovc lavc -oac lavc -o <output.avi>
 XviD Encoding using mencoder
You can encode XviD using one or two passes of the original file. You will get better quality if you use two-pass encoding, but does take longer to encode the video.
You can only set the bitrate or fixed_quant using one-pass encoding. You can only choose bitrate if you do two-pass encoding.
You must set one of these when encoding XviD using mencoder.
By setting bitrate:
- Using bitrate will encode the video to a constant bitrate (CBR).
- Set bitrate=<value>
- Higher = better quality with larger files, and
- lower = less quality with smaller files.
- A bitrate of 800 will give you near DVD quality when encoding hi-quality video like a DVD disc.
- VCD quality is around 400-500.
- Internet streaming quality is generally around 80-150.
- Mencoder's default bitrate is 687 kbits/s.
- You need about ~800kb/s to get high quality on a 720x480 sized movie.
Encoding example by setting the bitrate:
mencoder filename.avi -ovc xvid -oac mp3lame -xvidencopts bitrate=687 -o output.avi
Setting the audio bitrate:
You may also want to change the audio bitrate for the audio encoded using mp3lame when you are making a XviD file with the goal of making a small file using -lameopts abr:br=<audiobitrate>. For example, an audio bitrate of 92 (-lameopts abr:br=92) will do nicely for a video bitrate of 150.
mencoder <filename.avi> \ -ovc xvid \ -oac mp3lame \ -lameopts abr:br=92 \ -xvidencopts bitrate=150 \ -o <output.avi>
By bitrate calculated by a fixed file-size:
You can set the filesize you want in KiloBytes in order to make mencoder calculate the bitrate for you. This can be done by setting a "negative" bitrate, bitrate=-700000 will give you a movie file which fits nicely on a CD.
mencoder <filename.avi> -ovc xvid -oac mp3lame -xvidencopts bitrate=-700000 -o <output.avi>
By setting a fixed quality:
fixed_quant can also be used when doing one-pass encoding.
- fixed_quant=<1-31> specifies what "quality level" you want and will make mencoder automatically try to obtain the best quality/size ratio.
- 1 is the highest quality, and will result in very large file if you use it.
- 31 is the lowest, and the video will look very poor.
- 4 and 5 are good setting for high-quality video. 1, 2 and 3 will give you very little improvement compared to 4, but give you way bigger files.(example:1 is 20mb if 4 is 5mb)
mencoder <filename.avi> -ovc xvid -oac mp3lame -xvidencopts fixed_quant=4 -o <output.avi>
 Two-Pass Encoding
 Learning transcode
Basic transcode usage needs these options:
- -i ((input file)
- -o (output)
- -y (format - xvid/divx)
transcode -i movie.mpg -o movie.avi -y divx
Read the transcode manual page for all sorts of incriminating information.
 Special-purpose video encoding tools and tricks
 ffmpeg2theora for HTML 5 video
ffmpeg2theora uses ffmpeg. You can also (ab)use ffmpeg directly. The ffmpeg trick is to use -f ogg, optionally with '-vcodec libtheora and -acodec libvorbis like this:
ffmpeg -i inputfile.dv -f ogg -vcodec libtheora -b 800k -g 300 -acodec libvorbis -ab 128k outputfile.ogv
ffmpeg2theora allows you to convert anything to Ogg Theora video using other (perhaps simpler) parameters:
ffmpeg2theora --optimize --audioquality 4 --videoquality 5 inputfile.ext
The output file will be named inputfile.ext.ogv.
--videoquality 5 is a good choice for web use. 6 and above increases the file-size rapidly without giving you any notable visual improvement.
If you just want to make a ogv real quick then you can just do it with the basic ffmpeg options:
ffmpeg -i oi_nej_jag_tappade_ballen.mp4 oi_nej_jag_tappade_ballen.ogv
 Animated .gif files for ultimate l33tness (or not)
You can combine the power of mplayer & ImageMagick and make .gif files easy as pie.
- Make sure your video file is short. This is highly important as you do want to make sure your animated .gif ends up being reasonably small.
- Convert your video file to a very small resolution. Again, a 20 MB .mp4 file is alright, a 20 MB .gif file is NOT.
Now that you hopefully have a short low-resolution video file to work with:
1. Convert your video file into a series of still images. A trick that is used so successfully by the power l33t is to use mplayers video output (-vo) option to specify that you want jpeg or png images as output.
mplayer -vo png g0atpr0nvideo.avi
mplayer -vo jpeg g0atpr0nvideo.avi
You should now have a folder full of incriminating images. You can use them all or delete a few (every 4 images, for example).
convert *.png animation.gif
Beware that convert has a secret -delay option which could be used to set the time between images in the animated .gif file.
 Some file-formats worth mentioning
 Wintendo .WMV
All the above mentioned tools can convert from the Wintendo .wmv format.
There are no GNU/Linux tools to create this ridiculous excuse for a file-format (click edit if this is wrong) and good riddance for that.