├── README.md └── Convert.sh /README.md: -------------------------------------------------------------------------------- 1 | # Commercial-Clapboard 2 | 3 | This is a post processing BASH / Shell script designed for use with Tvheadend (tvheadend.org) to scan for commercials, transcode to H264 & cut commercials from the H264 file. The script also uses the xmltv output of mc2xml to name recordings for Plex/Kodi(XBMC) to scrape properly. 4 | 5 | The Post Processing Code in the Tvheadend configuration should be as follows: 6 | 7 | /path/to/script/Convert.sh %f %b %c %C %t %d %e %S %E 8 | 9 | Below is a description from Tvheadend of the output variables and their mappings in the script. 10 | 11 | * $1 Full path to recording (%f) /home/user/Videos/News.mkv 12 | * $2 Basename of recording (%b) News.mkv 13 | * $3 Channel name (%c) BBC world 14 | * $4 Who created this recording (%C) user 15 | * $5 Program title (%t) News 16 | * $6 Program description (%d) News and stories... 17 | * $7 Error message (%e) Aborted by user 18 | * $8 Start time stamp of recording, UNIX epoch (%S) 19 | * $9 Stop time stamp of recording, UNIX epoch (%E) 20 | 21 | source: https://tvheadend.org/projects/tvheadend/wiki/Digital_Video_Recorder_configuration 22 | 23 | This scripts was designed on Ubuntu Server 14.04 and requires the following installed: 24 | 25 | * Comskip Linux port from here: http://forum.kodi.tv/showthread.php?tid=150084 26 | * HandBrake CLI 27 | * xmllint (libxml2) 28 | * perl 29 | * mencoder 30 | * ffmpeg 31 | * mc2xml setup with TVHeadend as instructed here (https://tvheadend.org/boards/4/topics/10322) 32 | -------------------------------------------------------------------------------- /Convert.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # $1 Full path to recording (%f) /home/user/Videos/News.mkv 4 | # $2 Basename of recording (%b) News.mkv 5 | # $3 Channel name (%c) BBC world 6 | # $4 Who created this recording (%C) user 7 | # $5 Program title (%t) News 8 | # $6 Program description (%d) News and stories... 9 | # $7 Error message (%e) Aborted by user 10 | # $8 Start time stamp of recording, UNIX epoch (%S) 11 | # $9 Stop time stamp of recording, UNIX epoch (%E) 12 | 13 | #Change Paths below to suit your setup 14 | OutputPath=/Path/To/Output/$5 15 | Log=/home/Path/to/Log/ConvertLog.txt 16 | XmlPath=/Path/to/mc2xml 17 | #File Name and path without extension 18 | BaseFileName=${1%.*} 19 | 20 | echo " " >> $Log 21 | date >> $Log 22 | echo "Input Path and File: " $1 >> $Log 23 | echo "Channel: " $3 >> $Log 24 | echo "Recording User: " $4 >> $Log 25 | echo "Program title: " $5 >> $Log 26 | echo "Program Description: " $6 >> $Log 27 | echo "Error Message: " $7 >> $Log 28 | echo "Input Start Time: " $8 >> $Log 29 | echo "Input End Time: " $9 >> $Log 30 | who = whoami 31 | echo "Who Am I: " $who >> $Log 32 | 33 | #Convert Unix time-stamp to YYYY-MM-DD.HH-MM-SS set to variable $t 34 | t=$(perl -e "use POSIX qw(strftime); print POSIX::strftime('%Y%m%d%H%M%S', localtime($8))"); 35 | StartTimeLog=$(perl -e "use POSIX qw(strftime); print POSIX::strftime('%Y-%m-%d.%H-%M-%S', localtime($8))"); 36 | echo "Readable Start Time: " $StartTimeLog >> $Log 37 | echo "Converted Start Time: " $t >> $Log 38 | 39 | #query original air date from xmltv.xml (Generated by MC2XML) 40 | AirDate=$(xmllint --xpath "string(/tv/programme[title=\"$5\" and desc=\"$6\" and contains(@start, \"$t\")]/previously-shown/@start)" $XmlPath/xmltv.xml); 41 | echo "AirDate Query: xmllint --xpath 'string(/tv/programme[title=\"'"$5"'\" and desc=\"'"$6"'\" and contains(@start, \"'"$t"'\")]/previously-shown/@start)' $XmlPath/xmltv.xml" >> $Log 42 | echo "AirDate: " $AirDate >> $Log 43 | #Trim whitespace from Air Date Converted 44 | AirDate = "$AirDate" | tr -d ' ' 45 | 46 | #Set Handbrake Input File 47 | echo "HandBrake Input File: " $1 >> $Log 48 | #Set Handbrake Output File 49 | HandBrakeOutput="$BaseFileName.mp4" 50 | 51 | #Transcode to h264 file. 52 | echo "HandBrake Output Path: " $HandBrakeOutput >> $Log 53 | sudo HandBrakeCLI -i "$1" -o "$HandBrakeOutput" -e x264 --x264-preset ultrafast --x264-profile high --vfr -q 20 -X 720 -a 1,2,3 -E copy --audio-fallback mp3 -5 54 | echo "HandBrake Exit Code: " $? >> $Log 55 | 56 | #flag for commercials 57 | echo "***Comercial Flagging***" >> $Log 58 | echo "comskip" $1 >> $Log 59 | comskip "$1" # 2>&1 > $Log 60 | echo "comskip Exit code: " $? >> $Log 61 | echo "***End Commercial Flagging***" >> $Log 62 | 63 | #Set edl File Name 64 | EdlFile="$BaseFileName.edl" 65 | 66 | #allow time for edl to write. 67 | sleep 5 68 | 69 | echo "EDL File Name Used:" $EdlFile >> $Log 70 | #Check if edl file was generated by comskip. 71 | if [ -f "$EdlFile" ]; 72 | then 73 | #Cut Commercials 74 | echo "***Comercial Cutting***" >> $Log 75 | CommercialCutFile="$BaseFileName.m4v" 76 | sudo mencoder "$HandBrakeOutput" -edl "$EdlFile" -oac mp3lame -ovc copy -of avi -o "$CommercialCutFile" 77 | echo "Commercial Output File: " $CommercialCutFile >> $Log 78 | echo "mencoder exit code: " $? >> $Log 79 | echo "***End Comercial Cutting***" >> $Log 80 | FileToMove="$CommercialCutFile" 81 | else 82 | echo "Commercials not cut!" >> $Log 83 | FileToMove="$HandBrakeOutput" 84 | fi 85 | 86 | if [[ -z "$AirDate" ]]; 87 | then 88 | echo "Start Time of recording used in name" >> $Log 89 | #Convert Start Date of Recording for Plex/Kodi(XBMC) Naming convention. 90 | t=$(perl -e "use POSIX qw(strftime); print POSIX::strftime('%Y-%m-%d', localtime($8))"); 91 | #Set Output File 92 | OutputName="$5.$t.MP4" 93 | else 94 | echo "Previously Aired date from xmltv.xml used in name" >> $Log 95 | #Convert AirDate to date format for Plex/Kodi(XBMC) Naming convention. 96 | AirDateConverted=$(echo ${AirDate:0:4}-${AirDate:4:2}-${AirDate:6:2}); 97 | #Set Output File 98 | OutputName="$5.$AirDateConverted.MP4" 99 | fi 100 | 101 | echo "Checking for Output Folder: " $OutputPath >> $Log 102 | #Check if folder exists 103 | if [ ! -d "$OutputPath" ]; 104 | then 105 | echo "Output Folder Created: " $OutputPath >> $Log 106 | #if not make new folder 107 | mkdir "$OutputPath" 108 | fi 109 | #move transcoded file to Output 110 | mv "$FileToMove" "$OutputPath/$OutputName" 111 | 112 | 113 | #delete files after transcoding. 114 | if [ -f "$BaseFileName".mp4 ]; 115 | then 116 | rm -f "$BaseFileName".mp4 117 | fi 118 | 119 | #if [ -f "$BaseFileName".edl ]; 120 | #then 121 | # rm -f "$BaseFileName".edl 122 | #fi 123 | 124 | if [ -f "$BaseFileName".log ]; 125 | then 126 | rm -f "$BaseFileName".log 127 | fi 128 | 129 | if [ -f "$BaseFileName".txt ]; 130 | then 131 | rm -f "$BaseFileName".txt 132 | fi 133 | 134 | if [ -f "$BaseFileName".logo.txt ]; 135 | then 136 | rm -f "$BaseFileName".logo.txt 137 | fi 138 | 139 | if [ -f "$BaseFileName".m4v ]; 140 | then 141 | rm -f "$BaseFileName".m4v 142 | fi 143 | --------------------------------------------------------------------------------