Video conversion cheat sheet

I was faffing around with batched video conversion at work today, trying to recall how to get the powerful yet forboding interfaces of MEncoder and ImageMagick to play ball. I've used them both quite extensively before - most recently when encoding in-game footage from Millennium 3. As a younger hacker, I could remember arcane command line options forever, but these days it's a frustrating cycle of never using these tools quite often enough for the syntax to really stick. Such are the ravages of maturity on the capacity to learn!

Anyway, having wasted a load of time today scouring through 1,000KB man pages, I figured I'd jot down the command lines that did what I wanted, and publish them here for future reference. In my experience, what you want is an example of a use case that's vaguely similar to what you want to do, and then you can go look up individual options in the documentation and tweak things to fit your needs.

I'll expand this page as I find new examples. If you have any to share, or if I've said something blatantly wrong, post in the box below.

Examples

Open foo.png, resize to 640x480 ignoring aspect ratio, save to foo.jpg with JPEG quality 60.
The ! (ignore aspect ratio) may need to be escaped in some shells (e.g. \!).

convert -resize 640x480! -quality 60 foo.png foo.jpg

Extract audio track from foo.avi, resample to 32KHz, output to foo.wav.
-ni means force non-interleaved mode, useful for reading badly interleaved files correctly.

mplayer -ni -vc null -vo null -af resample=32000 -ao pcm:file=foo.wav foo.avi

Output frames of foo.avi to BMPs named foo_00001.bmp, foo_00002.bmp, etc.
The % needs to be escaped if you're inside a Windows batch file, i.e. foo_%%05d.bmp.
ffmpeg infers the output format from the filename. For JPEG, though, I couldn't find a way to control the compression level (quality), so I output to BMPs and then converted those to JPEG with Imagemagick's "convert".

ffmpeg -i foo.avi -f image2 foo_%05d.bmp

This outputs frames of foo.avi to JPEG files. Each frame is scaled to 640x480 and saved in directory foo_frames with JPEG quality 80.
-noframedrop stops MPlayer from trying to do the conversion in realtime. Without this option, it drops frames, and you end up with fewer JPEGs than you expected.
-noframedrop wasn't included in the MPlayer man page's huge list of command line options, but was sneakily mentioned in a usage example at the end.

mplayer -ni -noframedrop -vo jpeg:quality=80:outdir=foo_frames -vf scale=640:480 foo.avi

This creates a video foo.avi from all the JPEGs in the current directory, with foo.wav as the audio track.
Video format is MS MPEG4 v2 (a good lowest common denominator format for Windows XP/Vista systems) at 25fps. The audio track is encoded to MP3 format using LAME.

mencoder -audiofile foo.wav mf://*.jpg -ovc lavc -lavcopts vcodec=msmpeg4v2 -mf fps=25 -oac mp3lame -o foo.avi

For each foo.avi in the current directory, extract the audio track to foo.wav.

FOR %%i IN (*.avi) DO CALL :encode "%%i" "%%~ni.wav"
GOTO :end

:encode
mplayer -ni -noframedrop -vc null -vo null -ao pcm:fast:file=%2 %1

:end

For each foo.avi in the current directory, make a new movie with_new_audio/foo.avi, with a video track copied from foo.avi, and a PCM audio track from foo.wav.

FOR %%i IN (*.avi) DO CALL :encode %%i
goto :end

:encode
mencoder -ovc copy -oac pcm -audiofile "%~n1.wav" -o with_new_audio/%1 %1

:end

Finally, I always end up needing a list of the codecs available via mencoder -ovc lavc -lavcopts vcodec:codec_name. So here are the possible values for codec_name:

mjpeg, ljpeg, h261, h263, h263p, mpeg4, msmpeg4, msmpeg4v2, wmv1, wmv2, rv10, mpeg1video, mpeg2video, huffyuv, ffvhuff, asv1, asv2, ffv1, flv, dvvideo, svq1, snow

Links

IBM developerWorks ImageMagick tutorial
ffmpeg Windows binaries (you'll need 7-Zip)
Gentoo MEncoder HOWTO

Posted by pwr (site) at May 17, 2007, 8:38pm. Category: blog. semipermalink Tags: blog, video, mplayer, ffmpeg, imagemagick

Comment on this

StumbleUpon Toolbar Share on Facebook

Post a comment

name:
email:
site:
All three fields are optional. Your email address will only be made available to the site owner.




counter impenetrable trees have ravaged this planet since August 2002. You appear to be browsing from country code US.
This site and all its contents (except where otherwise stated) are Copyright © 1996-2007 Paul Roberts.
Powered by clunkyblog release 3.00. clunkyblog is Copyright © 2003, 2004, 2007 Paul Roberts. Generated in 6080ms.
Valid CSS! Valid XHTML 1.1!