1
15 October
2007

Tricks and incantations for resizing for iPhone, Android and other things

These are shell incantations for transcoding video with ffmpeg, especially for the ipod or iphone. ffmpeg is one of several free open source video editing programs you can use. These recipes are here partly to make it easier for other people to find them, and partly for me to find them again.

The standard open-source programs for transforming video are ffmpeg and mencoder. Each is capable from reading video in many different files formats, and writing out video using many different video encoding options. Each is supported in some form on Mac OS X, Linux and probably Windows. Both use common source libraries. You can get ffmpeg at http://www.mplayerhq.hu/design7/dload.html in many forms, or else compiled for Win32 at http://sourceforge.net /project/showfiles.php? group_id=205275.

mencoder has more filters than ffmpeg for processing the video. ffmpeg supports more different input and output formats, including direct output to mp4 container files. mencoder support for non-avi files is new and questionable, so for use with Quicktime you need to post-process with mp4creator: that's too much hassle for me, but instructions can be found at: mplayerhq.

My preferred solution is simply to use ffmpeg.

Basic command for iphone-compatible video
ffmpeg -i input-file -f mp4 -vcodec mpeg4 -maxrate 1000 -b 700
-bufsize 4096 -g 300 -acodec aac -ab 192 -s 480x320 output-file

iphone (better setting than above)
INFILE="$1"
OUTFILE="$2"
ffmpeg -i "$INFILE" -f mp4 -vcodec mpeg4 -maxrate 1000 -b 700 -qmin 3 -qmax 5
-bufsize 4096 -g 300 -acodec aac -ab 192 -s 480x320 -aspect 4:3 "$OUTFILE"

This should be fine with any ipod-like device, but it is customized for the iPhone screen size. The -s 480x320 refers to the size of the iPhone screen; customization for the video iPod would use the same incantation, but with a screen size of 320x240 to produce slightly smaller files. On the other hand, showing the video on TV would benefit from the larger size, or even 640x480. The Apple TV uses as much as 1280x720. (It is never worth using a larger size than your input footage, however, which is often limited to 640x480 for NTSC.) Thus, for the ipod video is might be:

ipod video
ffmpeg -i "$INFILE" -f mp4 -vcodec mpeg4 -maxrate 1000 -b 700 -qmin 3 -qmax 5
-bufsize 4096 -g 300 -acodec aac -ab 192 -s 320x240 -aspect 4:3 "$OUTFILE"
Other more exotic settings are the following:

sample separate image frames to jpeg images
ffmpeg -i "$INFILE" -y -ss 5 -an -sameq -f image2 -r 1/4 frame%03d.jpg

remove logo(from) from commercial video with lavc mpeg4 codec, avi container
mencoder -vf "delogo=545:401:100:45" -ovc lavc -lavcopts vcodec=mpeg4:acodec=libfaac 002.mpg -o 002.avi

Contrast enhancement (not recommended, can confuse compression)
mencoder -ovc lavc -lavcopts vcodec=mpeg4:acodec=libfaac -vf pp=autolevels:f 002.mpg -o 002.avi

WARNING: note that the -i inputfile flag needs to come early otherwise the resize (resizing/rescaling) flag -s will not have any effect. This is incredibly stupid, yes. At one point I found it didn't work and it took me a long time to figure out the problem that caused my resize to fail.

If you want to play with these, a quick guide to the key (somewhat obscure) ffmpeg parameters are as follows:

`-title string'

Set the title.

`-b bitrate'

Set the video bitrate in bit/s (default = 200 kb/s).

`-ss position'
Seek to given time position in seconds. hh:mm:ss[.xxx] syntax is also supported.

-r fps
Set to frame rate

`-s size'
Set frame size. The format is `wxh' (ffserver default = 160x128, ffmpeg 
default = same as source). A few of the many allowed appreciations are:
`qqvga'
160x120
`qvga'
320x240
`vga'
640x480
`-maxrate bitrate'
Set max video bitrate (in bit/s).
`-minrate bitrate'
Set min video bitrate (in bit/s).
`-ar freq'
Set the audio sampling frequency (default = 44100 Hz).
`-ab bitrate'
Set the audio bitrate in bit/s (default = 64k).
`-ac channels'
Set the number of audio channels (default = 1).
`-an'
Disable audio recording.
`-acodec codec'
Force audio codec to codec. Use the copy special value to specify
that the raw codec data must be copied as is.


By Gregory Dudek at | Read (8) or Leave a comment |    
Comments
Re: video transcoding recipes with ffmpeg

Hello,

thank for the command line to generate video for my ipod touch. I have a little question : do you know how to embed a subtitle (srt file) into the video. I didn't succeed. It seems to be possible with mencoder but not sure how to do this.

Posted by: smithbob at November 03,2008 17:07
Re: video transcoding recipes with ffmpeg

Sorry, I've never tried using a subtitle file. It would be a nice feature in general, but in the iphone I can see the small screen making them hard to read.

Posted by: GDudek at November 05,2008 16:26
Re: video transcoding recipes with ffmpeg

Hi, i wish to generate a metadafile with keyframe position that corresponding to each timestamp. However it requires me to input -r (rate) in the ffmpeg parameter. I just wonder how do i know the biggest/suitable rate for a video that i should input? Is there an option for me to get the exactly maximum rate? -maxrate?

[See response in next comment -gd]

Posted by: mimi at November 17,2008 22:56
maximum rate

Mimi, your question is reasonable, but you should note that for some data streams the frame rate does not have to be fixed (for example an MPEG2 transport stream). As a result, if you choose the shortest inter-frame interval for the "-r" parameter, you could drastically over-sample some portions of the video.
I am not sure what the best solution is, but I suggest getting the info with ffmpeg. You can use the "-i" flag (info) for this:
ffmpeg -i recordedfile.mpg
and look for the line that says something like "Stream #0".

I hope this helps.

Posted by: dudek at November 18,2008 16:20
Re: video transcoding recipes with ffmpeg

Hello,
Thanks for the recipes. I myself use mencoder for my ipod touch. I also include subtitles in it when I need to. Here's my mencoder recipe with subtitles (i don't even remember how i got there =D) :
mencoder "$INFILE" -o "$OUTFILE" -vf scale=480:-10,harddup -lavfopts format=mp4 -faacopts mpeg=4:object=2:raw:br=128 -oac faac -mc 0 -noskip -ovc x264 -sws 9 -x264encopts nocabac:level_idc=30:bframes=0:global_header:threads=auto:subq=5:frameref=6:partitions=all:trellis=1:chroma_me:me=umh:bitrate=1250 -sub "$SUBTITLES.SRT" -ffactor 1 -fontconfig -font Arial -subfont-text-scale 4 -of lavf

Since this is a long command-line, I made myself a script so that I just have to type "mycommand input output subtitles". I don't think posting it would be relevant but I just thought mentioning it would be fine.

Posted by: Hello at February 25,2009 06:45
Re: video transcoding recipes with ffmpeg

Here's a screenshot !

Free Image Hosting at www.ImageShack.us

QuickPost Quickpost this image to Myspace, Digg, Facebook, and others!

Posted by: Hello at February 25,2009 07:04
Re: video transcoding recipes with ffmpeg

There is another useful article on streaming for the iPhone located at http://linux.goeszen.com/http-live-streaming-to-the-iphone.html

Posted by: dudek at January 09,2011 14:38
Re: video transcoding recipes with ffmpeg

I am running ./configure --enable-cross-compile --arch=arm --target-os=darwin --cc='/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc -arch armv7' --sysroot=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk --cpu=cortex-a8 --enable-pic

for making ffmpeg compatible for iphone. its executes fine. But when i write make and press enter .It executes for a interval then give some errors. Can somebody guide me how to run "make".

Running ffmpeg on the iphone itself isn't what I would want to do, since the iPhone itself is not that fast. At any rate, debugging the compiler errors from make depends on the very specific errors you get --dudek ]

Posted by: anonymous at January 10,2011 05:45
Trackbacks
Please send trackback to:/blog/82/tbping
Convert for iPhone using mencoder/ffmpeg

I found this through the Google cache of http://www.dudek.org/blog/82.
These are shell incantations for transcoding video with ffmpeg. These are here partly to make it easier for other people to find them, and partly for me to find them again.
The stan...

Posted by: onlydarksets at June 18,2008 09:13
Convert for iPhone using mencoder/ffmpeg

I found this through the Google cache of I found this at http://www.dudek.org/blog/82.  I’m not sure why I couldn’t access it before, but, like Mark Twain, the reports of it’s death were greatly exaggerated.  For me, the most releva...

Posted by: onlydarksets at June 18,2008 09:16
Post your own response

Each comment is manually screened for the presence of appropriate and substantive content, due to a constant onslaught of comment-spam. This means there may be a delay before your comment appears.


(Some kind of name is required, will be visible)

Required, whatever you enter will be visible to other users.


(Optional, used for "mailto" link)

Your email address is not required, but if you insert it it will be displayed so people can contact you.

Answer this question correctly to demonstrate that you are not a dumb spambot.



The title for your comment.



Your comment goes here. All relevant comments are welcome, except for those that simply promote an irrelevant product or else are used to fraudulently inflate the link count to an irrelevant web page. They appear after moderation. Don't forget to also fill in the captcha below or your text will be rejected automatically!

You must answer this question to prove you are human
Which one has ice cream (pick a number 1-5): 1:DairyQueen, 2:KFC, 3:FBI, 4:NFL, 5:NBC-TV?

Answer this question correctly to demonstrate that you are not a dumb spambot.