DVB

This page describes some of the tricks and tools that can be used for capturing and post-processing DVB (Digital Video Broadcast) streams in the UK. It doesn't go into details of obtaining, building or installing software, or of installing device drivers, as that would make it rather too long.

Capturing DVB video

Hardware

To capture a DVB broadcast stream, you will need some hardware. There are three types of DVB going:

  • DVB-T (terrestrial broadcast: Freeview),
  • DVB-S (satellite: Sky), and
  • DVB-C (cable: NTL).

The hardware to capture these is similar, but different: you can't capture a DVB-T broadcast with a DVB-S card. Cards range from the cheap and cheerful (e.g. the Hauppauge NOVA-T) to the really quite expensive. All DVB cards can tune to different channels, receive transmissions, and make the received stream available to suitable software. Some cards can demux streams in hardware (more about this later). Some can take, or talk to, hardware decryption devices for pay-per-view channels. Some have video-out and hardware MPEG2 decoders on board. You get what you pay for.

A cheap budget card such as the NOVA cards will give you just the basic tuning and reception capability, and will cost you a bit less than £100. A card with hardware decoder and video-out may be anything up to 4 times that figure.

It is worth noting that the DVB-S in the UK is actually not quite the DVB-S standard – Sky use their own non-standard encryption system.

Some DVB background

The standard for DVB is for video broadcast as MPEG2, and audio broadcast as AC3 or MP2 (MP3's little brother). There are at least two data streams with each programme being broadcast: one audio, and one video. There may be additional streams such as other types of audio, or subtitles, or teletext. Each of these streams is wrapped up in an MPEG2 Elementary Stream (or ES).

Since DVB is compressed, the bandwidth required to broadcast a programme is approximately 1/4-1/6 of the total bandwidth for an analogue broadcast. This means that where (say) a single TV station can broadcast one programme per UHF channel, they can broadcast up to about 6 programmes per UHF channel with DVB. These collections of programmes, all broadcast on a single frequency, are known as multiplexes or muxes. The programmes bundled up together in each mux are sent out in a Transport Stream (or TS).

So, from the top:

  • Your DVB card can tune to one frequency at a time.
  • It receives a TS containing up to 6 video channels or a large number of audio channels.
  • The TS contains a number of ESes.
  • Each ES contains one data stream, which may be video, audio, or anything else.

The TS bundles contain numeric identifiers for the elementary streams that they contain. These are known as Programme Identifiers, or pids.

There are also Programme Streams (PS), which contain a single video stream, and one or more audio streams. The data in a DVD .vob file, when decrypted, is an MPEG2 PS.

Software

You will need a hardware driver for your DVB card, and various tools to manage it. Fortunately, almost all DVB cards are based on the same reference design, and vary only in some parts of the driver. Also fortunately, drivers for most DVB cards are already in the mainline 2.6 kernel. These drivers will give you a /dev/dvb directory, with a subdirectory for each card, and a bunch of control devices in each subdirectory:

$ ls -lR /dev/dvb/
/dev/dvb/adapter0:
total 0
crw-rw----    1 root     video    250,   1 Jun 19 19:09 audio0
crw-rw----    1 root     video    250,   6 Jun 19 19:09 ca0
crw-rw----    1 root     video    250,   4 Jun 19 19:09 demux0
crw-rw----    1 root     video    250,   5 Jun 19 19:09 dvr0
crw-rw----    1 root     video    250,   3 Jun 19 19:09 frontend0
crw-rw----    1 root     video    250,   7 Jun 19 19:09 net0
crw-rw----    1 root     video    250,   8 Jun 19 19:09 osd0
crw-rw----    1 root     video    250,   0 Jun 19 19:09 video0

It is recommended that you build your DVB drivers as modules. Note that you will need to prevent the driver from shutting down the card a short while after it starts. To do this, add the parameter dvb_shutdown_timeout=0 to the dvb-core module. A line in /etc/modprobe.conf will do this:

options dvb-core dvb_shutdown_timeout=0

The user-space tools for managing DVB cards can be found in two places: LinuxTV and the Metzler brothers' DVB page. The two sets of tools and drivers are similar. Note however, that the Metzler's versions of some pieces of software appear to have more useful tools available. You will need the dvb-mpegtools (part of LIBDVB from the Metzlers).

Finding channels

The first thing to do is to find some channels with your DVB card. Run the scan program. You will need to tell it what transmitter you are receiving from. scan comes with a list of known transmitters to kick-start the process. Pick the one that is relevant to you, and run the program:

$ scan /usr/share/doc/dvb-utils/scan/dvb-t/uk-Rowridge >channels.conf

This will take a while to run, and will eventually produce a complete list of channels available to you, in a somewhat cryptic format. The UK's DVB-T (Freeview) broadcasts approximately 28 TV channels, 23 radio channels, and 18 other services, split over 6 muxes. If you don't get that many channels, try running it again.

The standard output of scan can be used to drive the relevant zap program, which talks to the tuner on a DVB
card. There are three zap programs: tzap, szap, and czap. Pick the appropriate one for your type of DVB. Save the output of scan in ~/.tzap/channels.conf, and you will be able to set the channel using tzap (substitute czap or szap as appropriate):

$ tzap "Channel 4"
using '/dev/dvb/adapter0/frontend0' and '/dev/dvb/adapter0/demux0'
tuning to 530167000 Hz
status 01 | signal 3333 | snr 0000 | ber 0000e265 | unc 00000005 | 
status 1f | signal 3333 | snr d2d2 | ber 000001d8 | unc 00000011 | FE_HAS_LOCK
status 1f | signal 3333 | snr d1d1 | ber 000001b5 | unc 00000000 | FE_HAS_LOCK
...

Once you have a lock on the mux (it will print FE_HAS_LOCK at the end of each line of output if it's successful), you can stop tzap.

Alternatively, you can use the information in the channels.conf file to tune the card directly with dvbstream. More on doing this in the next section.

Getting hold of an MPEG stream

Now that you can tune your card to a mux, you can start getting data off that mux. dvbstream from the dvbtools package is the tool to do this. dvbstream will, by default, attempt to multicast the whole TS from the currently-tuned mux across the network on 224.0.1.2:5004.

You can use the -o flag of dvbstream, which tells dvbstream not to multicast the stream, but to output it to stdout. This allows you to pipe the stream to another process, such as mplayer or xine, or to dump the TS to disk for later processing.

The other thing that you can do, on the more advanced cards (i.e. not "budget" cards), is select the pids to stream with dvbstream. You do this by supplying a list of pids on the command-line. The pids are the third-last and second-last fields in the channels.conf file, and are numbers between 17 and 8191. So, for example, to record BBC 1 to disk, you would use tzap to tune to the right multiplex frequency, and run:

$ dvbstream 600 601 -o >BBC1.mpeg

This will use the TS-demuxing hardware on the [non-budget] DVB card to split out streams 600 and 601 from the broadcast, and deliver those as a DVB TS to the file BBC1.mpeg. Note that "budget" cards don't have the TS-demuxing hardware on them, so the pid selection must be done in software, which dvbstream seems to handle despite the documentation saying that it doesn't. There is a "special" pid that you can select, which is 8192, and which dumps the whole broadcast TS. Note that you can take as many streams as you like from the mux, allowing you to record more than one program from the mux at the same time.

On full cards with MPEG2 decode capabilities, you can use the -v and -a options to select the pids to decode (usually either to screen or to the video-out of the card).

You can also use dvbstream to do its own tuning, which means that you don't need to use tzap. There are a number of options available to do tuning. The main option is the -f option, which tells dvbstream what frequency to tune to. You can look up the frequency in the second field of each line of the channels.conf file. See the output of dvbstream --help for more information.

Playing your stream on the fly

Post-processing

If you use the DVB tools to record your DVB streams, you will probably want to do some post-processing, for example to crop out advertisements and remove leading and trailing material from timed recordings. Finding the right tools and techniques to do this can be problematic.

Find the streams

So, you have an MPEG2 TS that you recorded from your DVB card. Most video playing programs (xine and mplayer, for example) have options that allow them to read specified pids directly out of a TS. However, most of the video-editing tools want to work on a PS, or at best will only see the first each of audio and video in the TS. Therefore, you will need to extract a PS from the TS to work on.

If you don't know what the pids of the streams you want in the TS are, for example if you didn't have your recording script take note of them when it started, you can use tspids from the Metzler brothers' libdvb package, or tcprobe from transcode.

$ tspids <original_file.mpeg
$ tcprobe -i original_file.mpeg

Each of these will print a list of the pids that it finds in the file. Having identified which pids you want to take, the simplest program to extract the pids to a program stream is ts2ps:

$ ts2ps 600 601 <original_file.mpeg >program_stream.mpeg

Cleaning up

One problem with the current UK DVB-T broadcasts is that some channels don't broadcast well-formed MPEG2 transport streams. In fact, some of these streams are so corrupted that some of the video editing software can't cope with them. A good way of cleaning up a corrupted stream is to run it through mencoder (part of mplayer. The following will clean up the structure of a program stream:

$ mencoder -forceidx -lavdopts er=4 -vc ffmpeg12 -of mpeg \
         -oac copy -ovc copy \
         original_ps.mpeg -o cleaned_ps.mpeg

You can also use this method to extract specific pids from a transport stream and produce a clean program stream, by using the -aid and -vid options to mencoder to select the audio and video pids repectively:

$ mencoder -forceidx -lavdopts er=4 -vc ffmpeg12 -of mpeg \
         -oac copy -ovc copy -aid 600 -vid 601 \
         original_ts.mpeg -o cleaned_ps.mpeg

Editing

After you have a video stream, you may want to edit it to remove advertisements and leading/trailing material. Probably the best package to use at the moment is avidemux. Note, however, that it does have some issues at the moment with losing audio sync.

Changing size

If your video stream is slightly too big for your purposes, then you have two options: you can reduce the size of the audio stream, or reduce the size of the video stream. You can do the latter in avidemux, using the "requantize video" option from the Save As menu. I'm still trying to work out how to resize audio streams, or to resize video streams from the command line. More here when I've worked it out.

Scripts

There are some scripts that I've written to help with DVB things, including setting up timed recordings, and some of the uglier command lines from this
article.

Acknowledgements

Thanks to Bob Dunlop for sanity-checking for me.