TV Headend post processing of recordings

Post processing
A post-processing command can be configured to be run by TVHeadend after a recording has been finished.
Recordings in TVHeadend produce an mkv file (there are some more options in the transcoding branch).
Post-processing can be handy for example :
-to transcode the recording to a different media-format,
- to move a file depending on certain rules (move series one way, movies another), or
-to create a lower-bitrate versions of a recording for watching on a mobile device like andorid or iphone, or just to save disk-space (for regular TV signals the entire stream is recorded, resulting in a large file (8 Mb/s, giving a file of 3 or 4 GBytes for an hour of recording).
- or just to create a notification that a recording has finished, using for example a script that sends an email or a chat-message using xmpp.

Execute a bash command or script from Tvheadend
Scripts are called from the tab “configuration”, “digital video recorder” , and then the field “Post-processor command”

Use a bash script for processing
One can either execute a single line bash-command, or use that single line command to execute an external bash-script.

Most flexible way is this last option: to point the post-processing command to a bashscript, so you can add more commands and create a more complex post-processing script. Also, you are more flexible in testing the script.
So, first write a seperate script that does the transcoding, and than to call that script from TVHeadend.

Example Transcoding script using FFMPEG:
Create a file called “” using your text editor
with the code below; note that the script creates a logfile in “/opt/tvheadend/home” ; This is the home-location of my thheadend-user, but this might be different for your server. Any loication can be used, as long as the tvheadend user has permissions to write there:

echo "start script"
/bin/date >>/opt/tvheadend/home/convert.log
/usr/bin/ffmpeg -y -i "$1" -b 1000000 -ac 2 "$1.mpeg" >/opt/tvheadend/home/convert.log 2>&1

Make the script executable by the tvheadend user ( or by all users):
chmod +x

The script takes the inputfilename as option:
./ filename.mpeg

What does it do?
This simple script uses the filename specified as input for ffmpeg ($1 in the code).

The transcoding takes place in the line “/usr/bin/ffmpeg”, before the dev/null part. You need to specifiy the complete path to ffmpeg ( or any other command you use).

/usr/bin/ffmpeg -y -i "$1" -b 1000000 -ac 2 "$1.mpeg"

FFmpeg takes the file $1, transcodes it to an mpeg-file with a maximum bitrate of 10000000 (-b 1000000), and saves it to the same filename with the addition of .mpeg ( “$1.mpeg”). /dev/null is needed for ffmpeg to put its output to, otherwise the script will work from the commandline (bash shell), but not when it is called from Tvheadend.

The “>>/opt/tvheadend/home/convert.log 2>&1″ part writes the output to a logfile for debugging purposes. The logfile is located in /opt/tvheadend/home, since that is where the home directory of my Tvheadend user is located. (This might be different for your system if you used a different install method or parameters when you installed tvheadend; just make sure Tvheadend is allowed to write to that file).

Transcoding files
As mentioned, an obvious option for using post-processing is transcoding of the recorded file.

Transcoding files from one format into another or from one resolution to another ( with all the options there are) is a subject in itself. There are many ways to encode sound or video (like flv, mpeg, mpeg4, flac), containerfiles (like mkv, avi or mp4) and various programs to display or create various formats. For example, an mkv-file (for example as created by a tvheadend recording) is a container, containing a video and audio streams (for example, streams for various languages), and maybe also other information and subtitles. To read about the difference between containers, mediaformats and codecs take a look at
One way to transcode a mediafile is to use ffmpeg, but others are available as well, like VLC ( the well-know media player has a powerfull command line interface for all kind of transcoding and streaking options), “handbrake” or mencoder.

To analyse a stream one can use mediainfo. This is a commandline tool to analyse the content of a containerfile or mediafile. There is also a gui-version.
Install MediaInfo using using:
On gentoo:
emerge --ask mediainfo
Or on Debian:
apt-get install mediainfo

This entry was posted in HTPC, Linux, Uncategorized. Bookmark the permalink.