bojinov.org

USA Bulgaria Freebies Empty World

Batch Compressing AVI to MP4 on Windows

Introduction

Recently I found out that video clips from my Canon digital camera were too large to send to relatives. It turns out that the AVI files produced by the camera are using the MJPEG codec, which simply encodes each frame of the video in JPEG format. Clearly, better schemes exist that yield the same quality at a much lower bitrate (in other words, your files could be much smaller without a significant loss in quality).

My first reaction was to use some software that I had purchased for the purpose of ripping DVDs from my collection so that they can be viewed on my iPod. Apart from having some bugs, the software in question makes it really hard to batch-process videos. Since I had hundreds of videos that I wanted to transcode, I decided to look for a command-line transcoder and run that from a script. I imagine many people out there might look for the right way of doing this, so here it is for your enjoyment and benefit. I will note that I use Windows, and this document is mostly intended to serve Windows users, although it is applicable in Linux as well.

Specifics

In order to use my script you will need Python (version 2.5 and above, let's say, although the script is simple and likely will work with most versions out there). You will also need to get FFmpeg, which is a free program that performs the transcoding for a single file. FFmpeg contains a sufficient set of codecs to decode Canon's AVI files, and then encode them into a form suitable for viewing from QuickTime, or Windows Media Player, or Flash, etc. The script linked below encodes to the H.264 codec for video, and to AAC for audio, which makes the result readily viewable using Quicktime. You will need to have FFmpeg in your execution path or current directory when you run the script. As of December 2008, you can get a Windows binary from Sourceforge here.

My script is invoked with a single argument, the destination directory. The script will then mimick the directory structure it finds in the current directory, which is used as the source. The script converts all AVI files in all subdirectories of the current one (files in the current directory, or files in sub-sub-directories are not processed).

Here is the script.

The script does have some weaknesses. For example, I did not add any special arguments when running FFmpeg that would ensure a consistent bitrate (in fact, I had difficulty controlling the bitrate, except when I was willing to resize the video). It also doesn't do a full-depth sweep of the directory tree. It could be more configurable. Etc. You should treat this as a freebie. Use at your own risk. Please don't e-mail me if something just doesn't work for you as I most likely won't have the time to respond.

Update: September 2010

It seems that with the latest versions of FFmpeg, encoding with libx264 is no longer this simple and requires a preset file. I have modified the Python script to use the latest syntax of the command. The supplied preset file needs to be present in the same directory as the script and the FFmpeg binary (which is also the root of the source directory). Modify to suit your needs. In the new version, we specify a size of the output video (480x270, which happens to be a 16:9 ratio) rather than bitrate. This works well for shrinking HD movies, such as those produced by the latest Canon cameras.

The updated script as well as the preset file.

Conclusion

I hope you find this write-up useful. If you managed to find a solution to the bitrate problem I mentioned, please do e-mail it to me, and I can update this page to include references to your improvements. I would also be interested in hearing if this is redundant with some existing product. Enjoy!

©1992-2015 Hristo Bojinov. Contact: hristo-at-bojinov-dot-org. This address is subject to change.


We will not be liable for any damages anyone suffers because of using this information. While we believe all we write is correct, there may be errors. The contents of this site are provided without any expressed or implied warranty of fitness for any purpose. Use your judgement, and use at your own risk.