December 2, 2009

Burning AVIs to DVDs under Linux

Converting AVIs to DVD-compatible MPEGs

ffmpeg -i foo.avi -target ntsc-dvd foo.mpg

I love ffmpeg, by the way.

Two-pass encoding

ffmpeg -i foo.avi -pass 1 -target ntsc-dvd -an /dev/null
ffmpeg -i foo.avi -pass 2 -target ntsc-dvd foo.mpg

-pass 1 writes out a “log file.” Two-pass encoding only applies for video, so you -an to ignore audio on the first pass. -pass 2 then reads the log file that -pass 1 created for it.


-threads 2. That was easy.

Aspect ratio

If your movie is in 4:3 or 16:9 (the only two aspect ratios that seem to be common—if not the only two allowed—for [NTSC?] DVD video) then ffmpeg seems to pick up the aspect ratio right. Sometimes you do need to hint it with -aspect 4:3 or -aspect 16:9 though? Play around. If the resulting MPEG file doesn’t look right in your media player, it probably won’t look right on your screen.

You can pass other aspect ratios to ffmpeg, like -aspect 2.33:1, but don’t expect anything to like putting that on a DVD, and don’t expect your (my) old Sony player to play it at all.

Now say you have a movie in 2.35:1, a 1280×544 image. I was hoping ffmpeg would do “magic” to turn it into letterbox anamorphic widescreen. It did not. Instead, I had to do it by hand.

NTSC (this is all NTSC by the way) DVD video is 720×480. Anamorphic PAR is 32:27, so using square pixels 16:9 video with 480 lines would be 854×480. (That’s actually a lie: it’s closer to 853×480; but 854 seems to be the accepted number.)

Now imagine fitting your 1280×544 image into an 854×480 box. Since 2.35:1 is wider than 16:9 (1.77:1) we’ll have to pad the top and bottom. How much? 544 * 854 / 1280 = 362.95, so lets say we’re aiming for a height of 363 pixels.

480 – 363 = 117 pixels of padding needed, that’s 58.5 lines of black bars each for the top and the bottom of the image. Of course, you can’t have a fractional line (pixel). What’s more, ffmpeg requires padding to be a multiple of two, so we’re going to say 480 – 362 = 118, 118 / 2 = 59, so we’ll put 58 lines of padding on the top and 60 lines of padding on the bottom.

This leaves us with the following command line:

ffmpeg -i foo.avi -target ntsc-dvd -s 720x362 -padtop 58 -padbottom 60 -aspect 16:9 foo.mpg

-s 720x362 says to make the input movie that size. Then the -padtop and -padbottom are added to that size: 362 + 58 + 60 = 480 (720×480).

If you’re curious how we got back to 720 from 854, remember that NTSC DVD video is always going to be 720×480; the DVD player stretches the picture to 854×480 for you. So while 720×362 screws up our aspect ratio, anamorphic widescreen will stretch it back out to make it right.

(There is an additional restriction on the size of the video, but I don’t recall what it is; when you run into this ffmpeg will tell you, and you’ll have to slightly shrink/expand the video and adjust padding accordingly.)

DVD authoring

mkdir dvd
dvdauthor --title -f foo.mpg -o dvd
dvdauthor -T -o dvd

Automatically making chapters

I used makexml for this. Should be something like:

makexml -chapters 5 foo.mpg -out foo  # creates foo.xml
dvdauthor -x foo.xml -o dvd
# I don't remember if this last step is necessary when using an XML
# file, actually:
dvdauthor -T -o dvd

This makes chapters every five minutes in the movie. When you see the format of the XML file you’ll actually be embarrassed; I think you could write a shell script to do this very easily (faster and with fewer dependencies than makexml).

Making an ISO


mkisofs -dvd-video -o dvd.iso dvd

(Is mkisofs now deprecated? I think it calls some other program on my machine.)


hdiutil makehybrid -udf -udf-volume-name <label> -o dvd.iso dvd

I think the -dvd-video switch might toggle something a little special in the ISO, but I don’t have any concrete information on that. If I’m right, though, I’m betting the OS X version doesn’t toggle that magic bit. So if you have problems with the hdiutil-generated ISO, try making the ISO with mkisofs instead.