Project

General

Profile

Tvheadend post recording scripts » History » Version 13

Jaroslav Kysela, 2017-09-17 00:15

1 13 Jaroslav Kysela
h1. This page is outdated. Please, help us to sync it with the current tvheadend.
2 9 ruud -
3
h1. Post recording scripts
4
5 1 Hein Rigolo
On this page you can find a number of (proposed) post recording scripts that you could use to process the Tvheadend recording.
6
7 9 ruud -
8
h2. post recording basics
9
10 2 Hein Rigolo
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:
11 1 Hein Rigolo
12 9 ruud -
*Post-processor command*
13 2 Hein Rigolo
 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.
14
15 9 ruud -
<pre>
16 3 Hein Rigolo
 Support format strings:
17
    Format	Description					Example value
18
    %f		Full path to recording				/home/user/Videos/News.mkv
19
    %b		Basename of recording				News.mkv
20
    %c		Channel name					BBC world
21
    %C		Who created this recording			user
22 1 Hein Rigolo
    %t		Program title					News
23 3 Hein Rigolo
    %d		Program description				News and stories...
24 1 Hein Rigolo
    %S		Start time stamp of recording, UNIX epoch	1224421200
25 2 Hein Rigolo
    %E		Stop time stamp of recording, UNIX epoch	1224426600
26 1 Hein Rigolo
    %e          Error message                                   Aborted by user
27 9 ruud -
</pre>
28 1 Hein Rigolo
29 6 Anonymous
Example usage: /path/to/ffmpeg -i %f -vcodec libx264 -acodec copy "/path/with white space/%b"
30 1 Hein Rigolo
You need to use quotes or escape white spaces if you want white spaces in an argument.
31
32 9 ruud -
33
h2. possible uses
34
35 1 Hein Rigolo
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.
36
37
38 9 ruud -
h3. Commercial Flagging
39
40
[[span(*Work in Progress*, style=color: red; font-size: 100%)]]
41
42 2 Hein Rigolo
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.
43 1 Hein Rigolo
44 9 ruud -
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.
45 1 Hein Rigolo
46
A possible be script would do these steps:
47 9 ruud -
* recode the MKV to a MPEG-TS file
48
* Flag commercials to a cut list
49
* remove the suggested frames from the mpeg-ts
50 2 Hein Rigolo
51
flagging the commercials using mythcommflag could possibly be done like this:
52 9 ruud -
* use the "--file filename" option to point to the mpeg file
53
* use the "--gencutlist" option to generate a new cutlist for the file
54
* use the undocumented "--outputfile FILE " option to dump the cutlist to a file
55 2 Hein Rigolo
56 1 Hein Rigolo
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)
57
58
What about prospect of recording everything on specified channels/transponders then afterwards specifying channel name and in and out points to cut it up?
59 5 Anonymous
60
61 9 ruud -
h4. Flagging with Comskip.exe
62
63
64
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":http://forum.xbmc.org/showpost.php?p=602568&postcount=894 with recordings and comskip I needed to use the following to convert recordings into a format comskip can decode: 
65
<pre>
66 5 Anonymous
ffmpeg -v 10 -i "%f" -vcodec mpeg2video -sameq -acodec copy -f vob -copyts "title.mpg"
67 9 ruud -
</pre>
68 5 Anonymous
69
As %b contains .mkv you will need to use a script to achieve the output filename (title.mpg). 
70
71
Here's a script which I made for the purpose of transcoding and then running comskip.exe on the resulting file:
72
73 9 ruud -
<pre>
74 5 Anonymous
#!/bin/sh
75
76
# Variables
77
MKVVIDEO=$1	#Full path to recording				/home/user/Videos/News.mkv
78
b=$2		#Basename of recording				News.mkv
79
c=$3		#Channel name					BBC world
80 8 ruud -
C=$4		#Who created this recording			user
81 5 Anonymous
t=$5		#Program title					News
82
d=$6		#Program description				News and stories...
83
S=$7		#Start time stamp of recording, UNIX epoch	1224421200
84
E=$8		#Stop time stamp of recording, UNIX epoch	1224426600
85
b=$(echo "$b" | sed "s/....$//")    # Remove file ext (4 char)
86
MKVPATH=$(echo "$MKVVIDEO" | sed "s/....$//")    # Remove file ext (4 char)
87
88
TRANSCODEPATH='~/Videos/TranscodedRecordings/'					# EDIT
89
EDLFILE="$TRANSCODEPATH$b.edl"
90
COMSKIPPATH="c:/Program Files/comskip80_032/comskip.exe"			# EDIT
91
COMSKIPLOGS="/home/nick/.wine/dosdevices/c:/Program Files/comskip80_032/logs/"	# EDIT
92
93
MPGVIDEO=$TRANSCODEPATH$b".mpg"	# Add new extention to filename
94
95
/bin/sync	# flush diskbuffer
96
97
# Transcode to mpg
98
echo "*****" >/var/log/tvheadendpp.log
99
echo "*****" >>/var/log/tvheadendpp.log
100
echo "Starting Transcode to .mpg" >>/var/log/tvheadendpp.log
101
echo "*****" >>/var/log/tvheadendpp.log
102
echo "***** INPUT = $MKVVIDEO *****" >>/var/log/tvheadendpp.log
103
echo "***** OUTPUT = $MPGVIDEO *****" >>/var/log/tvheadendpp.log
104
echo "*****" >>/var/log/tvheadendpp.log
105
/usr/bin/whoami >>/var/log/tvheadendpp.log	# for debugging purposes, who is running this script?
106
/bin/date >>/var/log/tvheadendpp.log
107
echo "*****" >>/var/log/tvheadendpp.log
108
echo "*****" >>/var/log/tvheadendpp.log
109
110
ffmpeg -v 10 -i "$MKVVIDEO" -vcodec copy -acodec copy "$MPGVIDEO" </dev/null >>/var/log/tvheadendpp.log 2>&1	
111
112
echo "*****" >>/var/log/tvheadendpp.log
113
echo "*****" >>/var/log/tvheadendpp.log
114
115
116
echo "Starting Commercial Flagging" >>/var/log/tvheadendpp.log
117
echo "*****" >>/var/log/tvheadendpp.log
118
echo "***** INPUT = "$MPGVIDEO" *****" >>/var/log/tvheadendpp.log
119
echo "***** OUTPUT = "$EDLFILE" *****" >>/var/log/tvheadendpp.log
120 1 Hein Rigolo
echo "*****" >>/var/log/tvheadendpp.log
121
/usr/bin/whoami >>/var/log/tvheadendpp.log	# for debugging purposes, who is running this script?
122 5 Anonymous
/bin/date >>/var/log/tvheadendpp.log
123 1 Hein Rigolo
echo "*****" >>/var/log/tvheadendpp.log
124
echo "*****" >>/var/log/tvheadendpp.log
125 5 Anonymous
126
wine "$COMSKIPPATH" "$MPGVIDEO" </dev/null >>/var/log/tvheadendpp.log 2>&1 
127 1 Hein Rigolo
128
echo "*****" >>/var/log/tvheadendpp.log
129
echo "*****" >>/var/log/tvheadendpp.log
130 5 Anonymous
131
# Move Comskip Log File
132
echo "Moving Log file to '$COMSKIPLOGS$b.log'" </dev/null >>/var/log/tvheadendpp.log 2>&1 
133
134
mv "$TRANSCODEPATH$b.log" "$COMSKIPLOGS"
135
136
# Move .edl back to MKV Path
137
echo "Copying edl file to '$MKVPATH.edl'" </dev/null >>/var/log/tvheadendpp.log 2>&1 
138
cp "$EDLFILE" "$MKVPATH.edl"
139
140
echo "EDL for $MKVVIDEO:" >>/var/log/tvheadendpp.log
141
142 9 ruud -
echo "Finished at @date@" >>/var/log/tvheadendpp.log
143
</pre>
144 5 Anonymous
145
Use the following in Tvheadend 
146 9 ruud -
<pre>
147 5 Anonymous
/path/to/script.sh %f %b
148 9 ruud -
</pre>
149 5 Anonymous
150
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.
151
152 9 ruud -
153
h4. Flagging with Mythcommflag
154
155 1 Hein Rigolo
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.
156 11 C K
157
h3. Remove duplicate Recordings
158
159
How it works: 
160
161
# The python script creates a txt file containing the description of the recording.
162
# 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
163
# 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)
164
165
How to add to Tvheadend:
166
167
# Download pp.py from https://gist.github.com/ckarrie/1efbb8aaa8b8038b28f3/download
168
# Add a new or edit an existing recording setting
169
# Add following line in the Post-processing command field: /usr/bin/python2.7 <path/to/your/pp.py> "%f" "%d"
170
171
Note:
172
173
I've checked all Checkboxes in the "Filename Options" group. I don't know if its necessary. 
174
175 12 C K
Get more infos in the source code: https://gist.github.com/ckarrie/1efbb8aaa8b8038b28f3, suggests welcome!