Recording from DVB-T and encoding directly into DIVX-Avi-Files

Recently, I was confronted with a problem that was caused by Xinelib or Kaffeine: Any recording from my DVB-T-USB-stick was "out of sync" regarding audio, at least when watching it with any other player than Kaffeine. Searching the web did not help, neither did installing various versions of Xine, Kde, Kaffeine, Mplayer and others. I also tried other grafical tools like klear for recording, but nothing worked.

Thus I decided to go back to the "good old" mencoder/mplayer team, and that did the trick for me: In the Ubuntu user forum, i found a small but great script that the user stw0815 used for recording and encoding. I think it is very intuitive, since it uses Zenity for graphical dialogues. I changed the second version (by Heinrich Schwietering) of the script a little. Adding the mencoder option "-audio-delay 2" in the line REC="…" seems to be sufficient to have a correct audio-synchronisation.

Afterwards, i decided to add DIVX encoding, so that the Mpegs are directly encoded in DIVX (after recording). And I added the dialog in which the user can enter the bitrate he likes. More info to mencoder options are here and in the excellent manpage!

Just some words on the line DIVX="….":

  • Bitrate: According to the manpage, vbitrate=800 is the dafult. 500 kps will do for a watch-and-delete-recording, but 2000 kps will bring good-quality results.
  • Keyint: defines the space between the key frames. Low value means good quality, good searching in the file and big files. However, the bitrate makes much bigger differences.

Here is the script with my two or three changes. Don’t forget to change the default location of the file in the line FILE="…"!

#!/bin/bash

## Author stw0815; revised by Heinrich Schwietering

## Divx encoding and dialog added by Markus Feilner

## http://forum.ubuntuusers.de/topic/70377/; http://forum.ubuntuusers.de/topic/155644

MCD=/usr/bin/mencoder

zenity –help > /dev/null || { echo "Fehler: Du benötigst \"zenity\" um dieses Script nutzen zu können." && exit 1 ; }

test -e $MCD || { zenity –error –text "Fehler: Du benötigst \"mencoder\" um dieses Script nutzen zu können." && exit 1 ; }

test ! -e ~/.mplayer/channels.conf && zenity –error –text "~/.mplayer/channels.conf wurde nicht gefunden" && exit 1

SENDER=`cat ~/.mplayer/channels.conf | cut -d ":" -f 1 | zenity –list –title "Bitte Sender auswählen" –column "Sender:"` || exit 0

test "$SENDER" || { zenity –error –text "Es wurde kein Sender ausgewählt." && exit 1 ; }

DAUER=`zenity –entry –title "Dauer" –entry-text "60" –text "Bitte die Aufnahmedauer in Minuten eingeben"` || exit 0

LENGTH=$(($DAUER*60*25))

test "$LENGTH" -lt 1 && zenity –error –text "Die Aufnahmedauer ist fehlerhaft." && exit 1

DAY=`zenity –calendar –title "Aufnahmedatum" –text "Bitte das Datum der Aufnahme festlegen"` || exit 0

TIME=`zenity –entry –title "Aufnahmestart (HH.MM)" –text "Bitte den Startzeitpunkt der Aufnahme festlegen" –entry-text "$(date –date="0 minutes" +%H.%M)"` || exit 0

FILE=`zenity –file-selection –save –confirm-overwrite –title "Name und Ort zum Speichern auswählen" –filename=/Temp/"$SENDER"-"$DAY"-"$TIME".mpg` || exit 0

VBIT=`zenity –entry –title "Divx Bitrate" –entry-text "2000" –text "Bitte geben Sie die gewünschte Bitrate für die Divx-Datei ein:"` || exit 0

REC="dvb://$SENDER -audio-delay 2 -of mpeg -mpegopts format=dvd -ovc copy -oac copy -frames $LENGTH -o $FILE"

DIVX="-ffourcc DX50 -oac copy -ovc lavc -ofps 25 -lavcopts vcodec=mpeg4:autoaspect=1:vbitrate=$VBIT:vhq:keyint=10:acodec=mp3 -o $FILE.divx.avi $FILE"

echo "$MCD $REC && $MCD $DIVX" | at $TIME $DAY &> /tmp/recerror

test $? -gt 0 && zenity –error –text "$(cat /tmp/recerror)" && rm /tmp/recerror && exit 1

zenity –info –text "Sender: $SENDER\nBegin: $DAY, $TIME Uhr\nDauer: $DAUER Minuten, $LENGTH Einzelbilder\nDateiname: $FILE\n\nStatus: OK\n$(cat /tmp/recerror)"

test -e /tmp/recerror && rm /tmp/recerror

Leave a comment