FFmpeg is a great cross platform, open source encoder and converter for a wide range of audio and video file formats, but it’s command line syntax and Unix roots can make things tricky to get it working properly on Windows platforms. Encoding for iPod, iPad or iPhone devices is further complicated by the complexity of the H.264 video codec and it’s plethora of profiles, levels and options that all must be correctly specified in order to get a valid encode. Put the two together, and you have a recipe for head scratching.
In this post, I’ll show how I use FFmpeg on Windows to transcode video files for my iPhone.
First, you’ll need to install a Windows build of FFmpeg, which should come with a number of H.264 settings files in a presets folder, eg: libx264-slow.ffpreset. These files contain partial sets of encoding settings designed to be layered on top of one another to construct a full set of valid settings.
For my encodes, I layer libx264-slow.ffpreset, libx264-main.ffpreset and libx264-ipod640.ffpreset, along with some custom tweak settings. These options define settings for a slow but high quality encode, using the H.264 Main Profile and compatible with the iPod Touch.
To perform the encode I use the following short DOS batch script, passing the filename of the file to be encoded as the single parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
@echo off rem ffmpeg configuration SET FFDIR=C:\Programs\Video\ffmpeg SET FFPRE=%FFDIR%\presets SET FFBIN=%FFDIR%\ffmpeg.exe rem set encoding options for iphone 3gs SET VOPT=-vcodec libx264 SET VOPT=%VOPT% -fpre %FFPRE%\libx264-slow.ffpreset SET VOPT=%VOPT% -fpre %FFPRE%\libx264-main.ffpreset SET VOPT=%VOPT% -fpre %FFPRE%\libx264-ipod640.ffpreset SET VOPT=%VOPT% -s 320x480 -bf 3 -refs 5 SET VOPT=%VOPT% -qmax 25 rem audio options SET AOPT=-acodec libfaac -profile aac_low -ac 2 -ab 128k -ar 48k rem encode @echo on %FFBIN% -i "%1" %VOPT% %AOPT% -y -f mp4 "%~n1.mp4"
Lines 4-6 configure the script for the local FFmpeg installation (you’ll need to adjust these for your own installation); lines 9-12 layer the FFmpeg presets for the video encoding options as discussed above; lines 13-14 apply some custom video encoding settings; line 17 sets the audio encoding options; and finally line 21 actually calls FFmpeg to perform the encode.
The output file will have the same name as the input file except with a “.mp4″ extension. This file can simply be dragged into the iTunes film library and transferred to a device from there. The script creates videos that play back perfectly well on an iPhone 3GS at 320×480 pixels resolution, but other devices may vary.
The main way of controlling the video quality, and therefore filesize, is by adjusting the qmax value on line 14. qmax 25 gives a high quality encode but uses more space on the device: around 1800Kbps or 580Mb for 45 minutes of video (depends on source material). Higher values reduce the quality and filesize. qmax 31 is perfectly watchable and takes about half the space of qmax 25. qmax 51 is still probably fine.
For full details of the qmax, and other, parameters, refer to the FFmpeg help pages.
Getting FFmpeg to create iPhone compatible video files on Windows was a bit of a struggle. I hope this little tutorial will help you avoid similar head scratchings!
No related posts.