From: Peter Knaggs Peter.Knaggs@oracle.com
  To: members@penlug.org
  Subject: [PenLUG] squash that video, save your disk :)
  Date: Tue, 22 Jul 2003 15:31:35 -0700
  In the following sections, we'll look at a couple of the
  digital
  audiovisual formats we're likely to encounter when exploring
  the
  world of video on linux. This document focuses mainly on
  using
  transcode, although other encoders such as the MPlayer
  project's
  "mencoder" can also do a fine job. But we're jumping ahead of
  ourselves :)
  Let's start with some examples of the scenarios we're likely to
  encounter,
  and the tried-and-tested methods used to deal with them.
-------------------------------------------------------------------------------
One popular digital format for audiovisual data is the
  somewhat-compressed
  DV format. It's compressed just enough that it's fairly easy to
  read it into
  most computers, with ordinary IDE hard drives (as long as DMA is
  enabled)
  One common source of audiovisual streams in the DV format would
  be camcorders.
  Another source would be devices such as the "Dazzle Hollywood
  DV-Bridge", which
  (as explained by Rick Moen) can be used to create a DV stream
  from analog input
  such as a VCR, allowing VHS tapes to be converted to DV streams.
  The conversion
  is all done in hardware, no linux driver is needed. In any case,
  DV streams
  consume a lot of disk space, even though they are compressed
  enough to overcome
  the limitations of average IDE hard drives.
What if you would like to save some precious disk space, and
  compress those
  DV streams even further, into avi files containing the video as
  an xvid codec
  stream and the audio encoded as an MPEG audio layer 3 stream
  (more commonly
  known as mp3)? Such avi files can be played back very
  conveniently on both
  Windows and linux, although it seems that at the moment,
  standalone hardware
  players are scarce.
In any case, once you have a DV stream available,
  the first step is to read the DV stream into the computer
  using "dvgrab" (http://kino.schirmacher.de/).
  The second step is to compress the resulting file using
  "transcode".
For example, to compress the DV files read in from a "Sony
  TRV240 NTSC"
  camcorder to xvid and mp3, one could use the following
  script,
  courtesy of Florin Andrei (http://florin.myip.org/):
dvgrab --autosplit --format dv2 ./dv/${name}-
  transcode -i ./dv -w 1400,250,100 -b 128,0,0 -B 15,20,8 -V -j
  0,40,0,40 \
-I 1 -R 1 -x dv,avi -y xvidcvs,null -o /dev/null
  transcode -i ./dv -w 1400,250,100 -b 128,0,0 -B 15,20,8 -V -j
  0,40,0,40 \
-I 1 -R 2 -x dv,avi -y xvidcvs -o ./divx/${name}.avi
The transcode process is very CPU-intensive, and can be quite
  time-consuming,
  depending on what options you choose. You may prefer to run
  multiple transcode
  sessions on separate processors or separate machines to speed
  things up if you
  are compressing a large number of files.
Explanation of the transcode options used:
"-w 1400,250,100" : tells transcode to encode the video with a
  bit-rate
  of 1400kbps, 250 key-frames, crispness of 100.
"-b 128,0,0" : tells transcode to encode the audio with a
  constant
  bit-rate of 128kbps.
"-x dv,avi" : tells transcode which import modules for video
  and audio
  it should use.
"-V" : use YUV as the internal colorspace (much faster).
"-I 1" : enable de-interlacing mode. It may be faster to
  use
  "-J dilyuvmmx", which is a fast de-interlacing routine.
  If you're not in a hurry, or there is a lot of fast motion
  in the source material, "-I 3 -C 2" can give nice results.
"-y xvidcvs" : tells transcode which export module to use.
"-R 1" : tells transcode that this is the first encoding pass,
  and
  it should only look at the stream and determine the best
  bit-rate to use to encode it, writing the result in a file
  for the second encoding pass to make use of. The first pass
  doesn't actually do any encoding.
"-R 2" : tells transcode that this is the second pass. Make
  sure
  that it is able to open the file generated by the first
  encoding pass.
"-B 15,20,8" : tells transcode to resize to
  height-15*8 rows, width-20*8 columns.
"-j 0,40,0,40" : tells transcode to select the frame region
  by
  clipping border. In general, the format is:
  "-j top[,left[,bottom[,right]]]".
-------------------------------------------------------------------------------
Another popular digital audiovisual format is the digital
  versatile disc (DVD).
  How versatile it can be remains to be seen, but in any case
  occasionally the
  publishers of these discs write the files in plaintext.
  This makes it convenient to experiment with the data using the
  various
  transcode options, as well as the various video and audio codecs
  available.
  Examples of such work would include the movies "Revolution OS"
  and "Blink 182".
Assuming your linux kernel has support for the UDF
  file-system, which is the
  file-system which DVD files live in, then using a script such as
  the following
  would create avi files containing the video stream in the xvid
  codec, and
  the audio stream in the mp3 codec.
# For however many .VOB files exist in the VIDEO_TS directory
  of the DVD,
  # usually there would not be more than 10, as they are usually
  1GiB in size...
  for ((COUNT=1; COUNT < 10; COUNT++))
  do
  export IN=/mnt/dvd/VIDEO_TS/VTS_01_$COUNT.VOB
  export OUT=/mnt/local/work/VTS_01_$COUNT.avi
# Make a work directory for each of the transcode sessions to
  write
  # the first pass required bit-rate scan results file, and go
  there to work.
  # That way, if you decide to re-run the second encoding pass with
  different
  # options, the scan file is still available and hasn't been
  overwritten.
  mkdir /mnt/local/work/$COUNT
  cd /mnt/local/work/$COUNT
# This resize option is for 'full-screen' 4:3 aspect ratio video streams.
export RESIZE="-g 720x480 -Z 640x480"
  # This resize option is for 'wide-screen' aspect ratio video
  streams.
  #export RESIZE="-B 12,10,8"
# Low bit-rate, 650. Resulting file will be small, but video
  may be very grainy.
  #export BITRATE="-w 650,250,100"
  # High bit-rate, 2500. Resulting file will be large, but video
  will be clear.
  export BITRATE="-w 2500,250,100"
# A nice explanation of interlacing can be found at http://www.100fps.com/
  # De-interlacing is the attempt to remove those artifacts by
  interpolation.
  export DEINTERLACE="-I 3 -C 2"
# If audio stream is encoded in the ATSC A/52 codec (e.g.
  Dolby Digital AC3),
  # you may prefer to just pass it on through directly, leaving it
  unchanged,
  # as the open-source liba52.a library can decode ATSC A/52
  streams quite well.
  #export AC3PASSTHRU="-A -N 0x2000"
export AUDIO_CHANNEL="-a 0"
  export AUDIO_BITRATE="-b 128,0,0"
transcode -i $IN -M 2 $BITRATE $DEINTERLACE $AUDIO_CHANNEL
  $AUDIO_BITRATE
  $AC3PASSTHRU -V -f 25 $RESIZE -R 1 -x vob,null -o /dev/null -y
  xvidcvs
  transcode -i $IN -M 2 $BITRATE $DEINTERLACE $AUDIO_CHANNEL
  $AUDIO_BITRATE
  $AC3PASSTHRU -V -f 25 $RESIZE -R 2 -x vob -o $OUT -y xvidcvs
  done
------------------------------------------------------------------------------
Another scenario is, what if you simply need to extract the
  audio stream,
  to encode it as an mp3 file? Using a script such as the following
  should
  be sufficient, keeping in mind that any given VOB file might
  contain multiple
  audio streams, and selecting the right one using the "-a [0..7]"
  flags might
  involve a little trial and error.
for ((COUNT=1; COUNT < 10; COUNT++))
  do
  export IN=/mnt/dvd/VIDEO_TS/VTS_01_$COUNT.VOB
  export OUT=/mnt/local/work/VTS_01_$COUNT.mp3
  transcode -i $IN -a 0 -x null,ac3 -g 0x0 -s 4.47 -y raw -m $OUT
  -E 44100
  done
  If you prefer to extract the audio stream as a wav file instead
  of an mp3 file,
  perhaps so that you can more quickly make some modifications to
  it with it in
  your favorite editor (e.g. audacity, snd), use the following
  instead:
for ((COUNT=1; COUNT < 10; COUNT++))
  do
  export IN=/mnt/dvd/VIDEO_TS/VTS_01_$COUNT.VOB
  export OUT=/mnt/local/work/VTS_01_$COUNT.wav
  transcode -i $IN -a 0 -x null,ac3 -g 0x0 -s 4.47 -y wav -m $OUT
  -E 44100
  done
  Remember that some audio streams tend to have very low volume
  levels, so it
  might be necessary to use the "-s 4.47" flag to increase the
  volume by a
  factor of 4.47, for example.
-------------------------------------------------------------------------------
References:
  - Transcode, the Linux Video Stream Processing Tool (Dr. Thomas
  Ostreich):
  
  http://www.theorie.physik.uni-goettingen.de/~ostreich/transcode/
  - Transcode mailing list archives:
  
  http://www.theorie.physik.uni-goettingen.de/pipermail/transcode-users/
  - Transcode releases (now maintained by Tilmann Bitterberg):
  http://www.zebra.fh-weingarten.de/~transcode/
  - The xvid codec home-page:
  http://www.xvid.org/
  - The "kino" and "dvgrab" home-page:
  http://kino.schirmacher.de/
  - MPlayer "the movie player for linux" home-page:
  http://www.mplayerhq.hu
  - xine "a free video player" home-page:
  http://xinehq.de/
  - Audacity home-page:
  audacity.sourceforge.net/
  - The liba52 library - a free ATSC A/52 stream decoder
  implementation:
  http://liba52.sourceforge.net
  - Andreas Berntsen's explanation of how to convert from DV
  format:
  http://reefer.porrgrodan.com/convert.php
  - Interlacing explained:
  http://www.100fps.com/
  - Introduction to the mathematics of video compression:
  
  http://www.cs.sfu.ca/undergrad/CourseMaterials/CMPT479/material/notes/Chap4/Chap4.2/Chap4.2.html
  -------------------------------------------------------------------------------