Mar 9 2010

My favorite bash idiom

Stuart and I came up with a refinement to my favorite bash idiom today. It was:

find ... | while read FOO; do ...; done

as demonstrated in:

find . -name '*.mp3' -type f | while read MP3; do
    ffmpeg -i $MP3 ${MP3%.mp3}.m4a
done

This handles whitespace (except newlines) in filenames. Except that ffmpeg is sharing stdin with while, which can cause some very funky problems. This is safer:

find ... | while read FOO; do echo | (
    ...
); done

and while you’re at it, you might as well take into account the unlikely-but-possible corner case of a filename containing a newline:

find ... -print0 | while read -d $'\0' FOO; do echo | (
    ...
); done

Apr 20 2008

64-bit Transcoding

I have a 64-bit desktop machine, that has rarely been run as a 64-bit machine. The hassle was too great and I couldn’t really see a reason to put up with it.

I think that 64-bit support has come a long way in the meantime, and it may be time to try it out. It sounds like a livable situation. So with the pending release of the next Ubuntu version I’m thinking of wiping and going 64-bit.

One of the primary motivators is that 64-bit holds some promise for transcoding video, and now that I have an HDHomeRun to capture over-the-air HDTV signals, I will be doing quite a bit of video transcoding for MythTV (to save disk space—a full-quality HDTV program is about 9 gigabytes per hour).

But before taking the plunge, I thought I’d do an empirical test and see if there would be any real savings. I captured a couple of minutes of HD content from PBS, then transcoded 60 seconds using ffmpeg and mencoder. Then I did the same with the Ubuntu 64-bit live CD. The 64-bit execution difference was statistically significant.

ffmpeg was about 1.12 times as fast—a savings of about 10 seconds per minute, or 10 minutes per hour.

mencoder was about 1.08 times as fast—similar savings.

I didn’t test mythtranscode itself, since getting it installed in a live CD environment would be too much work. I also must point out some other possible confounding variables. I used the Ubuntu 7.10 versions of ffmpeg and mencoder in 32-bit, and the Ubuntu 8.04 versions in 64-bit. Did both projects improve their code to be about 10% faster in the meantime? Unlikely, but perhaps not unfathomable.

So will I make the switch? I don’t know yet. 10% faster is significant, but not obviously worth it. I’ll have to think about it.

For the curious, here’s my numbers. I did at least two runs of each to check for agreement, and what you see is the average. Of course, these would not be the settings you’d necessarily use to transcode—ffmpeg has a pretty low default bitrate for example—but I think we can agree the speedup is likely to be in the same ballpark no matter what settings you’re using.

# 64-bit    32-bit
# 86 s      95 s
time ffmpeg -y -t 60 -i foo.avi -acodec copy bar.avi
# 55 s      64 s
time ffmpeg -y -t 60 -i foo.avi -acodec copy -s 640x480 bar.avi
# 83 s      90 s
time mencoder foo.avi -oac copy -ovc lavc -frames $[30*60] -o baz.avi

Dec 24 2006

Authoring DVDs

Scenario: you have a bunch of avi files (or whatever) that you want to make into a DVD. They may be episodes of your favorite comedy that you recorded with MythTV and want to keep around but not on your hard disk. They may be home videos. Whatever.

There are commercial programs to do this, and they are either expensive or inflexible and for any of a million reasons they don’t meet your need. What do you do? There’s a lot of good information about the details out there, with the exception of how to transcode your video, so I will give the overview, some links, and details on how to transcode.

First, what a DVD is. It’s an ISO9660 filesystem following a certain convention. That convention is that stuff has certain names in a certain filesystem layout, and that stuff is encoded in certain ways. But it’s just a regular filesystem underneath. If you look at a DVD in the shell you can see what I mean.

Assuming you want to play video DVD on your Wal-Mart-purchased consumer electronics, you need to get your video into an MPEG-2 video with AC3 or PCM audio at 48khz, with the appropriate frame rate for your TV and the correct size and aspect ratio. The mencoder docs have a nifty table showing what you need to decide. Once you’ve decided, use ffmpeg to transcode. It’s not hard: ffmpeg -i in.avi -target ntsc-dvd -acodec ac3 out.mpg. You can change the bitrate with the -b option (see the myriad bitrate calculators online if you’re trying to squeeze things. I’ve found the bitrate calculator in ffmpegX to be the best to use, if you have OS X). You can change the audio bitrate, e.g. -ab 192. You can change the size from “full DVD” to “half DVD” with -s 352x480. You could play with lots of options. What’s important is that you see something like this:

Output #0, dvd, to 'out.mpg':
Stream #0.0: Video: mpeg2video, yuv420p, 352x480, q=2-31, 1703 kb/s, 29.97 fps(c)
Stream #0.1: Audio: ac3, 48000 Hz, stereo, 256 kb/s

If it doesn’t look right, it’s time to double check the options and the order of the options. That one bit me, but it’s right there in the manpage:

ffmpeg [[infile options][-i infile]]... {[outfile options] outfile}...

If it looks right, but mplayer doesn’t seem to grok the output, then get a new ffmpeg. I had one (ffmpeg from darwinports) that would seem to do everything right but somehow leave out or corrupt the audio, so my transcoded videos had no audio. So I compiled my own from SVN.

Once you have transcoded the videos, it’s time to use dvdauthor to make menus and create the DVD filesystem. It’ll take a little work and XMLing, but it’s well-documented online. Then you need to burn your image to a DVD.

But before you burn, try it out in-place with your favorite DVD watching program. Pointing it at the VIDEO_TS directory or perhaps VIDEO_TS/.. should do the trick. If it loads and plays, you’re probably good to go. Burn and test. Get out that sharpie and decorate. Enjoy your newfound sense of accomplishment!