Linux Video Stream Processing Tool - Examples
Keywords: DVD, digital video, DV, encoder, divx, DivX;-), lame, source, posix, avifile, opendivx, codec, linux, AC3, program stream, video, audio, transcode, decoder, stream, YV12

DVD to MPEG-4
This chapter gives an introduction to DVD to DivX compression. There are actually only 2 external packages required to make it work with transcode. The first is libdvdcss (recommended version is 0.0.3 - not 1.0.0!) required to overcome the context scrambling system (CSS) and the second is a modern MPEG-4 codec. See the modules section to find out which MPEG-4 codecs are currently supported and where to obtain the sources/binaries. Unless you enjoy command line utilities, please take a look at the excellent dvd::rip, which is a full featured DVD Ripper GUI for Linux, written in Perl, by Joern Reder. DVD (PAL) aspect ratio 4:3 aspect ratio 16:9 DVD (NTSC) aspect ratio 4:3 aspect ratio 16:9 multiple program stream VOB chunks encoding audio to MP3 AC3 audio pass-through LPCM audio pass-through MP2 to MP3 audio re-encoding making a main title DVD backup with tccat

PAL DVD ---> DivX 4.0 / DivX ;-)
- Here is a 3 step guide to convert a PAL DVD to an AVI DivX movie with MP3 (default) sound under linux using transcode: (I) Put the DVD in the drive: We assume, that /dev/dvd is a link to the actual device. The disk may be encrypted and we need libdvdcss as a shared library in the default library search path for this. We first do some preprocessing with tcscan to find out the best encoding bitrate and frame parameter to make the main DVD title fit on 1 or 2 CDs with renormalized sound. This is done by invoking the helper programs tccat, tcextract, tcdecode and tcscan: bitrate: tccat -t dvd -T 1,-1 -i /dev/dvd | tcextract -x ac3 -t vob | tcdecode -x ac3 | tcscan -x pcm We basically pipe all chapters (use "-1" for the chapter argument of option "-T") of the main title (this number may be different on your DVD) through tcextract, where we have to supply the filetype option "-t vob". The extracted AC3 stream is decoded by tcdecode and analyzed by tcscan. The output may look like: `[tcscan] audio frames=167838.40, estimated clip length=6713.54 seconds [tcscan] (min/max) amplitude=(-0.210/0.224), suggested volume [tcscan] rescale=4.470 [tcscan] length: 167838 frames [tcscan] runtime: 6713 sec @ 25.000 fps [tcscan] MP3 bitrate: 128 kbps [tcscan] audio: 104.89 MB [tcscan] disk size: 650 MB | video 545.11 MB | encoder bitrate 681.17 kbps [tcscan] disk size: 700 MB | video 595.11 MB | encoder bitrate 743.65 kbps [tcscan] disk size: 1300 MB | video 1195.11 MB | encoder bitrate 1493.42 kbps [tcscan] disk size: 1400 MB | video 1295.11 MB | encoder bitrate 1618.38 kbps` and is a simple estimate for the encoder bitrate, using the default values for MP3 sound encoded at 128 kbps. Invoke tcscan -h to learn about more options. The main title runtime is about 1h:52m and a high quality encoded movie deserves a 2 CD burn. As you can see, the unrealistic high bitrate values for the old Win32 dll codecs are no longer valid. frame parameter and aspect ratio: tccat -t dvd -T 1,-1 -i /dev/dvd | tcscan -x vob and press "^C" after the MPEG frame parameter have been displayed. In this case, the output may look like `found MPEG-2 video stream [0xe0] sequence: 720x576 4:3, 25 fps, ...` which means, that we need to rescale the frame to obtain the proper aspect ratio "4:3". Another common aspect ratio is "16:9". We show below suggested scaling parameter and two transcode processing modes for both cases. (II) The actual encoding session (parameter grouped by import/processing/export): Example: DVD title / aspect ratio "4:3" / letterbox format
  transcode | -i /dev/dvd/ -x dvd -T 1,-1 -V
  | -B 1,0 -Y 76,8 -s 4.47
|  | -t 83920,alien -y divx4 -w 1618

`[transcode] video: import frame | 720 x 576 1.25:1
[transcode] video: new aspect ratio | 720 x 544 1.32:1 (-B)
[transcode] video: clip frame (->) | 704 x 392`

- We use slightly more than 1/2 of the total number of frames "-t" or simply 1 huge file "-o alien.avi" to be split in the last step.
- We clip off 76 lines at the top and bottom and 8 rows on both sides of the movie "-Y 76,8" before encoding to get rid of the black bars. This is only recommended for letterbox format.
- In order to use the fast resizing of transcode with option "-B", the height and width must be a multiple of 32. Clipping 8 columns on both sides will do, in most cases they are black anyway. We will get a 704x392 frame with an aspect ratio of 1.32:1, which is acceptable.
- Optional: We might try to go for better encoding quality. In this case, it is recommended to use the multi-pass encoding feature found in DivX 4.0 with option "-R 1". However, you need a second run with identical transcode options, except "-R 2", instead of "-R 1". The first run produces a logfile, which is analyzed to estimate optimal encoding parameter and to achieve an average encoding bitrate, that is close to the supplied value of "-w" and will fit the result onto 2 CD's.
- Optional: Use the high-quality zoom option "-Z 720x544" instead of "-B 1,0".
- The option "-V" consumes less CPU/PCI bandwidth and gives a big performance increase.

Example: DVD chapter mode / aspect ratio "16:9" / letterbox format

transcode | -i /dev/dvd/ -x dvd -V
| -j 16,0 -B 5,0 -Y 40,8 -s 4.47
|  | -U alien -y divx4 -w 1618

`[transcode] video: import frame | 720 x 576 1.25:1
[transcode] video: clip frame (<-) | 720 x 544
[transcode] video: new aspect ratio | 720 x 384 1.77:1 (-B)
[transcode] video: clip frame (->) | 704 x 304
`

- We clip off 16 lines at the top and the bottom of the movie to use the fast resizing of transcode to an almost ideal aspect ratio of 1.77:1. Clipping 8 columns on both sides and 40 rows at the top and the bottom removes the remaining black bars prior to encoding.
- In the chapter mode "-U", the output is split into separate files labeled alien-ch00.avi, alien-ch01.avi, ... which contain the DVD chapters, that are known from the DVD player menu.

Example: DVD chapter #25 / aspect ratio "16:9"

transcode | -i /dev/dvd/ -x dvd -T 1,25 -V
| -j 0,8 -B 6,1 -Y 40,8 -s 4.47
|  | -o alien-ch25.avi -y divx4 -w 1618

`[transcode] video: import frame | 720 x 576 1.25:1
[transcode] video: clip frame (<-) | 704 x 576
[transcode] video: new aspect ratio | 672 x 384 1.79:1 (-B)
`

- We clip off 8 columns on both sides of the movie to use the fast resizing of transcode to an almost ideal aspect ratio of 1.79:1. No further clipping necessary for the final frame size of 672x384.
- The encoding stops after the selected chapter 25 is done.

- (III) It's almost done:
  The avisplit utility allows to split the AVI file(s) produced by transcode. The utility may not work for other AVI files. If you have one big file, use
  avisplit -i alien.avi -s 700
  to break a huge file (AVI files have a 2 GB limit) into the pieces alien.avi-0 and alien.avi-1 with no more than 700 MB each.

  For the second example above, we now have quite a lot of files, each containing a chapter of the movie. Distribute the files, preserving the order, onto one ore more subdirectories ./CD-N, N=1,2,..., each containing no more than 650 or 700 MB, depending on your choice of CD. Now invoke avimerge for each subdirectory
  avimerge -o movie-disk-N.avi -i ./CD_N/*.avi
  This will glue all the small files to one big AVI file named movie-disk-N.avi, where N is the number of the CD. avimerge supports wildcards and is very careful in not trashing your existing AVI files. Try to play the big files with mplayer and seek around. The audio/video synchronization should be acceptable. If everything seems fine, go ahead and burn your CDs.

  NTSC DVD ---> DivX
  - Here is a 3 step guide to convert a NTSC DVD to an AVI DivX movie with MP3 (default) sound under linux using transcode: (I) Put the DVD in the drive: Follow step (I) for the PAL DVD, with option "-f 23.976024" for tcscan. (II) The actual encoding session (parameter grouped by import/processing/export) : Example: DVD title / aspect ratio "4:3" / letterbox format
    transcode | -i /dev/dvd/ -x dvd -T 1,-1 -g 720x480 -M 2 -V
    | -X 2,0 -Y 80,8 -s 4.47
  |  | -t 83920,alien -y divx4 -w 1618 -f 23.976024

  `[transcode] video: import frame | 720 x 480 1.50:1
  [transcode] video: new aspect ratio | 720 x 544 1.32:1 (-X)
  [transcode] video: clip frame (->) | 704 x 384`

  - We use slightly more than 1/2 of the total number of frames "-t" or simply 1 huge file to be split in the last step.
  - We clip off 80 lines at the top and bottom and 8 rows on both sides of the movie "-Y 80,8" before encoding to get rid of the black bars. This is only recommended for letterbox format.
  - In order to use the fast resizing of transcode with option "-X", the height and width must be a multiple of 32. Clipping 8 columns on both sides will do, in most cases they are black anyway. We will get a 704x384 frame with an aspect ratio of 1.32:1, which is acceptable.
  - We must provide the frame parameter "-g 720x480", the frame rate "-f 23.976024" and the demuxer option "-M 2" to obtain a clean stream at a constant frame rate.

  Example: DVD chapter mode / aspect ratio "16:9" / letterbox format

  transcode | -i /dev/dvd/ -x dvd -g 720x480 -M 2 -V
  | -j 16,0 -B 2,0 -Y 32,8 -s 4.47
  |  | -U alien -y divx4 -w 1618 -f 23.976024

  `[transcode] video: import frame | 720 x 480 1.50:1
  [transcode] video: clip frame (<-) | 720 x 448
  [transcode] video: new aspect ratio | 720 x 384 1.75:1 (-B)
  [transcode] video: clip frame (->) | 704 x 320
  `

  - We clip off 16 lines at the top and the bottom of the movie to use the fast resizing of transcode to an almost ideal aspect ratio of 1.77:1. Clipping 8 columns on both sides and 32 rows at the top and the bottom removes the remaining black bars prior to encoding.
  - In the chapter mode "-U", the output is split into separate files labeled alien-ch00.avi, alien-ch01.avi, ... which contain the DVD chapters, that are known from the DVD player menu.

  Example: DVD chapter #10 / aspect ratio "16:9" / viewing angle #2

  transcode | -i /dev/dvd/ -x dvd -T 1,10,2 -g 720x480 -M 2 -V
  | -j 0,8 -B 3,1 -s 4.47
  |  | -o alien-ch10-2.avi -y divx4 -w 1618 -f 23.976024

  `[transcode] video: import frame | 720 x 480 1.50:1
  [transcode] video: clip frame (<-) | 704 x 480
  [transcode] video: new aspect ratio | 672 x 384 1.79:1 (-B)
  `

  - We clip off 8 columns on both sides of the movie to use the fast resizing of transcode to an almost ideal aspect ratio of 1.79:1. No further clipping necessary for the final frame size of 672x384.
  - The encoding stops after the selected chapter 10 is done. Here, we choose a second camera angle, if the DVD has this feature.

- (III) It's almost done:
  Follow step (III) of the PAL DVD example.

  Multiple MPEG program stream chunks (VOB) ---> DivX
  - Here is a 3 step guide to rip a DVD and convert the multiple VOB chunks, or only a single file, using transcode: We rip the DVD and put all VOB chunks, that make up the actual movie into a subdirectory, denoted here as "my_movie/". For an encrypted DVD, we will need libdvdcss under linux to do this, but this may not be legal. If you do not need DVD navigation or multiple angle features, the ripping is done by: tccat -i /dev/dvd -T 1,-1 | split -b 1024m - my_movie/movie- I assume, "/dev/dvd" is a link to the DVD device and the main title is 1. The DVD does not have to be mounted, just put it in the drive. After some time, we chunks named: movie-aa, movie-ab, ..., which are, but the last, exactly 1GB. The following is also valid, if we have used other programs to copy the DVD title VOBs onto the harddisk. It's a good idea, to let tcprobe take a look at the files you want to encode. This is done by typing: tcprobe -i my_movie/ assuming that "my_movie/" is a directory in your present work directory. The output may look like `[tcprobe] MPEG program stream [tcprobe] summary for my_movie/, (*) = not default, 0 = not detected import frame size: -g 720x480 [720x576] (*) aspect ratio: 16:9 (*) frame rate: -f 23.976 [25.000] frc=1 (*) audio track: -a 0 [0] -e 48000,16,2 [48000,16,2] -n 0x2000 [0x2000] detected (6) subtitle(s)` As you can see, the auto-probing feature has detected a NTSC program stream with a single AC3 audio track. In the following, we only need to take care of the aspect ratio 16:9, but that has already been explained in the previous section. Now we can invoke transcode with the directory mode, that internally concatenates all VOB chunks. If you need more bitrate or audio renormalization information, check the DVD section. The default is for MP3 audio encoding: ### AC3->MP3
    transcode | -i my_movie/ -V
    | -j 0,8 -B 3,1
  |  | -t 10000,movie -y divx4

  To enable AC3 pass-through, use "-A" and use "-N 0x2000" to set the proper codec in the AVI file and the player. The default audio track is 0, which is in most cases the original language. If you want to have AC3 sound in your AVI-file, use:

  ### AC3->AC3

  transcode | -i my_movie/ -V -A
  | -j 0,8 -B 3,1
  |  | -t 10000,movie -y divx4 -N 0x2000

  If you own a DVD with uncompressed PCM audio, e.g., audio track 1, and want to keep the quality, i.e., pass-through the sound, use:

  ### PCM->PCM

  transcode | -i my_movie/ -V -a 1
  | -j 0,8 -B 3,1
  |  | -t 10000,movie -y divx4 -N 0x1

  If your DVD has MPEG audio, it's usually MP2 format. This is automatically detected and recompression to MP3 audio is performed with the default settings. However, if you need to resample your sound, here is an example, using audio track 2:

  ### MP2->MP3

  transcode | -i my_movie/ -V -a 2
  | -j 0,8 -B 3,1 -E 44100
  |  | -t 10000,movie -y divx4

  The "-t" option splits the output into separate files labeled movie000.avi, movie001.avi,..., with exactly 10000 frames per file. On my CII 533@800 MHz, I get around 11 fps for the encoding, using the "-V" mode, which saves alot of bandwidth. This will take some hours, but since we work with linux, we don't need to care, just wait.

  - If you have to interrupt your session for some reason, you can always restart with the help of the "-c" option. You only need to count the valid files, i.e., with 10000 frames, already encoded to find out the first parameter for this option. Drop the last AVI file for it may be broken, but this is not the case if you use ^C. Also use some other basename for the "-t", like "-t 10000,movie1" to make sure, not to overwrite your old files. Ok, the braindead seeking implementation takes some time, but this is rarely used anyway.

- Take a look at the DVD section to learn how to avimerge the AVI-files. After merging, try to play the big files with mplayer and seek around. The audio/video synchronization should be acceptable. If everything seems fine, go ahead and burn your CDs. Note, that AC3 AVI files tend to be up to 25% larger compared to MP3 audio, which is the default for transcode.

  making a main title DVD backup with tccat
  - Here is a short guide for making a quick "backup" of your shiny new multi-angle DVD. NOTE: Please make sure, the DVD explicitly allows you to make a backup copy. (0) insert your DVD;-) (1) switch to a new empty directory $MY_PATH/DVD/ on your hard drive. (2) if /dev/dvd is a link to your block device, use tccat's new -P option
    tccat -i /dev/dvd -P 1 | split -b 1024m - VTS_01_
  - This will result in a couple of files named VTS_01_aa, VTS_01_ab, ... (once LFS is fully working, a single file should be sufficient (???), i.e., "tccat -i /dev/dvd -P 1 > VTS_01_1.VOB"). (3) copy the IFO files by hand. For this, you will need to mount the DVD:
    mount /dev/dvd /mnt/dvd
    cp /mnt/dvd/VIDEO_TS/VIDEO_TS.IFO .
    You also need the title IFO file. In this case cp /mnt/dvd/VIDEO_TS/VTS_01_0.IFO .
    umount /mnt/dvd
  - (4) rename the chunks, first file is VTS_01_1.VOB, not VTS_01_0.VOB. The latter contains still pictures and the like (?).
    mv VTS_01_aa VTS_01_1.VOB
    mv VTS_01_ab VTS_01_2.VOB
    ...
  - (5) repeat step (2-4) for other titles, you wish to keep. In most cases, the main title will do. Note that the Example after you're done: $>ls -l . total 5529180 -r--r--r-- 1 bgates users 26624 Jan 13 10:29 VIDEO_TS.IFO -r--r--r-- 1 bgates users 124928 Jan 13 10:29 VTS_01_0.IFO -rw-r--r-- 1 bgates users 1073741824 Jan 13 18:05 VTS_01_1.VOB -rw-r--r-- 1 bgates users 1073741824 Jan 13 18:06 VTS_01_2.VOB -rw-r--r-- 1 bgates users 1073741824 Jan 13 18:08 VTS_01_3.VOB -rw-r--r-- 1 bgates users 1073741824 Jan 13 18:10 VTS_01_4.VOB -rw-r--r-- 1 bgates users 1073741824 Jan 13 18:12 VTS_01_5.VOB -rw-r--r-- 1 bgates users 287461376 Jan 13 18:12 VTS_01_6.VOB (6) test your copy, you need to provide an absolute path to the backup directory:
    tcprobe -i $MY_PATH/DVD -T 1
    tccat -i $MY_PATH/DVD -T 1,C,A | [mplayer | xine | ... ] -
  and optionally check other titles, chapters (C) or even viewing angles (A). (7) Please note this backup copy is suitable for navigation with import module "-x dvd". If you prefer the cluster mode, you need to apply my cluster mini guide to this copy of your DVD or the DVD in your drive. The program stream contains full navigation and angle information. These information are not preserved using the -T option with tccat. ----- Hint: It does not make sense to apply the procedure to all titles found with tcprobe, since a lot of titles share VTS* files and IFO files.

  Last modified: Thu May 16 12:56:51 CEST 2002