I have some videos from a few years ago,with various formats, such as avi, mpg, wmv, rm, rmvb, .... Their sizes are huge(more than 500 MB, and sometimes > 1GB).

  1. Given there may likely be some advance in data compression, I would like to know which file formats and compression methods are recommended these days, by the standard that without losing obvious data, while achieving big size reduction.
  2. How can I perform the file format conversion and data compression in Ubuntu 12.04? Command line and batch ways would be the most convenient, although GUI ways are also appreciated.

Thanks and regards!

  • 2
  • Essentially the same goal. Check our blog entry for more info about FFmpeg and how to install it from source in Ubuntu. Let me know if you need more info, I can write up an answer. – slhck Jun 3 '12 at 1:09
  • @slhck: Thanks! I have installed both ffmpeg and libx264-120. I tried your recommended command ffmpeg -i input.avi -vcodec libx264 -crf 22 output.mp4. (1) But there is one warning: "This program is not developed anymore and is only provided for compatibility. Use avconv instead (see Changelog for the list of incompatible changes)." (2) Also there is an error: "Unknown encoder 'libx264". Do you know how to deal with the two messages? – Tim Jun 3 '12 at 1:32
  • I also installed x264. but things still are the same. (3) without -vcodec libx264, it also says "encoder 'aac' is experimental and might produce bad results. Add '-strict experimental' if you want to use it." And "Seems stream 0 codec frame rate differs from container frame rate". – Tim Jun 3 '12 at 2:59
  • @Tim Concerning your "Unknown encoder 'libx264'" error with the binary package, you can also look at stackoverflow question Unknown encoder 'libx264'. I ran into that same error (on Linux Mint 13) and was able to fix it by running apt-get install libavcodec-extra-53. – Michael Krebs Nov 26 '12 at 2:50

Ubuntu and its bundled FFmpeg

Well, you've run into the problem that FFmpeg doesn't ship with Ubuntu anymore but you've tried to install it through apt-get.

This program is not developed anymore and is only provided for compatibility

This is simply a lie. FFmpeg is actively developed, but the Ubuntu packages are outdated and they switched to libav. See here for the bug report that discusses the wording of the message.

Installing from source or using a static build

If you're not into compiling yourself, you can simply grab a static build from the download page.

If you want to stay up to date and configure the tool yourself you'll need to compile it from source. Check the Ubuntu compilation guide for a detailed howto on installation. Following these steps, you'll end up with the newest version of both FFmpeg (the encoding tool) and x264 (a certain video encoder).

What do I need to reduce the size?

You ask …

which file formats and compression methods are recommended these days

In fact, there's only one compression method ("codec") everyone would recommend. It's the H.264 or MPEG-4 Part 10 / AVC standard, which specifies a compression method for low bandwidth video. H.264 also achieves great results for higher bandwidths. A very common encoder for it is called "x264", the one we've installed above.

It supersedes MPEG-4 Part 2, the video codec standard that made the "DivX" and "XviD" encoders famous. These are good too, but they need more bandwidth to preserve the same quality.

The actual file format does not matter. H.264 video can reside in almost any container, from AVI to MP4, MOV and MKV. It's important to know the difference between a video codec per se, and the container that simply encapsulates it. See here for more: What is a Codec (e.g. DivX?), and how does it differ from a File Format (e.g. MPG)? I would choose MP4 or MKV as output container these days. AVI is just too old and doesn't allow you to do much with the video, and for the aforementioned containers there are better tools for remultiplexing available.

When you want to reduce the size, you want to reduce the average bitrate of the video. In the simplest case this would mean forcing the encoder to, let's say, 1.5 MBit/s constant bit rate. This works, but it has a few drawbacks. Instead of encoding with a fixed bit rate, let the encoder choose how much it wants to spend. This results in an average bitrate, adapted to the contents of the video. Do this by using the Constant Rate Factor option.

For x264, this means setting a value somewhere between 18 and 26 for this CRF. The default is 23, and higher values will give you worse quality, and vice-versa. You don't need to know exactly what it does, but if you're interested see this CRF guide.

How do you perform the conversion?

Once you have FFmpeg up and running, you can simply convert a video to x264 / MP4 with AAC audio at 192 kBit/s fixed bitrate:

ffmpeg -i input.avi -c:v libx264 -crf 23 -c:a aac -strict experimental -b:a 192k output.mp4

That's it. You can vary the output size and quality by adjusting the CRF value. Lower means better quality and larger size. Higher means worse quality and lower size. As I said, sane values are anywhere between 18 and 26, maybe more. If you really want to reduce the size of the input video, choose the CRF value that still delivers acceptable video quality with a reduction of file size.

To test how your video would look, you can press Q any time during the conversion. It'll leave a truncated file which should still be playable though. You could also supply the -t <seconds> option, where <seconds> specifies how long you want to encode (e.g. the first 30 seconds only).

For more settings see the H.264 encoding guide.

Batch processing

If you have a batch of AVI files that you want to convert, you could do something like that, which recursively transcodes all files, for example in Bash:

while IFS= read -d '' -r file; do 
  ffmpeg -i "$file" -c:v libx264 -crf 23 -c:a aac -strict experimental -b:a 192k ${file%%.avi}-converted.mp4 
done < <(find . -iname "*.avi" -print0)

You can change the .avi suffix in this command and let it run again for other files.

Note that video encoding is a time consuming task and it takes a while to find the perfect settings for your source files.


Another versatile (and with a very practical GUI) tool is Avidemux.

Here's some detailed info on how batch processing works: Avidemux DocuWiki - Batch processing

  • Can you batch-convert videos? How can you set the codecs and quality levels for the result videos? It'd be great if you could add a little to your answer :) – slhck Jun 16 '12 at 15:25
  • Right. I have actually always used it as a GUI tool, but found just now that it could used for batch processing. Will add a note on the answer. – Mark0 Jun 17 '12 at 17:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.