Convert any video file for an iPod or iPhone
Problem
You want to use your fancy new Apple iPod Video or Apple iPhone, but you don't know how to convert video so it will play.Update: Ubuntu 9.10 (Karmic Koala) ships with a version of ffmpeg without libfaac support because of licensing issues. This means you will need to compile ffmpeg yourself. This guide explains how.
Keywords
apple, ipod, iphone, linux, ffmpeg, convert, avi, mpg, mpeg, asf, video, 320x240, 480x320, mp4, resolution, aspect ratio.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.
Compile ffmpeg by hand (see below for install from package)
Roughly following other instructions. Uninstall ffmpeg.$ sudo aptitude remove ffmpegInstall some packages:
sudo aptitude install liba52-dev libdts-dev libgsm1-dev libvorbis-dev \
libxvidcore4 libxvidcore-dev libfaac-dev \
libschroedinger-dev libx264-dev libdc1394-22-dev \
libamrnb-dev libamrwb-dev \
libmp3lame-dev libfaad-dev \
libtheora-dev libsdl1.2-dev \
libspeex-dev libxvmc-dev texi2html doxygen
Download ffmpeg source code, unpack it.
$ wget http://ffmpeg.org/releases/ffmpeg-0.5.tar.bz2 $ tar xvfj ffmpeg-0.5.tar.bz2Then compile it.
$ cd ffmpeg-0.5
$ ./configure --enable-gpl --enable-nonfree --enable-shared --enable-postproc \
--enable-avfilter --enable-avfilter-lavf --enable-pthreads --enable-x11grab \
--enable-bzlib --enable-libamr-nb --enable-libamr-wb --enable-libdc1394 \
--enable-libfaac --enable-libfaad --enable-libfaadbin --enable-libgsm \
--enable-libmp3lame --enable-libschroedinger \
--enable-libtheora --enable-libvorbis --enable-libx264 \
--enable-libxvid --enable-zlib --prefix=/usr/local/
$ make
$ sudo make install
Note that you may have to set LD_LIBRARY_PATH to
/usr/local/lib/ to run ffmpeg.
Create modified ffmpeg package
If you don't want to pollute your machine with manually compiled and installed software, you can build a modified package yourself, as follows:
$ sudo aptitude install debhelper quilt zlib1g-dev libsdl1.2-dev \
libfreetype6-dev libimlib2-dev texi2html \
libtheora-dev fakeroot build-essential \
devscripts ubuntu-dev-tools dh-make \
module-assistant cdbs debconf-utils fakeroot
$ apt-get source ffmpeg
Now modify debian/confflags (around line 51) such that:
gpl_confflags += --enable-x11grab gpl_confflags += --enable-nonfreeThen create the package:
dpkg-buildpackage -rfakeroot -bThen install the resulting .deb files in the parent directory (you can skip the -dev packages, but they don't really hurt either):
sudo dpkg -i *.deb
Converting videos
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 xvid -maxrate 1000 \
-qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec aac \
-s 320x240 -ab 128 -b 400 some_movie.mp4
If you get an error, "Unknown codec 'xvid'", try changing "-vcodec xvid" to
"-vcodec libxvid". (mp4ize will try "xvid"
and "libxvid".)
If you get an error, "Unknown encoder 'aac'", try changing "-acodec aac" to "-acodec libfaac". (mp4ize will try "aac" and "libfaac".)
If you get an error, "WARNING: The bitrate parameter is set too low. It takes bits/s as argument, not kbits/s", try multiplying the -b and -ab arguments by 1024, or add a "k" at the end of the number. (mp4ize will detect the error message and fix the arguments automatically.)
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 \
-s 320x180 -padtop 30 -padbottom 30 \
-ab 128 -b 400 some_movie.mp4
The automatic way: mp4ize
This ruby script (version 2009-11-05) does all of this automatically for you. 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 ...
