Take the 2-minute tour ×

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!

share|improve this question
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
add comment

2 Answers

up vote 5 down vote accepted

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 either from Burek or Relaxed. Extract the folder and use the ffmpeg binary that's in there.

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 libfaac -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).

Batch processing

If you have a batch of AVI files that you want to convert, you could do something like that:

while IFS= read -d '' -r file; do 
  ffmpeg -i "$file" -c:v libx264 -crf 23 -c:a libfaac -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.

share|improve this answer
Great answer. +1 –  Mr.Wizard Jun 3 '12 at 8:57
Thanks! (1) Do you recommend to install libvpx after installing x264? (2) After installation from source, I tried your conversion command, but there is an error Unrecognized option 'c:v' Failed to set value 'libx264' for option 'c:v'. –  Tim Jun 3 '12 at 12:11
@Tim You don't need libvpx – it's for WebM video only. Ad (2): Have you followed the tutorial to install FFmpeg from source, with the libx264 option enabled (i.e. --enable-libx264)? If you call ffmpeg -codecs list | grep x264, does it give you at least one libx264 option? If not, something with your installation went wrong. Make sure you uninstalled the old apt-get version of FFmpeg too, so that a call to ffmpeg results in the version you just compiled. –  slhck Jun 3 '12 at 12:30
Thanks! I got it. Now running the conversion. It takes really long. I wonder how to know the progress, i.e.how much percentage of the work has been done at each moment? –  Tim Jun 3 '12 at 13:27
Also after the conversion finished, the size changes from 55MB for a FLV file to 67MB for a mp4 file, using the one before last command in your reply. I can't observe difference in visual quality. I wonder if the fixed rate 192 kBit/s is always recommended? Is it good to keep it the same as the original file? –  Tim Jun 3 '12 at 13:49
show 12 more comments

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

share|improve this answer
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
add comment

Your Answer


