How To: Transcode AVI Videos to Video DVDs in Linux

It’s easy enough to rip DVDs to .AVIs or .MKVs – you can just use nice, GUI-enabled tools like HandBrake. But what about when you want to transfer an AVI back to DVD format so it can be played on a standalone DVD player? Well, the Shetboy sent me his command-line technique a while back (which we can automate to some extent) – and I’ve got to say it works flawlessly.

1.) Get the Right Tools for the Job

If you’ve ever had a look at a video DVD on your PC, you’ll notice is has a bunch of .VOB files broken up into roughly 1GB sections which contain the pieces of your movie (if it’s a DVD of TV episodes you’re more likely to have a greater number of VOBs split up into one per episode on the disc). To deal with all the transcoding we’re going to do, we need the right tools. Some of these you’re likely to already have, others maybe not.

Package pre-requisites for transcoding:
mencoder, mplayer, libxvidcore, libxvidcore4, xvid4conf, xvidtune, ffmpeg, libavcodec0d, libmpeg*, w32codecs, transcode, mjpegtools, dvdauthor

You can install them all via the following command (where any you already have will just be skipped):

2.) Find Out Your .AVI’s Crop Area

To do a nice transcode, best results are gained by not transcoding any black bars in the avi. This step also helps in getting a perfect re-size every time without messing up the aspect ratio.

To do this run:

An mplayer window will open and the avi will play. Press the right arrow about 10 times and leave it running for a few seconds. The reason for this is that different resolutions can sometimes be used for the intro to TV shows, or company logos and things, and we want the main feature being played. After it has been showing the body of the show for a few seconds kill the mplayer window.

Output from the above command will be shown in your console window, and should look something like this:


mplayer -vf cropdetect Star.Wars.The.Clone.Wars.S01E01.HDTV.XviD-0TV.avi
MPlayer 2:0.99+1.0pre8-0ubuntu8.3 (C) 2000-2006 MPlayer Team

— A whole bunch of other stuff we don’t care about here —

Opening video filter: [cropdetect]
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg’s libavcodec codec family
Selected video codec: [ffodivx] vfm: ffmpeg (FFmpeg MPEG-4)
==========================================================================
alsa-init: using device default
alsa: 48000 Hz/2 channels/4 bpf/65536 bytes buffer/Signed 16 bit Little Endian
AO: [alsa] 48000Hz 2ch s16le (2 bytes per sample)
Starting playback…
VDec: vo config request – 624 x 352 (preferred colorspace: Planar YV12)
VDec: using Planar YV12 as output csp (no 0)
Movie-Aspect is 1.77:1 – prescaling to correct movie aspect.
VO: [xv] 624×352 => 624×352 Planar YV12
[CROP] Crop area: X: 623..0 Y: 351..0 (-vf crop=-608:-336:618:346).
[CROP] Crop area: X: 623..0 Y: 351..0 (-vf crop=-608:-336:618:346). *** This is the crop-area of the pre-show company logos etc. – don’t use it! ***
[CROP] Crop area: X: 623..0 Y: 351..0 (-vf crop=-608:-336:618:346).

[CROP] Crop area: X: 0..623 Y: 0..351 (-vf crop=624:352:0:0).7 0
[CROP] Crop area: X: 0..623 Y: 0..351 (-vf crop=624:352:0:0).7 0
[CROP] Crop area: X: 0..623 Y: 0..351 (-vf crop=624:352:0:0).7 0 *** This is the crop-area of the main presentation which we’re going to use! ***
[CROP] Crop area: X: 0..623 Y: 0..351 (-vf crop=624:352:0:0).7 0
[CROP] Crop area: X: 0..623 Y: 0..351 (-vf crop=624:352:0:0).7 0


The last bunch of lines showing the crop area for the main presentation are what’s important here. We’ll use this info shortly.

3.) Transcode the Video File

We’ll be doing the transcoding using mencoder, which takes an un-holy number of parameters to do a great job.

An example of a command in the proper format is …

To figure out what’s going on so you can tweak the numbers appropriately, I’ve separated out the switches into a table to best explain how it works:

Unholy Amount of Command Line Switches and Parameters Needed To Transcode .AVIs to DVDs
Switch Should I Tweak It? Explanation
mencoder Haha, No. Fire up mencoder to do our bidding
-oac lavc No. Output audio using libavcodec’s audio codecs
-ovc lavc No. Output video using libavcodec’s video codecs
-of mpeg No. Output our file in MPEG format – expect large file sizes!
-mpegopts No. Tell mencoder that from now on all switches are to control the MPEG output
format=dvd:tsaf No. Create our output with timestamps on every frame to keep everything in sync.
Note that we don’t put a dash before this option because it’s the first option after specifying -mpegopts
-srate 48000 No. Output our MPEG audio at 48Khz so it’s as high quality as we can make it
-ofps 25 Up to you. Output our MPEG video at 25fps for a PAL DVD. Change this to 23.97fps for a NTSC DVD.
-lavcopts No. Start our section where we specify libavcodec’s options
vcodec=mpeg2video No. Specify we want to create MPEG2 video. Note the lack of a prefix dash and the colon on the end to specify all further options will be specifying the details of our MPEG2 video output
vrc_buf_size=1835: No. Specify our buffer size. Note the colon at the end which is necessary to combine multiple parameters to our -lavcopts switch.
keyint=15: No. Specify our keyframe interval as 15 i.e. One frame in every 15 will be a keyframe which is encoded at high quality. Note the colon.
vrc_maxrate=9800: Yes. Specify the maximum data rate for our video. Note the colon.

It’s best to use a bitrate calculator to get this value as outlined in the row below on vbitrate

vbitrate=4900: Yes The average bitrate for our variable bitrate for the video. Note the colon.

If you’re planning to burn the video you’re transcoding to a DVD (i.e. a standard, single-layer DVD5) then it’s worthwhile using a bitrate calculator to get the best number to plug in here. The bitrate calculator will just ask you for the length of the video, the audio bitrate (as specified below), and it’ll give you values to plug in for the average and maximum (see above) video bit-rates.

aspect=16/9: Up to you. The aspect ratio of our output video. Options are 16/9 for widescreen and 4/3 for old-school anamorphic. Note the colon.
acodec=ac3: No. Set the audio codec to use to AC3 (also known as Dolby Digital). Note the colon.
abitrate=192 No. Set our auto bitrate to 192 kbit/sec. Note – No colon this time as we’re at the end of our -lavcopts section! Finally!
-af lavcresample=48000 No. Resample our audio to 48Khz for maximum quality
-vf crop=624:352:0:0, Yes! Set our crop ratio to be what we retrieved from our mplayer -vf cropdetect command [eg. 624:352:0:0]. Note the comma.
scale=720:576, Yes! Set our scaling: x-scale (horizontal) is set to 720 pixels, and the y-scale (vertical is set to 576 pixels. 720×576 is the resolution of a PAL DVD, pick.

This is the important part: We set the x-scale to be the full width of our screen, and then we use the following formula to generate what our y-scale should be: yscale = (y-resolution * aspect ratio * y-crop-area) / x-crop-area [So for this example it’s (576 * (16/9) * 352) / 624 = 578 ==> If more than 576, set it to 576! Or if it’s more than 480 set it to 480 for a NTSC DVD]. Note the comma!

expand=720:576,harddup Up to you. Set our video expansion to be the resolution we finally want to use: For PAL it’s 720:576, for NTSC you’ll want to use 720:480. This might be the same as the scale details above, but they don’t have to be. The harddup video filter is used to keep the audio and video in sync when changing frame rates. Note the comma between the expanded resolution and calling the harddup filter!
-frames 7500 Use only to test. OPTIONAL: Only encode first 5 minutes to test. 7500 = 60 seconds per minute * 5 minutes * 25 frames per second.
-o <output-MPEG-filename.mpg> Yes. Your MPEG output path and filename, in this example I’m using: ~/transcoded/ep01.mpg
<input-file-name> Yes. The avi we’re using for input, in this example I’m using: ./Star.Wars.The.Clone.Wars.S01E01.HDTV.XviD-0TV.avi

So, even though it looks like a bit of a nightmare, you only need to change a few things in the entire command:

1.) Choose your output fps by setting -ofps 25 for PAL or -ofps 23.95 for NTSC.
2.) Set whether we want to output in widescreen 16/9 or old-school 4/3 by setting -aspect=16/9 or -aspect=4/3
3.) Feed in the result of running our mplayer cropdetect so we end up with something like -vf crop 624:352:0:0,
4.) Feed in the result of running our scale calculation so we end up with something like scale=720:576,
5.) Give it an input source AVI and an output file to dump the transcoded MPEG to
6.) Optionally, only transcode a couple of minutes to make sure it’s all running alright. Once you’re happy with the transcode take out the -frames line and it’ll transcode the whole video file for you.

If you really want to dig into the switches more, you could always take a look at this page which shows how to use mencoder in significantly more detail.

4.) Generate the DVD

Now we’ve done the hard part and we’ve got a big ‘ol .mpg file of our avi, it’s time to generate the DVD structure. If you’re putting multiple mpegs to a DVD, keep the file limit to about 4.2GB otherwise it won’t fit on a single-sided DVD5. If you’ve got a DVD9 writer you might be alright up to about 8GB, but I don’t have one so I haven’t tested this.

Word of Warning: When you’re generating large MPEG files for DVDs do NOT work on a FAT32 file system – FAT32 has a 4GB file limit, and if you end up with a 4GB+ file, things are going to fail.

You need to run two commands to generate a DVD:

The first command instructs dvdauthor to make a title out of each file (think chapter so we can skip through them easily), and can take a while to run. The second command generates our table of contents and completes near instantly. Read more via the mighty man dvdauthor if you’re that way inclined…

So, an example of the command usage, assuming I’d created the dvdprep folder in my home folder, might be:

Important: In the above example ~/dvdprep MUST exist but the directory starwars1 MUST NOT!

With this done, the start of each new file can be jumped to within the DVD by hitting chapter next or previous on your DVD software or player. Before burning to disc you should check that it works ok by checking with a video player like VLC and select ‘Play DVD from Directory’ or similar.

Workaround for ERR: no video format specified for VMGM error: If you get the error just mentioned, then it’s an issue with the DVDauthor v0.7.0 package. To fix things, run the command export VIDEO_FORMAT=PAL or export VIDEO_FORMAT=NTSC then run the second line (ending with -T) again and you should be fine.

5.) Burn the DVD!

That’s it! We’re done! Burn the DVD directory (i.e. the contents of ~/dvdprop/starwars1 now containing the AUDIO_TS and VIDEO_TS folders in his example) using Brasero, K3b or your burning software o’ choice. Whatever you use, make sure you burn the disc as a New Video DVD though!!

Happy transcoding!


An extra-special shout-out to the Shetboy for sending me this how-to ages back – apols. for not getting around to posting it sooner! I just knew it’d be a bugger to nail perfectly (i.e. unholy table of doom!) so I was kind putting it off =P

3 thoughts on “How To: Transcode AVI Videos to Video DVDs in Linux”

  1. Nice write up!
    Clear, concise and explained very well.
    I like the way you suggest to use a GUI for the burning of the DVD too, since doing it by command line can be unpredictable at times.

    Just one point to add, I never created the DVD image files on FAT32 either. It looked like all the files were named uppercase but after burning the disk had mixed case filenames and never worked in a standalone player. Maybe it is something to do with my setup and config? But anyway, something for your readers to be wary of.

    1. Cheers, man – thanks for the guide in the first place, that’s some quality work!

      I got a 1.5TB HD the other day and it was formatted FAT32 – went to copy a DVD ISO to it…. Denied! 4GB file limit. An I didn’t realise it was FAT32 before I copied across a couple of hundred GB of stuff, so now I’ll probably have to resize the partition, make a NTFS one (for Win/Lin compatibility), copy from one to the other, resize FAT32 down and then NTFS up, wash/rinse/repeat to get rid of that poxy FAT32 partitioning…. Grrr!

      Point well taken about the burning, at least you can see what options/compatibility modes are available from a GUI, and it’ll pick some sensible defaults for you, so yeah, Brasaro ftw!

Leave a Reply

Your email address will not be published.