Convert old videos to have smaller sizes

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!


Solution 1:

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

Solution 2:

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