IPTV – Tuning and Recording

Listing of Digital Channels in Australia can be found here.

bash-3.1$ tzap -c /etc/channels.conf -r “ABC2”
using ‘/dev/dvb/adapter0/frontend0’ and ‘/dev/dvb/adapter0/demux0’
reading channels from file ‘/etc/channels.conf’
tuning to 226500000 Hz
video pid 0x0905, audio pid 0x0906
status 00 | signal 00ff | snr 8000 | ber 00003fff | unc 00000000 |
status 1f | signal 002c | snr ffff | ber 00000000 | unc 00000000 | FE_HAS_LOCK
status 1f | signal 002c | snr ffff | ber 00000000 | unc 00000000 | FE_HAS_LOCK
status 1f | signal 002c | snr ffff | ber 00000000 | unc 00000000 | FE_HAS_LOCK
status 1f | signal 002c | snr ffff | ber 00000000 | unc 00000000 | FE_HAS_LOCK

bash-3.1$ cat /dev/dvb/adapter0/dvr0 > /videos/sunday-07-04-29.ts

The above command uses cat to create a file named /videos/sunday-07-04-29.ts. The output from dvr0 is a transport stream, hence the extension .ts. When using this method I am limited to saving the broadcast as a transport stream. I will convert (encode) it later to a program stream, i.e. .mpeg-ps. The .VOB files found on DVD’s are .mpeg-ps files.
Cat will keep on running until I kill it so I switch to third terminal and run ls -l. As you can see above, a minute or so after running “cat”, /videos/sunday-07-04-29.ts is already at 393.2 Kb. You need lots of space to record videos. It is better to do it on a separate partition. If you leave a program streaming into a directory on your root partition until there is no space left, you may wish you hadn’t.

bash-3.1$ tcprobe -i /videos/sunday-07-04-29.ts
[tcprobe] MPEG transport stream (TS)
(ts_reader.c) end of stream
(ts_reader.c) Pids: 0x906 0x905 [tcprobe] summary for /videos/sunday-07-04-29.ts,
(*) = not default, 0 = not detected
no audio track: use “null” import module for audio

The above output shows the results of running tcprobe from Transcode. It confirms that the file being created is an mpeg-ts. There is no seperate audio track. I can also use Mplayer to analyse the video.

bash-3.1$ mplayer -vo dummy -identify /videos/sunday-07-04-29.ts
MPlayer 1.0rc1-3.4.6 (C) 2000-2006 MPlayer Team
CPU: AMD Athlon(tm) XP 2800+ (Family: 6, Model: 10, Stepping: 0)
CPUflags: MMX: 1 MMX2: 1 3DNow: 1 3DNow2: 1 SSE: 1 SSE2: 0
Compiled with runtime CPU detection.
Playing /videos/sunday-07-04-29.ts.
TS file format detected.
NO VIDEO! NO AUDIO! NO SUBS (yet)!
No stream found.
Exiting… (End of file)

The transport stream format is O.K. if I just want a quick look at something but if I wanted to keep it for later viewing or create a video DVD I would encode to an mpeg-ps format. There is more than one way to do this but I use mencoder, which comes with Mplayer. Syntax for a basic encoding follows:

mencoder -of mpeg -ni -mpegopts format=dvd:vbitrate=9000 -o sunday-07-04-29.mpg -oac copy -ovc copy sunday-07-04-29.ts

Upon completion of this command I will now have a video encoded to an mpeg-ps format and I can check out it’s properties with mplayer.

bash-3.1$ mplayer -vo dummy -identify /videos/sunday-07-04-29.ts
MPlayer 1.0rc1-3.4.6 (C) 2000-2006 MPlayer Team
CPU: AMD Athlon(tm) XP 2800+ (Family: 6, Model: 10, Stepping: 0)
CPUflags: MMX: 1 MMX2: 1 3DNow: 1 3DNow2: 1 SSE: 1 SSE2: 0
Compiled with runtime CPU detection.
Playing sunday-07-04-29.mpg.
ID_VIDEO_ID=0
ID_AUDIO_ID=0
MPEG-PS file format detected.
VIDEO: MPEG2 720×576 (aspect 3) 25.000 fps 9000.0 kbps (1125.0 kbyte/s)
ID_FILENAME=sunday-07-04-29.mpg
ID_DEMUXER=mpegps
ID_VIDEO_FORMAT=0x10000002
ID_VIDEO_BITRATE=9000000
ID_VIDEO_WIDTH=720
ID_VIDEO_HEIGHT=576
ID_VIDEO_FPS=25.000
ID_VIDEO_ASPECT=0.0000
ID_AUDIO_FORMAT=80
ID_AUDIO_BITRATE=0
ID_AUDIO_RATE=0
ID_AUDIO_NCH=0
ID_LENGTH=65.52
Error opening/initializing the selected video_out (-vo) device.
Exiting… (End of file)

This encoding will do just fine if I am saving videos to disk for later viewing or if I want to edit out commercials and such using Gopdit, my prefered mpeg-ps editor.
However, I use a longer mencoder command to prepare my videos for burning to a DVD. The command listed below usually decreases the size of the video and my experience has been that I have less problems with audio/video sync. It will encode either a tranport stream or a program stream. Both this command and the one above produce a PAL video.

mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd -vf scale=720:576,harddup -srate 48000 -lavcopts \ vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=5000:keyint=15:acodec=ac3:abitrate=192:aspect=16/9 -ofps 25 $INPUT_FILE -o $OUTPUT_FILE

Note: I have broken the third line of the above command with a comment preceded by ” # “. The third line in the above example is a continuation of the second line. If you copy it, join the two lines with no #, comment or space between them there is no way to break that line with a forward slash.

To make life simple, and because I am incapable of remembering the syntax of long commands, everything required could be put into a script The snippet below shows how I could use tzap to lock on to a signal and dvbstream to save it to disk as an mpeg-ps in a script.

# If frontend0 is active, kill it.
fuser -k /dev/dvb/adapter0/frontend0
# Tune in and lock channel
/usr/local/bin/tzap -c /etc/channels.conf -r “ABC2” &
# Then get the pid of tzap
TZAP_PID=$!
# Ok so far? Lets get that channel up with dvbstream
/usr/local/bin/dvbstream -f 226.5 2307 2308 -ps -o > $VIDEO_DIR/$NAME.mpg &
DVBSTREAM_PID=$!
# Set duration of recording after converting minutes to seconds
DURATION=$(($TIME*60))
sleep $DURATION
kill $TZAP_PID
kill $DVBSTREAM_PID

In the example above tzap usage is exactly the same but dvbstream also needs to know which channel (frequency) to grab. The numbers after “-f” are taken from my channels config.

ABC2:226500000:INVERSION_AUTO:BANDWIDTH_7_MHZ:FEC_3_4:FEC_3_4:QAM_64:TRANSMISSION_MODE_8K:GUARD_INTERVAL_1_16:HIERARCHY_NONE:2307:2308:562

Below is the command and output from dvbstream for ABC2. Don’t give the line about “tuning DVB-T (in United Kingdom)” too much credence. That’s in the code and I am in Australia.

bash-3.1$ ././dvbstream -qam 64 -cr 3_4 -gi 16 -bw 7 -tm 8 -f 226500 -ps -n 10 -o 2308 2307 > /videos/sunday-07-04-29_dvbstrm.mpg
dvbstream v0.5 – (C) Dave Chapman 2001-2004
Released under the GPL.
Latest version available from http://www.linuxstb.org/
Using DVB card “Conexant CX22702 DVB-T”
tuning DVB-T (in United Kingdom) to 226500000 Hz
polling….
Getting frontend event
FE_STATUS:
polling….
Getting frontend event
FE_STATUS: FE_HAS_SIGNAL FE_HAS_LOCK FE_HAS_CARRIER FE_HAS_VITERBI FE_HAS_SYNC
Bit error rate: 0
Signal strength: 43
SNR: 65535
FE_STATUS: FE_HAS_SIGNAL FE_HAS_LOCK FE_HAS_CARRIER FE_HAS_VITERBI FE_HAS_SYNC
Setting filter for PID 2308
Setting filter for PID 2307
Output to stdout
Streaming 2 streams
Audiostream: Layer: 1 BRate: 128 kb/s Freq: 44.1 kHz
Videostream: ASPECT: 16:9 Size = 720×576 FRate: 25 fps BRate: 15.00 Mbit/s

Let’s see what mplayer has to say about the video I just created:

bash-3.1$ mplayer -vo dummy -identify test.mpg
MPlayer 1.0rc1-3.4.6 (C) 2000-2006 MPlayer Team
CPU: AMD Athlon(tm) XP 2800+ (Family: 6, Model: 10, Stepping: 0)
CPUflags: MMX: 1 MMX2: 1 3DNow: 1 3DNow2: 1 SSE: 1 SSE2: 0
Compiled with runtime CPU detection.
Playing test.mpg.
ID_VIDEO_ID=10
ID_AUDIO_ID=0
MPEG-PS file format detected.
VIDEO: MPEG2 720×576 (aspect 3) 25.000 fps 15000.0 kbps (1875.0 kbyte/s)
ID_FILENAME=test.mpg
ID_DEMUXER=mpegps
ID_VIDEO_FORMAT=0x10000002
ID_VIDEO_BITRATE=15000000
ID_VIDEO_WIDTH=720
ID_VIDEO_HEIGHT=576
ID_VIDEO_FPS=25.000
ID_VIDEO_ASPECT=0.0000
ID_AUDIO_FORMAT=80
ID_AUDIO_BITRATE=0
ID_AUDIO_RATE=0
ID_AUDIO_NCH=0
ID_LENGTH=159.98
Error opening/initializing the selected video_out (-vo) device.
Exiting… (End of file)

Bad Behavior has blocked 744 access attempts in the last 7 days.