So let me start with what "Working but needs help" means.
It means this script is
* flagging for commercials and outputting and .edl file
* Mencoder is reading the edl file and making cuts
* HandBrake CLI is reading the cut file and transcoding
* The File name also includes the original air date as grabbed from the xmltv file generated by mc2xml
What's not working is
* After being cut the audio and video are out of sync and the video gets jumpy where the original was not.
If someone knows a terminal video editor that can use an .edl aside from Mencoder I'm all ears.
Notes:
* If I don't run the recording through Mencoder then HandbrakeCLI transcodes the original file with commercials just fine with no sync issues. So there's something wrong with the Mencoder stage. I've also tried (without luck) to convert the Mpeg-TS to Mpeg-PS first with Mencoder and FFMpeg and rebuilding the index before comskip scanning to no avail.
* Some of the commands use sudo for permissions issues (bad I know). I setup auto sudo privilege for hts using visudo. Example can be found here:
https://tvheadend.org/d/1864-post-processing-problem-filesystem-sync
My System Info
* Ubuntu Server 14.04
* Intel Core 2 Duo
* Hauppauge HVR 1600
* Time Warner Cable QAM service
Pre-requisites to this script:
* Comskip Linux port from here:
http://forum.kodi.tv/showthread.php?tid=150084
* HandBrake CLI
* xmllint (libxml2)
* perl
* mencoder
* ffmpeg
* mc2xml setup with TVHeadend as instructed here (
https://tvheadend.org/d/1587-complete-setup-guide-for-tvheadend-ubuntu-hdhomerun-tuner-xbmc-and-north-american-program-guides)
TVHeadend Post Processing Command:
/home/hts/Convert %f %b %c %C %t %d %e %S %E
#!/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)
#My paths for example##
#WorkPath=/home/hts/postprocessing
#OutputPath includes show name
#with the date in the filename (part of this script)
#Plex can scrape thetvdb.com info :)
#OutputPath=/media/seagate/DVR/$5
#Log=/home/hts/logs/ConvertLog.txt
#XmlPath=/home/hts/mc2xml
#File Name and path without extension
#BaseFileName=${1%.*}
#end my paths example##
WorkPath=/path/for/handbrake/output
OutputPath=/path/to/final/output
Log=/home/path/to/log/file
XmlPath=/path/to/xmltv.xml
#File Name and path without extension
BaseFileName=${1%.*}
echo " " >> $Log
date >> $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
who = whoami
echo "Who Am I: " $who >> $Log
#Convert Unix time-stamp to YYYY-MM-DD.HH-MM-SS 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))");
FileNameTime=$(perl -e "use POSIX qw(strftime); print POSIX::strftime('%Y-%m-%d.%H-%M', 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
#Trim whitespace from Air Date Converted
AirDate = "$AirDate" | tr -d ' '
#flag for commercials
echo "***Comercial Flagging***" >> $Log
echo "comskip" $1 >> $Log
#attempts to convert to Mpeg-PS before comskip scan
#MpegFile=$BaseFileName.mpg
#ffmpeg -i "$1" -map 0:a -c:a copy -map 0:v -c:v copy "$MpegFile"
#sudo mencoder "$1" -forceidx -mc 0 -noskip -oac copy -ovc copy -of mpeg -o "$MpegFile"
#comskip "$MpegFile" # 2>&1 </dev/null >> $Log
#end attempts to convert to Mpeg-PS before comskip scan
comskip "$1" # 2>&1 </dev/null >> $Log
echo "comskip Exit code: " $? >> $Log
echo "***End Comercial Flagging***" >> $Log
EdlFile=$BaseFileName.edl
echo "EDL File Name Used:" $EdlFile >> $Log
#Check if edl file was generated
if [ -f $EdlFile ];
then
#Cut Commercials
echo "***Comercial Cutting***" >> $Log
CommercialCutFile=$BaseFileName.CommCut.mpg
sudo mencoder "$1" -edl "$EdlFile" -oac copy -ovc copy -of mpeg -o "$CommercialCutFile"
echo "Commercial Output File: " $CommercialCutFile >> $Log
echo "mencoder exit code: " $? >> $Log
echo "***End Comercial Cutting***" >> $Log
HandBrakeInput=$CommercialCutFile
else
#edl file is missing, encode original and output to log
echo "Commercials not cut!" >> $Log
HandBrakeInput=$1
fi
echo "HandBrake Input File: " $HandBrakeInput >> $Log
#Check if original Airdate was found in xmltv.xml
if [[ -z "$AirDate" ]];
then
#Airdate not found use today's date from recording start time.
echo "Start Time of recording used in name" >> $Log
#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))");
#Set Handbrake Output File
HandBrakeOutput=$workPath/$5.$t.MP4
else
#Original Airdate was found
echo "Previously Aired date from xmltv.xml used in name" >> $Log
#Convert AirDate to date format for Plex/Kodi(XBMC) Naming convention.
AirDateConverted=$(echo ${AirDate:0:4}-${AirDate:4:2}-${AirDate:6:2});
#Set Handbrake Output File
HandBrakeOutput=$WorkPath/$5.$AirDateConverted.MP4
fi
#Transcode to h264 file.
echo "HandBrake Output Path: " $HandBrakeOutput >> $Log
sudo HandBrakeCLI -i "$HandBrakeInput" -o "$HandBrakeOutput" -X 720 --preset="Normal"
echo "HandBrake Exit Code: " $? >> $Log
#If HandBrake was successful
if [ $? == 0 ];
then
echo "Checking for Output Folder: " $OutputPath >> $Log
#Check if folder exists
if [ ! -d "$OutputPath" ];
then
echo "Output Folder Created: " $OutputPath >> $Log
#if not make new folder
mkdir "$OutputPath"
fi
#move transcoded file to Output
mv "$HandBrakeOutput" "$OutputPath/"
fi
#delete files after transcoding.
rm -f $BaseFileName*