24/01/2008

Fun with MPlayer and ImageMagick

Just a loose collection of stuff I found very easy to do with MPlayer and, occasionally, ImageMagick.

There is no VirtualDub for Linux; if you want to batch-extract 50 interlaced videos to pixmaps, crop, and deinterlace both half-frames into separate image files, Avidemux is not an option. After some googling I got the following solution:
mplayer -vf tfields=1:0,crop=xheight:yheight:xoffset:yoffset -frames <numberofframes> -benchmark -nosound -vo pnm:pgm foo.avi.
The actual deinterlacing is done by the tfields=1:0 videofilter, which outputs two split frames from one interlaced one. The flag 1 means interpolation of missing lines (without you just get a squeezed half-frame), the 0 flag (deprecated, my bad) means temporal precedence of even half-frames (set to an odd number to switch half-frames). The video output filter -vo pnm:pcm outputs portable greymaps, most common image types work also, e.g. -vo jpeg. -benchmark and -nosound speed it up a bit, as mplayer doesn't keep to frame rates that way (actually, in my case with 120 fps, I might get dropped frames). The -frames option just gives you a subset of frames (starting at the beginning of the movie).

Extract an audio stream from a video DVD and compressing it to mp3:
mplayer -benchmark -vo null -ao pcm:file=/home/avocadohead/dvdaudio.wav dvd:// (-dvd-device <e.g. /dev/sr0>)
The -dvd-device option is necessary if mplayer doesn't find your DVD drive ("Couldn't open DVD device"). Use the output of wodim --devices to get the right device. If you want to cut the resulting wav file, try wavsplit:
wavsplit dvdaudio.wav 3:00 6:21 8:20
(see Aaron Birenboim's page). If you want to do more or need a GUI, Audacity is a great sound editor. Compress with LAME, e.g.:
lame -h -V 6 dvdaudio.wav dvdaudio.mp3
Warning: This might not be important in the days of abundant disk space, but consider that you will need roughly 500-600 MB of temporary space for each hour of uncompressed WAV. MP3 compression shrinks this by a factor of 10, more or less.

Encode a series of jpegs into a movie (Xvid codec, frame rate 12 fps, mplayer encodes the images in alphanumerical order).
mencoder mf://*.jpg -fps 12 -ovc xvid -o -xvidencopts bitrate=<bitrate> output.avi

Do the reverse - extract jpeg frames from animated GIF:
mplayer -vo jpeg <somegif>.gif
Ought to work for other movie/image formats, too.
Ricardo Fabbri recommended (see comment below)
mogrify -format png video.avi

Record streaming media from a SMIL or ASX playlist:
First, download and open the playlist to find out what file it it refers to:
wget <someaddressofaplaylist>.smil
vi <playlist>.smil
Now copy the URL of the streaming media file from the SMIL file (typically .rm) and play it in mplayer (no audible output with dumpstream option)
mplayer -dumpstream <media file URL>
mv stream.dump mediafile.rm

I didn't get Real Player for Linux to play the extracted stream, maybe it was damaged or missed headers or whatever. Wav conversion worked, though.
mplayer -quiet -vo null -vc dummy -ao pcm:waveheader:file="<wavfile>.wav" "mediafile.rm"
ASX works analogously, the resulting movie file type is WMV or WMA. You can also dump a DVD to MPEG:
mplayer -dumpstream dvd://

Record a Windows Media streaming movie (must play with the mplayerplug-in in browser). Right click on movie in browser, save as… This gets you an ASX markup file linking to the ASF stream (address in <ref> tag). In a shell, dump the stream with mplayer and rename afterwards (to WMV or ASF) as described above for Real Audio.

Create a PDF file from a series of consecutively named image files with ImageMagick:
Make sure there are no other images with the same extension in the same folder, and that your images are named in alphanumerical order (like img_001.jpg, img_002.jpg etc.).
avocadohead@tisiphone:/home/avocadohead/imgfolder> convert *.jpg mypdf.pdf
As simple as that.

All batch image conversion/scaling: mogrify does the trick.
mogrify -scale 20% *.jpg
mogrify -format bmp *.jpg


convert all PDF files in the directory to PNG (from Rob Felty's blog):
for file in *.pdf;\
do echo $file;convert -density 400x400 -quality 90\
$file `echo $file|cut -f1 -d'.'`.png;\
done

2 comments:

Ricardo Fabbri said...

I also like

mogrify -format png video.avi

as a quick way to extract all frames from a video into image files.

avocadohead said...

Thanks, I'll put in a reference to that ;-)