Capturing to SVCD tutorial, by Jens

1.Capturing

Preamble 

I bought a Pinnacle PCTV pro which should be able to capture a software encoded MPEG2-stream in DVD quality. There is a audio cable from the TV card into the soundcard - external or internal.

To capture under Linux i found Roman Hochleitner's NuppelVideo really useful. The important thing on it is the output of copied frames. A copied frame is generated from the previous frame, if the actual frame (from video) is lost. This is needed to keep audio in sync. On the end of the capturing process you will see, what you got. If you have zero, one or two copied frames, you have a excellent source for the next steps. If you have many copied frames (often many of them come together), you have problems with your hardware or worked too hard on your OS while capturing ;-)

To use this feature you need to apply two extra options to the bttv module in the kernel. You can do that through the file /etc/modules.conf

    Code listing .1

    
    ###
    ### Video
    ###
    options bttv fieldnr=1 gbuffers=2
    alias char-major-81 bttv
    

The record program nuvrec will generate one or more output files, each of 2 GB size. Cause i find this not very handy, i've written this patch. With argument "-2" you will get the original behaviour, without this argument the output will be one big file.

nuvrec will write out a RTJPEG compressed audio/video stream - it's the smaller brother of the MJPEG format. You have to choose a channel with some other program (i.e. with xawtv), end this program and start nuvrec. The quality over the coax cable is not really good. So i use the composite video input on my TV card and the line in on my ;Ensoniq ES1371 sound card. To get the same audio profile each time, i have created a special file for that.

Capturing sequence  

Depending on your sound system, you may want to comment out the OSS/kernel part and uncomment the Alsa one.

    Code listing .2

    # save current audio profile
    #/usr/sbin/alsactl -f /tmp/audio_profile store  # for ALSA sound
    /usr/bin/aumix     -f /tmp/audio_profile -S     # for kernel sound driver
    # load video capture audio profile
    #
    #/usr/sbin/alsactl -f /etc/asound.composite_video restore       # ALSA
    /usr/bin/aumix     -f /etc/aumix.composite_video -L >/dev/null  
    # kernel
    # capture
    nuvrec -H 576 -W 720 -a 32768 -N -S 1 video.nuv
    # -H 576 -W 720 for DVD quality - use good values here, your video will never be
    #               better than the source
    # -a 32768      for 50% audio volume - there are 2 stages:
    #                1. this volume level goes out to LINE OUT over the internal/external
    #                   audio cable to the soundcard
    #                2. the capture volume in the soundcard is set by our audio profile
    #                   above
    #               cause i have a direct cable from VHS to my audio card, this value
    #               has no effect for me
    # -N            no (lzo) compression
    # -S 1          Video source: 0 coax cable
    #                             1 composite video
    # restore the audio profile
    #
    #/usr/sbin/alsactl -f /tmp/audio_profile restore       
    # ALSA
    /usr/bin/aumix     -f /tmp/audio_profile -L >/dev/null  
    # kernel
    # clean up
    rm -f /tmp/audio_profile
    

Capturing sequence with ffv1rec  

Alternatively, if you have a fast cpu, you can use ffv1rec, which gives better picture with a smaller size at the cost of a much higher CPU consumption.

The previous chaper basics still applies as the generated stream is a modified nuv file.

2.Encode to Mpeg2/SVCD

The aim of this paragraph is to proper a edl/project file containing the original captured file, with all commercials removed and video cleaned up as much as possible.

Generate an index 

First we will create an index of the file to allow fast reloading later.

Open the file File-> Open video : video.nuv. Upon loading completion, you will be prompted to save an index : video.nuv.idx

Reload the generated index

Commercials removal 

For each unwanted part, set the markA at the beginning of the part you want to remove by pressing , then go the first frame after the last frame of the commercial and press .

That selects an area for action [markA,markB[.

Press CTRL+X. And repeat to remove beginning/ ending crap and all commercials.

When satisfied with the result, save the edl file by using the Edit ->save edit list menu. For example, video.edl.

Now we have an edl file containing only the interesting part of the video.

Encode audio with avidemux: 

Reload the edl, and set the audio filters using the

Select Normalize WAV, we wish to have all SVCD's with the same base volume.

Don't forget to press the .

Finally, don't forget to select a codec, in our case we want to have mpeg audio. So press the audio codec, and select FFmpeg mpeg audio and configure. For example, 224 kbits stereo.

Last step: encoding! . File->save audio : video.mp2. Alternatively, you could use the internal toolame library, the quality is generally better.

Encode video with avidemux 

The first step is to reload the workbench :Edit->Load Edit List.

We will crop the video, apply some filters and resize the video to SVCD.

Let's go to the filter chain, either by using F1 or .

The first filter will be crop to remove black borders or flickering part, don't hesitate to cut deep. Noise will make the encoding harder and drop the quality

You may also want to deinterlace the movie. Depending if you use Pal or NTSC and the source you may want Deinterlace, Pal Smart, ivtc .... Try them using the preview and pick the one you prefer.

When happy, click the SVCD Res button to resize it to 480*xxx. The source is considered as being 1:1, do it manually if it is not the case

Last step : encoding.

Select video codec, SVCD and configure. If you have plenty of time, select 2 pass, else CQ = 7 for example.

In both cases set the max bitrate to 2300 to be within SVCD specs. Don't forget to set video mode to process!

File / save video: video.m2v

3.Multiplex audio and video

There are two programs to do that:

  • mplex from mjpegtools package
  • tcmplex from transcode package
cause i had mjpegtools installed, i used that.

    Code listing .1

    $ mplex -f 4 -V -s 2324 -p 1 -o video.mpg video.mp2 video.m2v
if you have transcode installed, you can try:

    Code listing .2

    $ tcmplex -i video.m2v -p video.mp2 -o video.mpg -m s 

4.Build SVCD image

You cannot write the files directly to the CD-R. To build a compliant SVCD you should use vcdimager:

    Code listing .1

    $ vcdimager -t svcd video.mpg  
    INFO: scanning mpeg track #0 for scanpoints...   
    INFO: writing track 1 (ISO9660)...   
    INFO: writing track 2, MPEG2, PAL S (480x576/25fps), 1 audio stream...
    finished ok, image created with 1023 sectors (2406096 bytes)
    
if you get:

    Code listing .2

    $ vcdimager -t svcd video.mpg
    INFO: scanning mpeg track #0 for scanpoints...
    vcdimager: vcd.c:612: _finalize_vcd_iso_track: Assertion `_vcd_salloc_get_highest (obj->iso_bitmap) + 1 == 225' failed.Aborted (core dumped)
    
then you should use tcmplex for multiplexing or vcdimager with version >= 0.7

The future: vcdimager also contain two tools named vcdxgen and vcdxbuild.

    Code listing .3

    $ ./vcdimager-0.7.13/frontends/xml/vcdxgen -t svcd video.mpg
    (Super) VideoCD xml description created successfully as `videocd.xml'

The XML File created by vcdxgen can be changed to support chapters. To do this edit the sequence items in the videocd.xml file. Change:

    Code listing .4

      <sequence-items>
        <sequence-item src="video.mpg" id="sequence-00"/>
      </sequence-items>
to

    Code listing .5

      <sequence-items>
        <sequence-item src="video.mpg" id="sequence-00">
          <entry id="chapter1">0.00</entry>
          <entry id="chapter2">15.00</entry>
          <entry id="chapter3">30.00</entry>
        </sequence-item>
      </sequence-items>

The numbers inside the entry tags are seconds from start of the track. To have a DVD chapter like handling, rewrite the pdc section from:

    Code listing .6

      <pbc>
        <playlist id="lid-000">
          <next ref="lid-end"/>
          <return ref="lid-end"/>
          <wait>5</wait>
          <autowait>0</autowait>
          <play-item ref="sequence-00"/>
        </playlist>
        <endlist id="lid-end" rejected="true"/>
      </pbc>
    
to

    Code listing .7

      <pbc>
        <selection id="select-chapter1">
          <next ref="select-chapter2"/>
          <timeout ref="end"/>
          <wait>1</wait>
          <loop jump-timing="immediate">1</loop>
          <play-item ref="chapter1"/>
        </selection>
        <selection id="select-chapter2">
          <prev ref="select-chapter1"/>
          <next ref="select-chapter3"/>
          <timeout ref="end"/>
          <wait>1</wait>
          <loop jump-timing="immediate">1</loop>
          <play-item ref="chapter2"/>
        </selection>
        <selection id="select-chapter3">
          <prev ref="select-chapter2"/>
          <timeout ref="end"/>
          <wait>1</wait>
          <loop jump-timing="immediate">1</loop>
          <play-item ref="chapter3"/>
        </selection>
        <endlist id="end" rejected="true"/>
      </pbc>
    

A description of the xml tags can be found at http://www.geocities.com/medinotes/vcd/general_xml_structure.html.

You should read the excellent document http://www.geocities.com/medinotes/vcd/chapters.html to learn, why VCD/SVCD chapters never will work like DVD chapters !!!

    Code listing .8

    $ ./vcdimager-0.7.13/frontends/xml/vcdxbuild videocd.xml
    ++ WARN: initializing libvcd 0.7.13 [linux-gnu/i686]
    ++ WARN:  
    ++ WARN:  this is the UNSTABLE development branch!
    ++ WARN:  use only if you know what you are doing
    ++ WARN:  see http://www.hvrlab.org/~hvr/vcdimager/ for more information
    ++ WARN:  
       INFO: scanning mpeg sequence item #0 for scanpoints...
    ++ WARN: sequence 'sequence-00' is not reachable by PBC
       INFO: writing track 1 (ISO9660)...
       INFO: writing track 2, MPEG2, PAL 2/3 D1 (480x576/25fps), audio[0]: l2/44.1kHz/128kbps/stereo ...
       INFO: finished ok, image created with 336057 sectors [74:40.57]
    

5.Burning the image

    Code listing .1

    $ cdrdao write --device /dev/sg0 videocd.cue
    or
    $ cdrdao write --device 2,0,0 --driver generic-mmc -n videocd.cue
Of course replace sg0 or 2,0,0 by your actual cd-writer device.

6.Extracting back the Mpeg file

Sometimes you may need the "original" MPEG files back from SVCD. From the Hard Disc:

    Code listing .1

    $ vcdrip --rip -b videocd.bin
    INFO: avseq01.mpg: 450 -> 1023
From the CD-R:

    Code listing .2

    $ vcdrip --rip --cdrom-device=/dev/scd0
    INFO: avseq01.mpg: 450 -> 1260

7.Final words

If you have problems with the resulting SVCD, you may want to try one of these :
  • Try vcdimager demo image
  • Use mplex / or tcmplex
  • vcdimager options :
  • Code listing .1

         with option --broken-svcd-mode
         with option --nopbc
         with option --broken-svcd-mode --update-scan-offsets --nopbc