Project

General

Profile

Post Processing with Handbrake

Added by Andrew Blank almost 10 years ago

This is my first whack at a Post Processing script for the TVHeadend DVR software.

It has it’s faults but it does work. I have future ambitions of creating a script complete with commercial removal and .nfo file generation for the Plex Media Server and Kodi (XBMC) to pick up.

So without further ado here’s a script that takes recording from TVHeadend and transcodes it to an h264/AAC mp4 file using the “Normal” preset in Handbrake(CLI).


In the Post Processing configuration of TVHeadend I have this line:

/home/hts/Convert %f %t %S

That executes my bash script I created called “Convert” in the home directory of TVHeadend (/home/hts/) on Ubuntu and passes in the Full file name and path (%f), the title of the show (%t) and the Unix time stamp of the start of the recording %S.


And here’s the contents of Convert:

#!/bin/bash

# $1 Full File Name with path (%f)
# $2 Show name without path or extension (%t)
# $3 Unix time-stamp for recording start (%S)

# Convert Unix time-stamp to YYYY-MM-DD.HH-MM-SS set to variable $t using perl

t=$(perl -e “use POSIX qw(strftime); print POSIX::strftime(‘%Y-%m-%d.%H-%M-%S’, localtime($3))”);

###################################################
# Encode with Handbrake. 
# Change to Output path is needed here for your setup. 
# Also you can remove -X 720 from the command to keep the full resolution. 
# I’m scaling to max width 720 (480P) as my server is only running an Intel 
# Core-2-Duo and doing this affords me about 60fps encoding speed. 
# Which is a 1:1 on 720P content (1hr show takes an hour to encode).
###################################################

HandBrakeCLI -i “$1″ -o “/path/to/output/folder/$2.$t.MP4″ -X 720 –preset=”Normal”

#delete original file after transcoding.
rm -f $1

******************************************************************

One downfall to this is that since the last line deletes the original recording without updating the TVHeadend database the next time you login to TVHeadend it will show the recording as a failed recording with a missing file. You could of course delete that line and keep the original file too.


Replies (1)

RE: Post Processing with Handbrake - Added by Andrew Blank almost 10 years ago

I've been working on trying to improve this script and here's my attempt at trying to use an xmltv.xml from mc2xml to get the original Air Date of a program in the name of the transcoded file for Plex/Kodi/XBMC to scan and recognize. Unfortunately it looks like for some tv shows I still need Series and Episode number and the output from mc2xml is useless in that regard. Here's the code though if someone finds it useful or if someone can extend it's function:

#!/bin/bash

# $1 Full path to recording (%f) /home/user/Videos/News.mkv
# $2 Basename of recording (%b) News.mkv
# $3 Channel name (%c) BBC world
# $4 Who created this recording (%C) user
# $5 Program title (%t) News
# $6 Program description (%d) News and stories...
# $7 Error message (%e) Aborted by user
# $8 Start time stamp of recording, UNIX epoch (%S)
# $9 Stop time stamp of recording, UNIX epoch (%E)

InputPath=/path/to/recordings
OutputPath=/path/to/output
Log=$InputPath/ConvertLog.txt
XmlPath=/path/to/xmltv.xml

echo    >> $Log
echo Input Path and File: $1 >> $Log
echo Channel: $3 >> $Log
echo Recording User: $4 >> $Log
echo Program title: $5 >> $Log
echo Program Description: $6 >> $Log
echo Error Message: $7 >> $Log
echo Input Start Time: $8 >> $Log
echo Input End Time: $9 >> $Log

#Convert Unix time-stamp to YYYYmmddHHMMSS set to variable $t
t=$(perl -e "use POSIX qw(strftime); print POSIX::strftime('%Y%m%d%H%M%S', localtime($8))");

StartTimeLog=$(perl -e "use POSIX qw(strftime); print POSIX::strftime('%Y-%m-%d.%H-%M-%S', localtime($8))");

echo Readable Start Time: $StartTimeLog >> $Log

echo Converted Start Time: $t >> $Log

#query original air date from xmltv.xml (Generated by MC2XML)
AirDate=$(xmllint --xpath "string(/tv/programme[title=\"$5\" and desc=\"$6\" and contains(@start, \"$t\")]/previously-shown/@start)" $XmlPath/xmltv.xml);

echo AirDate Query: xmllint --xpath '"string(/tv/programme[title=\"'$5'\" and desc=\"'$6'\" and contains(@start, \"'$t'\")]/previously-shown/@start)"' $XmlPath/xmltv.xml >> $Log
echo AirDate: $AirDate >> $Log

#Convert AirDate to date format for Plex/Kodi(XBMC) Naming convention.
AirDateConverted=$(echo ${AirDate:0:4}-${AirDate:4:2}-${AirDate:6:2});

echo AirDateConverted: $AirDateConverted >> $Log

#Check if AirDate Converted is not set

if [$AirDateConverted == '']
then
    #Convert Start Date of Recording for Plex/Kodi(XBMC) Naming convention.
    t=$(perl -e "use POSIX qw(strftime); print POSIX::strftime('%Y-%m-%d', localtime($8))");
    #Encode with Handbrake as root.
    sudo HandBrakeCLI -i "$1" -o "$OutputPath/$5.$t.MP4" -X 720 --preset="Normal" 

else
    #Encode with Handbrake as root.
    sudo HandBrakeCLI -i "$1" -o "$OutputPath/$5.$AirDateConverted.MP4" -X 720 --preset="Normal" 
fi
#delete original file after transcoding.
rm -f $1

I also changed the TVHeadend Post Processing command to export every option available in case I need it:

/home/hts/Convert %f %b %c %C %t %d %e %S %E

Notes:

Since the Start time of the recording is used, in matching the guide data from xmltv.xml, in order for this script to work there has to be 0 minutes of padding at the start of the recording. Down the road I'd like to add padding as a parameter and calculate the actual start time from there.

    (1-1/1)