2011
04.24

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!

Related Posts

No related posts.

6 comments so far

Add Your Comment
  1. Why are you using Main profile for devices? Older iPhones won’t necessarily be able to play Main profile video–their specs dictate Baseline profile, and if you want extreme compatibility, only 1 reference frame too. I also noticed that your switches enabled B-frames as well, which is a Main (or High) profile-only option.

    If you’re looking to maximize portability with iOS and Android devices, I’d recommend using two sets of H.264 files: Baseline for mobile, and Main for tablet/desktop. My “OmniRx” encoding on videoRx.com does these two sets of H.264 for deployment. I also wrote more about considerations for devices and encoding in my recent blog post at http://blogs.flashsupport.com/robert/2011/04/flash-video-is-coming-to-ipadyet-another-streaming-solution-will-be-available-for-h264-to-ios/.

  2. I’m not looking to maximise compatibility :) I’m encoding specifically for iPhone 3GS, which supports Main Profile, b-frames and multiple reference frames (at least in my testing).

  3. The tech specs on apple.com at http://support.apple.com/kb/SP565 state Baseline, but you’re not the only renegade to go against specs and push Main profile. Could potentially eat up more battery power using Main since decoding requirements are higher for Main over Baseline. I’d have to investigate that.

  4. Fair point. If it’s a concern, replacing lines 11-13 in the script with those below should create more widely compatible output files:

    SET VOPT=%VOPT% -fpre %FFPRE%\libx264-baseline.ffpreset
    SET VOPT=%VOPT% -fpre %FFPRE%\libx264-ipod320.ffpreset
    SET VOPT=%VOPT% -s 320x480 -refs 1

  5. Shouldn’t the output resolution be 480×320? After all, if you’re watching the video in landscape mode on an iPhone or iPod Touch that would be the HxV resolution you want to match.

  6. Were you able to test FFMpeg with Iphone 4 or 4S. What version of ffmpeg are you using? So Far I havent figure out the Baseline profile yet Level 3 can you provide more detail about your ffmpeg command line