This page is outdated. Please, help us to sync it with the current tvheadend.

Post recording scripts

On this page you can find a number of (proposed) post recording scripts that you could use to process the Tvheadend recording.

post recording basics

In the DVR configuration page you can specify a "Post-processor command". This can be reference to any program/script that tvheadend will run after the recording has been finished. In order to make it possible to give the program/script some information about the recording that has just finished you can use some variables to build up the actual command that will be executed. Here is the extract from the Tvheadend help page:

Post-processor command
Command to run after finishing a recording. The command will be run in background and is executed even if a recording is aborted or an error occurred. Use the %e error formatting string to check for errors, the error string is empty if recording finished successfully.

 Support format strings:
    Format    Description                    Example value
    %f        Full path to recording                /home/user/Videos/News.mkv
    %b        Basename of recording                News.mkv
    %c        Channel name                    BBC world
    %C        Who created this recording            user
    %t        Program title                    News
    %d        Program description                News and stories...
    %S        Start time stamp of recording, UNIX epoch    1224421200
    %E        Stop time stamp of recording, UNIX epoch    1224426600
    %e          Error message                                   Aborted by user

Example usage: /path/to/ffmpeg -i %f -vcodec libx264 -acodec copy "/path/with white space/%b"
You need to use quotes or escape white spaces if you want white spaces in an argument.

possible uses

The post-processor command can be used to start anything, like in the example about you could use it to transcode the recording to an other format, or anything else you would like. Here we will list some options. Some are complete scripts, others are just thoughts that need to be worked out further.

Commercial Flagging

[[span(Work in Progress, style=color: red; font-size: 100%)]]

Tvheadend does not have it's own build in commercial flagging tool. This was done on purpose because Tvheadend wants to concentrate on the recording and streaming side and leave any post processing to applications/scripts that are much better at this. This means that in order to detect and possibly remove commercials you will need to use seperate programs.

One of the most known commercial detection programs is the one included with MythTV: mythcommflag. This program can also be run as a stand alone tool to detect and flag commercials in valid MPEG files. At the moment Tvheadend does not record directly to MPEG, but it is using the MKV container.

A possible be script would do these steps:
  • recode the MKV to a MPEG-TS file
  • Flag commercials to a cut list
  • remove the suggested frames from the mpeg-ts
flagging the commercials using mythcommflag could possibly be done like this:
  • use the "--file filename" option to point to the mpeg file
  • use the "--gencutlist" option to generate a new cutlist for the file
  • use the undocumented "--outputfile FILE " option to dump the cutlist to a file

removing the commercials from the file could posisbly be done by mythtranscode. This needs further investigation, because it looks that mythtranscode does not have an option to specify a cutlist from a file. It expects the cutlist to be available in the mythtv database (which we do not have ofcourse)

What about prospect of recording everything on specified channels/transponders then afterwards specifying channel name and in and out points to cut it up?

Flagging with Comskip.exe

Tvheadend can be used with Comskip and Wine (Please note that in order to keep your system as clean as possible you only need to install the wine-core package). However, due to a/v sync problems with recordings and comskip I needed to use the following to convert recordings into a format comskip can decode:

ffmpeg -v 10 -i "%f" -vcodec mpeg2video -sameq -acodec copy -f vob -copyts "title.mpg" 

As %b contains .mkv you will need to use a script to achieve the output filename (title.mpg).

Here's a script which I made for the purpose of transcoding and then running comskip.exe on the resulting file:

#!/bin/sh

# Variables
MKVVIDEO=$1    #Full path to recording                /home/user/Videos/News.mkv
b=$2        #Basename of recording                News.mkv
c=$3        #Channel name                    BBC world
C=$4        #Who created this recording            user
t=$5        #Program title                    News
d=$6        #Program description                News and stories...
S=$7        #Start time stamp of recording, UNIX epoch    1224421200
E=$8        #Stop time stamp of recording, UNIX epoch    1224426600
b=$(echo "$b" | sed "s/....$//")    # Remove file ext (4 char)
MKVPATH=$(echo "$MKVVIDEO" | sed "s/....$//")    # Remove file ext (4 char)

TRANSCODEPATH='~/Videos/TranscodedRecordings/'                    # EDIT
EDLFILE="$TRANSCODEPATH$b.edl" 
COMSKIPPATH="c:/Program Files/comskip80_032/comskip.exe"            # EDIT
COMSKIPLOGS="/home/nick/.wine/dosdevices/c:/Program Files/comskip80_032/logs/"    # EDIT

MPGVIDEO=$TRANSCODEPATH$b".mpg"    # Add new extention to filename

/bin/sync    # flush diskbuffer

# Transcode to mpg
echo "*****" >/var/log/tvheadendpp.log
echo "*****" >>/var/log/tvheadendpp.log
echo "Starting Transcode to .mpg" >>/var/log/tvheadendpp.log
echo "*****" >>/var/log/tvheadendpp.log
echo "***** INPUT = $MKVVIDEO *****" >>/var/log/tvheadendpp.log
echo "***** OUTPUT = $MPGVIDEO *****" >>/var/log/tvheadendpp.log
echo "*****" >>/var/log/tvheadendpp.log
/usr/bin/whoami >>/var/log/tvheadendpp.log    # for debugging purposes, who is running this script?
/bin/date >>/var/log/tvheadendpp.log
echo "*****" >>/var/log/tvheadendpp.log
echo "*****" >>/var/log/tvheadendpp.log

ffmpeg -v 10 -i "$MKVVIDEO" -vcodec copy -acodec copy "$MPGVIDEO" </dev/null >>/var/log/tvheadendpp.log 2>&1    

echo "*****" >>/var/log/tvheadendpp.log
echo "*****" >>/var/log/tvheadendpp.log

echo "Starting Commercial Flagging" >>/var/log/tvheadendpp.log
echo "*****" >>/var/log/tvheadendpp.log
echo "***** INPUT = "$MPGVIDEO" *****" >>/var/log/tvheadendpp.log
echo "***** OUTPUT = "$EDLFILE" *****" >>/var/log/tvheadendpp.log
echo "*****" >>/var/log/tvheadendpp.log
/usr/bin/whoami >>/var/log/tvheadendpp.log    # for debugging purposes, who is running this script?
/bin/date >>/var/log/tvheadendpp.log
echo "*****" >>/var/log/tvheadendpp.log
echo "*****" >>/var/log/tvheadendpp.log

wine "$COMSKIPPATH" "$MPGVIDEO" </dev/null >>/var/log/tvheadendpp.log 2>&1 

echo "*****" >>/var/log/tvheadendpp.log
echo "*****" >>/var/log/tvheadendpp.log

# Move Comskip Log File
echo "Moving Log file to '$COMSKIPLOGS$b.log'" </dev/null >>/var/log/tvheadendpp.log 2>&1 

mv "$TRANSCODEPATH$b.log" "$COMSKIPLOGS" 

# Move .edl back to MKV Path
echo "Copying edl file to '$MKVPATH.edl'" </dev/null >>/var/log/tvheadendpp.log 2>&1 
cp "$EDLFILE" "$MKVPATH.edl" 

echo "EDL for $MKVVIDEO:" >>/var/log/tvheadendpp.log

echo "Finished at @[email protected]" >>/var/log/tvheadendpp.log

Use the following in Tvheadend

/path/to/script.sh %f %b

It is by no means complete or the best way to achieve this. It is just the best I could put together with the limited information I could find. I am looking forward to discussion and improvement in achieving commercial flagging with comskip as the results I have seen have been very good but the processing time is long due to having to transcode the recordings first.

Flagging with Mythcommflag

Despite supposedly being possible, I was unable to get mythcommflag to work with the -skipdb option. It would work with recordings which it had in mythtv's database but renaming and using the same file with the -skipdb option would result in an empty edl. I am open to suggestions in this area too.

Remove duplicate Recordings

How it works:

  1. The python script creates a txt file containing the description of the recording.
  2. The script walks through the folders and looks for similar txt files with the same description by comparing the first 100 characters of a description
  3. If multiple recordings based on the same description found, the file sizes of the matched recordings are compared and the files with lower file sizes will be deleted (including their txt file)

How to add to Tvheadend:

  1. Download pp.py from https://gist.github.com/ckarrie/1efbb8aaa8b8038b28f3/download
  2. Add a new or edit an existing recording setting
  3. Add following line in the Post-processing command field: /usr/bin/python2.7 <path/to/your/pp.py> "%f" "%d"

Note:

I've checked all Checkboxes in the "Filename Options" group. I don't know if its necessary.

Get more infos in the source code: https://gist.github.com/ckarrie/1efbb8aaa8b8038b28f3, suggests welcome!