Converting AVI to MP4 with ffmpeg

I wanted to convert a file from AVI (mpeg2, mp3) to MP4 (mpeg4, aac) to be able to play it on another device (one without VLC). I found a ‘helpful’ post on how to do this, but the commands were for a much much older (2008) version of ffmpeg. I then came across a ubuntu forums post that used the same commands but updated for the neweer (but still 2009) version of ffmpeg. After a little more fiddling, I finally found something that worked fantastic!

updated 2017-02-12

There are some new kids in town when it comes to best practices.

First of all we want to use the built-in AAC encoder with ffmpeg now.

Secondly we want to use H.264 instead of -vcodec mpeg4 and just let it do it’s thing without messing with the bitrate. Also, we want to use a Constant Rate Factor for h.264.

TL;DR

If you don’t know what most or all of the below command means, you should probably read this entire post first.

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

thanks to Werner Robitza for motivating me to update this and researching the new hotness.

original post from 2012-05-10

Caveats:

If you’ve already got ffmpeg and an appropriate aac encoder, here’s the ffmpeg command you’ll need:

NOTE: this is the old and busted command. see updated command above.

ffmpeg -i input.avi -acodec libfaac -b:a 128k -vcodec mpeg4 -b:v 1200k -flags +aic+mv4 output.mp4

The long story

I ran the above command on a Macbook Air running OS X Lion 10.7.3 and installed ffmpeg and it’s dependencies with homebrew. There were a couple steps to get to this point.

The following assumes you have homebrew installed and use the brew command.

Installing ffmpeg

brew install ffmpeg

If you don’t have libogg installed (which is likely) brew will choke and give you a couple more steps. It will probably tell you to run brew link libogg, and then the install command above again.

If you don’t care much about how the actual ffmpeg command works, you can now run the below command (replacing ‘input.avi’ and ‘output.mp4’ with their respective values) and you’re done.

ffmpeg -i input.avi -acodec libfaac -b:a 128k -vcodec mpeg4 -b:v 1200k -flags +aic+mv4 output.mp4

ffmpeg command explained

I won’t go into too much detail about the command, just some high points.

-acodec libfaac

This uses the faac codec to encode the audio to aac.

-b:a 128k and -b:v 1200k

These are the audio and video (respectively) bitrates of 128kb and 1200kb

-flags +aic+mv4

These options are explained in more detail here

Customizing

The beauty with using ffmpeg is that there are zillion options for customizing how your file is output. The most noticable things will be upping the audio and video bitrates, and using a better audio codec for aac.

Discuss

blog comments powered by Disqus