Convert any video file for an iPod or iPhone
Problem
You want to use your Apple iPod or Apple iPhone to play video, but you don't know how to convert video so it will play.ffmpeg/libfaac problem: Ubuntu 9.10 (Karmic Koala), 10.04 (Lucid Lynx), 10.10 (Maverick Meerkat), and 11.04 (Natty Narwhal) all ship with a version of ffmpeg without libfaac support because of licensing issues. This means you will need to install a package from the medibuntu repository to enable the libfaac encoder. (See below.)
Keywords
apple, ipod, iphone, linux, ffmpeg, convert, avi, mpg, mpeg, asf, video, 320x240, 480x320, 960x640, mp4, resolution, aspect ratio, aac, libfaac.Solution
If you are not interested in the details, you can jump straight to the bottom of this page and download mp4ize.Other people solved this problem, also. You can start out by reading some related pages.
Enable the libfaac encoder
Add the following line to your /etc/apt/sources.list:deb http://packages.medibuntu.org/ natty free non-free(This obviously assumes you are running Ubuntu 10.10 (Maverick Meerkat).)
Then install the package:
sudo aptitude update sudo aptitude reinstall libavcodec-extra-52
Converting videos
The following examples assume ffmpeg version 0.6-4 and the presence of the libavcodec-extra-52 package from the medibuntu repository.Assuming your movie is called "some_movie.avi" and ignoring the aspect ratio, you can convert it to an mp4 file naively as follows:
# this messes up the aspect ratio! ffmpeg -i some_movie.avi -f mp4 -vcodec libxvid -maxrate 1000 \ -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec aac -strict experimental -mbd 2 -s 320x240 -ab 128 -b 400 some_movie.mp4
(John Taggart pointed me at a web page that has ffmpeg transcoding profiles for various devices.)
The only real thing to watch out for is, as mentioned, the aspect ratio. My iPod video comes with a 320x240 display. You can figure out the aspect ratio of an existing video file as follows:
ffmpeg -i some_movie.aviThere should be a line somewhere that sort of looks as follows:
Stream #0.0: Video: mpeg4, yuv420p, 624x352, 23.98 fps(r)The aspect ratio is the first number (before the 'x') divided by the second number (right after the 'x'). You need to adjust the height of the generated movie appropriately. Assuming an iPod with a 320 pixel wide display, the height can be computed as:
new_height = (320 / (movie_width/movie_height))You should compensate the height you lose (i.e., 240 - new_height) by padding it with black bars. Each bar gets (240 - new_height) / 2 pixels.
You can pass the desired width and height to ffmpeg using the -s parameter. You can also pass a -padtop and -padbottom flag to insert the black bars. For example, for a 16:9 movie:
# this maintains the proper aspect ratio for a 16:9 video ffmpeg -i some_movie.avi -f mp4 -vcodec xvid -maxrate 1000 \ -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec aac \ -mbd 2 -s 320x180 -padtop 30 -padbottom 30 \ -ab 128 -b 400 some_movie.mp4
The automatic way: mp4ize
This ruby script (version 2012/04/20) does all of this automatically for you. (Somebody wrote a Python version of this script, but it may not be as up-to-date.) It figures out the aspect ratio of the movie you're trying to convert and inserts the blacks bars at the top and bottom. If the movie is narrower than it is high, it inserts the padding on the sides. You should use the script as follows:mp4ize movie1.avi movie2.asf movie3.mpg ...In this example, the output files will be named movie1.mp4, movie2.mp4, and movie3.mp4.
If you are converting to the iPhone, you should also include the --iphone argument as follows:
mp4ize --iphone movie1.avi movie2.asf movie3.mpg ...