Thursday, September 25, 2008

Command Line DVD Authoring: Part 3.1

This is part of a multi-part series on creating DVDs manually from the command line. It is not expected that regular users will generally be performing video editing or DVD authoring from the command line. Rather, this guide is intended for programmers who may be wishing to build a front-end for DVD authoring, and don't want to sift through miles of documentaion just to get the basics. This guide makes use of command-line utilities already freely available, but is not meant to be a complete set of documentation for any of these utilities. Instead, consider it a primer. The parts in this series are:

Part 1: Editing a Video File with MPlayer
Part 2: Converting a Video to DVD Format
Part 3: Making a DVD Menu
Part 3.1: Extracting Audio From A Video
Part 4: Building a DVD .iso File

It should be noted that while the programs themselves should remain relatively the same between Linux distros, the name of the packages themselves are likely to change. This tutorial was written using Ubuntu 8.04 as the reference OS, so if you use a different distro, your mileage may vary.


Part 3.1: Extracting Audio From A Video

I have been burning off a bunch of TV shows onto DVD, for personal use. These are largely shows that I do not expect to be released commercially on DVD. When a show I do like is released on DVD, I prefer buying a nice, clean, professional copy to just watching my homemade copy with TV logos all over it. But when I do put together my own, sometimes I like to use the theme song for the menu music. I've also seen commercially-produced DVDs that just use audio segments from the shows for the menu audio. It's easy to extract the audio, and in fact, you already know how to do most of it.

First, you need to block out the section of audio that will be used. This is as simple as an edl file. Use MPlayer to find the section that you're looking for, use 'i' to mark the beginning, find the end, use 'i' to mark off the end. Fine tune it the same way I showed you in Part 1, and when you're ready, use MEncoder to cut out that single piece of video. The command line will look something like this:

mencoder myoriginalvideo.mpg -of mpeg -oac copy -ovc copy
-o mycutvideo.mpg -edl myedlfile.edl

Part 2 explained what these specific command line options do, so by now you know that you're basically creating a video that only contains the clip that you want to extract the audio from. Since you're just making a frame-by-frame copy, both of the audio and the video, no re-encoding needs to happy, and that means no loss of quality.

For the next step, you need to install a lovely little package called transcode, which includes a utility called tcextract. This program gives us the ability to extract either audio or video from a stream, and save it in whatever format you like. The default is to save it in whatever format it detects inside the stream, but if it's having problems detecting it, you may need to specify it. First, let's take a look at a sample command line for extracting audio:

/usr/bin/tcextract -i myshow.mpg -a 0 -x mp3 2> /dev/null > myshowmusic.mp3

The '-i' option specifies the input file. The -a option specifies which audio or video track to rip (there will likely be only one, which would be track 0) and -x specifies the output format (default is whatever the original was encoded in). Because people are used to mp3 files, I just went with that. If you're *nix-savvy, you already know that '2> /dev/null' is throwing away STDERR messages, and '> myshowmusic.mp3' is dumping the STDOUT to a file. The tcextract program doesn't have an output file option, it just sends it through STDOUT, which is useful for all sorts of piping operations.

You might also be interested in the command line to extract just the video. This is because some DVRs (including MythTV) may store video in standard MPEG2 files, but they also add little index markers to help with playback. This results in a non-standard file which isn't going to play well with some players. Splitting the audio and video and then recombining them is an effective way to drop these index markers. The video command might look like this:

/usr/bin/tcextract -i myshow.mpg -x mpeg2 2> /dev/null > myshowvideoonly.mpeg2

The command line is pretty close to the audio one, it just uses a different codec. When you're finished extracting both the audio and the video, you can use the mplex utility (provided by the mjpegtools package) to combine them back together. The command line will look something like this:

/usr/bin/mplex -O -200 -f 8 -M -o myshow.mpg myshowvideo.mpeg2 myshowaudio.mp3

The '-O' option is important in our case, because sometimes splitting audio and video causes them to get out of sync with each other. The '-200' is actually an argument to '-O' telling it to start the video negative 200 milliseconds before the audio (so basically, 200ms after). The '-f 8' tells mplex to use a very minimal DVD format (check the man page for other formats available). The '-M' switch, yeah, I'm not totally keen on what it does. According to the man page, 'This flag makes mplex ignore sequence end markers embedded in the first video stream instead of switching to a new output file. This is sometimes useful splitting a long stream in files based on a -S limit that doesn’t need a run-in/run-out like (S)VCD.' I hope you know what that means, because I got lost halfway in. Last up, -o specifies the output file, and the next two arguments are the input files (video and audio) that will be stuck back together.

For those of you that are interested, the command line switches that I used were swiped from the source code of a program called tivo2dvd. If you're up to speed on your Perl, you might want to check out the source. In fact, when it comes down to it, most of this series of articles was derived from reading the source code of this package. It's amazing what you can discover by reading a little source code, isn't it? My articles go into detail that source code doesn't, but let's make sure to give credit where credit is due.

No comments:

Post a Comment

Comments for posts over 14 days are moderated

Note: Only a member of this blog may post a comment.