I've written these scripts and wish to share them with others and would welcome any improvements that people suggest. There are lots of other scripts around but none of them fully met what I was trying to achieve, this was....
1. Leave mythTV recordings fully untouched so if a mistake in a cutlist was found, I could rerun the script to fix issues.
2. Create a folder and name structure that is Kodi compatible.
3. Compress the file down to a mp4 file so the raspberry pi2 does not need a mp2 licence for decoding, remove interlacing and to drop the file size down.
4. If the NAS was offline when the script was running it leaves the file already compressed in a fallback folder to save the time that handbrake takes to run.
5. Be written 100% in shell commands and kept as simple as possible with comments to help others modify it to their needs. Just comment out any lines you wish to skip.
You do not need a NAS, you can use this to export to MP4 in a friendly name format to any folder. It does not touch your database or replace the mythtv recordings with lower quality.
Why two scripts?
I run one script for TV shows because some shows would end up with the same file name if treated the same way as a movie. The TV Show script will add the recording time stamp after the shows name if it is not part of a known series. An example is 'Todays News - 20160324102800.mp4'
This way if you record a show that has the same name everyday it will keep them all and they will not get overwritten as the timestamp is in the filename.
The movie script will output a file like 'Shrek.mp4' with no time stamp so if you record the same movie you will overwrite the old one and not end up with multiple copies. Both scripts will handle TV shows that will output as 'Lost - s1e1.mp4' and either script can be used.
I would like to merge both into the same script but currently I can not see a way to do this without reading the database directly or having a new feature added to mythtv.
TV Shows script
Code: Select all
#!/bin/sh
# Script written by 'Skinah'.
# This script will export a mythtv recording to a folder in a friendly readable name that is
# compatible with KODI. It will honor cutlists and also use handbrake to compress recordings
# to MP4 to save HDD space.
#
# It now supports lossless cutting of MP4 HD footage as well as MPEG2.
# All of this is done without changing Mythtv's database or recordings, this way if a
# mistake is found in the cutting of the recording then before myth expires the recording, we can
# rerun the job after adjusting the cutlist manually.
#
############### To install do the following #######################
# 1. Add this next line to the mythbackend setup for a user job
# /pathtoscript/TVShowToNAS.sh "%CHANID%" "%STARTTIMEUTC%" "%TITLE%" "%SUBTITLE%" "%SEASON%" "%EPISODE%" "%FILE%" "%DIR%"
#
# 2. Save this as a plain text file with .sh extention making sure it matches what you used in the last step.
# 3. Set the file to be executable.
# 4. Setup the script by editing the below text.
# 5. Install handbrake and any other requirements for MP4 source recordings.
#
# to install handbrake use these commands or visit the handbrake website.
# sudo add-apt-repository ppa:stebbins/handbrake-releases
# sudo apt-get update
# sudo apt-get install handbrake-gtk handbrake-cli
#
# To do lossless cutting with MP4 recordings do the following to install the requirements. MP2 does not need these.
# Visit https://mkvtoolnix.download/downloads.html and add the software sources for your linux build and the signed key.
# Any problems with mp4 cuts could be caused by an old version of mkvtoolnix so make sure you follow the above line.
# sudo apt-get install mkvtoolnix mkvtoolnix-gui
# sudo apt-get install bc
############# Section for Setting up script and changing options ########################
# path to MythTV transcoding tools, tested with mythbuntu 14.04
INSTALLPREFIX=/usr/bin
#If your HD channel is not 25 frames/sec then edit this with the value. Only used for h264 cuts.
fps=25
#Quality Settings, a value of 0 will give lossless cuts and skip handbrake encoding.
#A lower number will mean higher video quality and larger file size.
TVShowQuality=25
# Where your finished videos will be placed. This can be a NAS drive that is mounted in etc/fstab
# Mythtv user must have write permissions for this location.
# If you want movies and tv shows to go to the same place, just make both of these the same.
MOVIEOUTDIR="/mnt/nas/Movies"
TVOUTDIR="/mnt/nas/TV Shows"
# If the move command fails due to the NAS not being mounted, the file will remain here so as not to waste the Handbrake time.
# Mythtv user must have write permissions for this location.
# If you do not have a NAS, think of this as a working aka temp folder.
FALLBACKDIR="/home/matt/Videos"
######## Leave everything below alone unless you know how to mod and debug #############
# Store the variables that the userjob passes on to this shell script
TVTITLE=$3
TVSUBTITLE=$4
SeasonEpisode="s$5e$6"
MYTHRECORDINGDIR=$8
# First clean up any weird tags that the TV channel adds.
# These helped when Shepperd was not setup for XMLTV in Australia.
TVTITLE=`echo "$TVTITLE" | sed 's/Movie Premiere: //g' | sed 's/- Season Premiere//g' | sed 's/Movie: //g' | sed 's/://g' | sed 's/?//g' | sed 's/,//g'`
TVSUBTITLE=`echo "$TVSUBTITLE" | sed 's/\[RPT\] //g' | sed 's/(R) //g' | sed 's/ - //g' | sed 's/(G) //g' | sed 's/(AV) //g' | sed 's/(M) //g' | sed 's/(PG) //g' | sed 's/\[CC\] //g' | sed 's/(MA) //g'`
# clean up the title and subtitle strings by deleting any non-standard characters that can cause issues in file names
TVTITLE=`echo "$TVTITLE" | sed 's/://g' | sed 's/?//g' | sed 's/,//g' | sed 's/\///g'`
TVSUBTITLE=`echo "$TVSUBTITLE" | sed 's/://g' | sed 's/?//g' | sed 's/,//g' | sed 's/\///g'`
# print the following for logging purposes
echo "The line below records the file details of the last run." > $FALLBACKDIR/ScriptsLastRun.log
echo "ChanID_StartTimeUTC-Title-SeasonAndEpisodeNumber-Subtitle" >> $FALLBACKDIR/ScriptsLastRun.log
echo "$1_$2-$TVTITLE-$SeasonEpisode-$TVSUBTITLE" >> $FALLBACKDIR/ScriptsLastRun.log
echo "DIR/Filename" >> $FALLBACKDIR/ScriptsLastRun.log
echo "$8/$7" >> $FALLBACKDIR/ScriptsLastRun.log
# play nice with other processes
MYPID=$$
renice 19 $MYPID
ionice -c 3 -p $MYPID
#Check to see if we already have a cutlist before we overwrite any manually created skips, otherwise create new cutlist.
if [ -z "`$INSTALLPREFIX/mythutil --chanid "$1" --starttime "$2" --getcutlist | grep Cutlist | sed 's/Cutlist: $//'`" ]; then
echo "no cutlist found....generating new cutlist"
$INSTALLPREFIX/mythutil --chanid "$1" --starttime "$2" --gencutlist
fi
$INSTALLPREFIX/mythutil --chanid "$1" --starttime "$2" --getcutlist >> $FALLBACKDIR/ScriptsLastRun.log
# Try to remove the cutlist without transcoding so as not to lose quality. This fails on MP4 files as its not yet supported by mythtranscode so we must use another method.
echo "Using mythtranscode to remove cutllist after first creating a backup." >> $FALLBACKDIR/ScriptsLastRun.log
#backup recording as a workaround to a bug in 0.28
#cp $MYTHRECORDINGDIR/$7 $FALLBACKDIR/$1_$2.bup >> $FALLBACKDIR/ScriptsLastRun.log
# Remove the cutlist without transcoding so as not to lose quality.
$INSTALLPREFIX/mythtranscode --chanid "$1" --starttime "$2" --mpeg2 --honorcutlist -o "$FALLBACKDIR/$TVTITLE-$2.mpg" >> $FALLBACKDIR/ScriptsLastRun.log
############## Work out if MP4 or MP2 code needs to run ###############
#check exit code
if [ $? = 0 ]
then
echo "Lossless transcode on MP2 using mythtranscode worked fine" >> $FALLBACKDIR/ScriptsLastRun.log
# next two lines are for a work around to a bug.
#cp $FALLBACKDIR/$1_$2.bup $MYTHRECORDINGDIR/$7 >> $FALLBACKDIR/ScriptsLastRun.log
#rm $FALLBACKDIR/$1_$2.bup >> $FALLBACKDIR/ScriptsLastRun.log
else
#rm $FALLBACKDIR/$1_$2.bup >> $FALLBACKDIR/ScriptsLastRun.log
echo "File must be a mp4 recording as we can not do lossless transcoding with mythtranscode" >> $FALLBACKDIR/ScriptsLastRun.log
CUTLIST=`$INSTALLPREFIX/mythutil --chanid "$1" --starttime "$2" --getcutlist | grep Cutlist | sed 's/Cutlist: //' | \
sed 's/-/ /g' | sed 's/^\|$/-/g' | sed 's/,/-/g'`
echo "The cutlist is:" >> $FALLBACKDIR/ScriptsLastRun.log
echo $CUTLIST >> $FALLBACKDIR/ScriptsLastRun.log
keepframes=""
keepsecond=""
clipcount=0
for i in ${CUTLIST}
do
start=`echo $i | sed 's/ //g' | sed 's/^\(.*\)-.*$/\1/'`
end=`echo $i | sed 's/ //g' | sed 's/^.*-\(.*\)$/\1/'`
#if $start is empty, deal with it
if [ -z $start ]; then
start=0
fi
#If the end is empty then guess how long the file is.
if [ -z $end ]; then
end=500000
fi
#if the start and end are the same then skip the cut
if [ $start -eq $end ]; then
continue
fi
#if the clipcount is greater than 0 we need to add ,+
if [ $clipcount -gt 0 ]; then
keepframes="$keepframes,+"
keepsecond="$keepsecond,+"
fi
keepframes="$keepframes$start-$end"
clipcount=$((clipcount+1))
echo "clipcount=$clipcount" >> $FALLBACKDIR/ScriptsLastRun.log
echo "Using frames:" >> $FALLBACKDIR/ScriptsLastRun.log
echo "Start=$start-End=$end" >> $FALLBACKDIR/ScriptsLastRun.log
#convert start into time in seconds (divide frames by frames per second)
start=$(echo "scale=2; $start / $fps" | bc -l)
#Use the program bc to create a floating point value in seconds with 2 decimal places.
end=$(echo "scale=2; $end / $fps" | bc -l)
echo "Using Seconds:" >> $FALLBACKDIR/ScriptsLastRun.log
echo "Start=$start-End=$end" >> $FALLBACKDIR/ScriptsLastRun.log
keepsecond="$keepsecond$(date -d@$start -u +%H:%M:%S)-"
keepsecond="$keepsecond$(date -d@$end -u +%H:%M:%S)"
done
echo "The keepframes string contains:$keepframes" >> $FALLBACKDIR/ScriptsLastRun.log
echo "The keepsecond string contains:$keepsecond" >> $FALLBACKDIR/ScriptsLastRun.log
echo "Using mkvmerge to create a single file without pieces from the cutlist" >> $FALLBACKDIR/ScriptsLastRun.log
mkvmerge --split parts:$keepsecond -o "$FALLBACKDIR/$TVTITLE-$2.mkv" "$MYTHRECORDINGDIR/$7" >> $FALLBACKDIR/ScriptsLastRun.log
#mkvmerge --split parts-frames:$keepframes -o "$FALLBACKDIR/$TVTITLE-$2.mkv" "$MYTHRECORDINGDIR/$7" >> $FALLBACKDIR/ScriptsLastRun.log
if [ "$SeasonEpisode" = "s0e0" ] && ["$TVSUBTITLE" = ""]
then
mkdir -p "$TVOUTDIR/$TVTITLE"
if [ $TVShowQuality = 0 ]
then
mv -u "$FALLBACKDIR/$TVTITLE-$2.mkv" "$TVOUTDIR/$TVTITLE/$TVTITLE - $2.mkv"
else
/usr/bin/HandBrakeCLI -i "$FALLBACKDIR/$TVTITLE-$2.mkv" -o "$FALLBACKDIR/$TVTITLE - $2.m4v" -e x264 -q $TVShowQuality -O -r 30 --pfr -x ref=6:bframes=5:vbv-maxrate=62000:vbv-bufsize=62000 -X 720 --decomb --loose-anamorphic --modulus 2 --x264-tune film --x264-preset medium --h264-profile high --h264-level 4.1 -a 1,1 -E copy:ac3,faac -B auto,160 -R auto,auto -6 auto,dpl2 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 -f mp4
mv -u "$FALLBACKDIR/$TVTITLE - $2.m4v" "$TVOUTDIR/$TVTITLE/$TVTITLE - $2.m4v"
rm "$FALLBACKDIR/$TVTITLE-$2.mkv"
fi
rm "$FALLBACKDIR/$TVTITLE-$2.mpg"
exit 0
else
mkdir -p "$TVOUTDIR/$TVTITLE"
if [ $TVShowQuality = 0 ]
then
mv -u "$FALLBACKDIR/$TVTITLE-$2.mkv" "$TVOUTDIR/$TVTITLE/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.mkv"
else
/usr/bin/HandBrakeCLI -i "$FALLBACKDIR/$TVTITLE-$2.mkv" -o "$FALLBACKDIR/$TVTITLE - $SeasonEpisode.m4v" -e x264 -q $TVShowQuality -O -r 30 --pfr -x ref=6:bframes=5:vbv-maxrate=62000:vbv-bufsize=62000 -X 720 --decomb --loose-anamorphic --modulus 2 --x264-tune film --x264-preset medium --h264-profile high --h264-level 4.1 -a 1,1 -E copy:ac3,faac -B auto,160 -R auto,auto -6 auto,dpl2 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 -f mp4
mv -u "$FALLBACKDIR/$TVTITLE - $SeasonEpisode.m4v" "$TVOUTDIR/$TVTITLE/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.m4v"
rm "$FALLBACKDIR/$TVTITLE-$2.mkv"
fi
rm "$FALLBACKDIR/$TVTITLE-$2.mpg"
exit 0
fi
fi
######## End of MP4 only code ############
if [ "$SeasonEpisode" = "s0e0" ] && ["$TVSUBTITLE" = ""]
then
mkdir -p "$TVOUTDIR/$TVTITLE"
if [ $TVShowQuality = 0 ]
then
mv -u "$FALLBACKDIR/$TVTITLE-$2.mpg" "$TVOUTDIR/$TVTITLE/$TVTITLE - $2.mpg"
else
/usr/bin/HandBrakeCLI -i "$FALLBACKDIR/$TVTITLE-$2.mpg" -o "$FALLBACKDIR/$TVTITLE - $2.m4v" -e x264 -q $TVShowQuality -O -r 30 --pfr -x ref=6:bframes=5:vbv-maxrate=62000:vbv-bufsize=62000 -X 720 --decomb --loose-anamorphic --modulus 2 --x264-tune film --x264-preset medium --h264-profile high --h264-level 4.1 -a 1,1 -E copy:ac3,faac -B auto,160 -R auto,auto -6 auto,dpl2 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 -f mp4
mv -u "$FALLBACKDIR/$TVTITLE - $2.m4v" "$TVOUTDIR/$TVTITLE/$TVTITLE - $2.m4v"
rm "$FALLBACKDIR/$TVTITLE-$2.mpg"
fi
rm "$FALLBACKDIR/$TVTITLE-$2.mpg.map"
exit 0
else
mkdir -p "$TVOUTDIR/$TVTITLE"
if [ $TVShowQuality = 0 ]
then
mv -u "$FALLBACKDIR/$TVTITLE-$2.mpg" "$TVOUTDIR/$TVTITLE/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.mpg"
else
/usr/bin/HandBrakeCLI -i "$FALLBACKDIR/$TVTITLE-$2.mpg" -o "$FALLBACKDIR/$TVTITLE - $SeasonEpisode.m4v" -e x264 -q $TVShowQuality -O -r 30 --pfr -x ref=6:bframes=5:vbv-maxrate=62000:vbv-bufsize=62000 -X 720 --decomb --loose-anamorphic --modulus 2 --x264-tune film --x264-preset medium --h264-profile high --h264-level 4.1 -a 1,1 -E copy:ac3,faac -B auto,160 -R auto,auto -6 auto,dpl2 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 -f mp4
mv -u "$FALLBACKDIR/$TVTITLE - $SeasonEpisode.m4v" "$TVOUTDIR/$TVTITLE/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.m4v"
rm "$FALLBACKDIR/$TVTITLE-$2.mpg"
fi
rm "$FALLBACKDIR/$TVTITLE-$2.mpg.map"
exit 0
fi
This following script is for Movies. If it is run by mistake on a tv show, it will detect this and still handle tv shows that are part of a known series. It has better error logging than the previous script currently so get this one working first.
Code: Select all
#!/bin/sh
# Script written by 'Skinah'.
# This script will export a mythtv recording to a folder in a friendly readable name that is
# compatible with KODI. It will honor cutlists and also use handbrake to compress recordings
# to MP4 to save HDD space.
#
# It now supports lossless cutting of MP4 HD footage as well as MPEG2.
# All of this is done without changing Mythtv's database or recordings, this way if a
# mistake is found in the cutting of the recording then before myth expires the recording, we can
# rerun the job after adjusting the cutlist manually.
#
############### To install do the following #######################
# 1. Add this next line to the mythbackend setup for a user job
# /pathtoscript/sendtonas.sh "%CHANID%" "%STARTTIMEUTC%" "%TITLE%" "%SUBTITLE%" "%SEASON%" "%EPISODE%" "%FILE%" "%DIR%"
#
# 2. Save this as a plain text file with .sh extention making sure it matches what you used in the last step.
# 3. Set the file to be executable.
# 4. Setup the script by editing the below text.
# 5. Install handbrake and any other requirements for MP4 source recordings.
#
# to install handbrake use these commands or visit the handbrake website.
# sudo add-apt-repository ppa:stebbins/handbrake-releases
# sudo apt-get update
# sudo apt-get install handbrake-gtk handbrake-cli
#
# To do lossless cutting with MP4 recordings do the following to install the requirements. MP2 does not need these.
# Visit https://mkvtoolnix.download/downloads.html and add the software sources for your linux build and the signed key.
# Any problems with mp4 cuts could be caused by an old version of mkvtoolnix so make sure you follow the above line.
# sudo apt-get install mkvtoolnix mkvtoolnix-gui
# sudo apt-get install bc
############# Section for Setting up script and changing options ########################
# path to MythTV transcoding tools, tested with mythbuntu 14.04
INSTALLPREFIX=/usr/bin
#If your HD channel is not 25 frames/sec then edit this with the value. Only used for h264 cuts.
fps=25
#Quality Settings, a value of 0 will give lossless cuts and skip handbrake encoding.
#NOTE: If it is a movie and from a MP4 source it will be lossless regardless of this setting.
#A lower number will mean higher video quality and larger file size.
MovieQuality=21
TVShowQuality=25
# Where your finished videos will be placed. This can be a NAS drive that is mounted in etc/fstab
# Mythtv user must have write permissions for this location.
# If you want movies and tv shows to go to the same place, just make both of these the same.
MOVIEOUTDIR="/mnt/nas/Movies"
TVOUTDIR="/mnt/nas/TV Shows"
# If the move command fails due to the NAS not being mounted, the file will remain here so as not to waste the Handbrake time.
# Mythtv user must have write permissions for this location.
# If you do not have a NAS, think of this as a working aka temp folder.
FALLBACKDIR="/home/matt/Videos"
######## Leave everything below alone unless you know how to mod and debug #############
# Store the variables that the userjob passes on to this shell script
TVTITLE=$3
TVSUBTITLE=$4
SeasonEpisode="s$5e$6"
MYTHRECORDINGDIR=$8
# First clean up any weird tags that the TV channel adds.
# These help if you do not use a XMLTV grabber like Shepperd and use IET for guide data.
TVTITLE=`echo "$TVTITLE" | sed 's/Movie Premiere: //g' | sed 's/- Season Premiere//g' | sed 's/Movie: //g' | sed 's/://g' | sed 's/?//g' | sed 's/,//g'`
TVSUBTITLE=`echo "$TVSUBTITLE" | sed 's/\[RPT\] //g' | sed 's/(R) //g' | sed 's/ - //g' | sed 's/(G) //g' | sed 's/(AV) //g' | sed 's/(M) //g' | sed 's/(PG) //g' | sed 's/\[CC\] //g' | sed 's/(MA) //g'`
# clean up the title and subtitle strings by deleting any non-standard characters that can cause issues in file names
TVTITLE=`echo "$TVTITLE" | sed 's/://g' | sed 's/?//g' | sed 's/,//g' | sed 's/\///g'`
TVSUBTITLE=`echo "$TVSUBTITLE" | sed 's/://g' | sed 's/?//g' | sed 's/,//g' | sed 's/\///g'`
# print the following for logging purposes
echo "The line below records the file details of the last run." > $FALLBACKDIR/ScriptsLastRun.log
echo "ChanID_StartTimeUTC-Title-SeasonAndEpisodeNumber-Subtitle" >> $FALLBACKDIR/ScriptsLastRun.log
echo "$1_$2-$TVTITLE-$SeasonEpisode-$TVSUBTITLE" >> $FALLBACKDIR/ScriptsLastRun.log
echo "DIR/Filename" >> $FALLBACKDIR/ScriptsLastRun.log
echo "$8/$7" >> $FALLBACKDIR/ScriptsLastRun.log
# play nice with other processes
MYPID=$$
renice 19 $MYPID
ionice -c 3 -p $$
#Check to see if we already have a cutlist before we overwrite any manually created skips, otherwise create new cutlist.
if [ -z "`$INSTALLPREFIX/mythutil --chanid "$1" --starttime "$2" --getcutlist | grep Cutlist | sed 's/Cutlist: $//'`" ]; then
echo "no cutlist found....generating new cutlist" >> $FALLBACKDIR/ScriptsLastRun.log
$INSTALLPREFIX/mythutil --chanid "$1" --starttime "$2" --gencutlist >> $FALLBACKDIR/ScriptsLastRun.log
fi
$INSTALLPREFIX/mythutil --chanid "$1" --starttime "$2" --getcutlist >> $FALLBACKDIR/ScriptsLastRun.log
# Try to remove the cutlist without transcoding so as not to lose quality. This fails on MP4 files as its not yet supported by mythtranscode so we must use another method.
echo "Using mythtranscode to remove cutllist after first creating a backup." >> $FALLBACKDIR/ScriptsLastRun.log
#backup recording as a workaround to a bug in 0.28
#cp $MYTHRECORDINGDIR/$7 $FALLBACKDIR/$1_$2.bup >> $FALLBACKDIR/ScriptsLastRun.log
$INSTALLPREFIX/mythtranscode --chanid "$1" --starttime "$2" --mpeg2 --honorcutlist -o "$FALLBACKDIR/$TVTITLE-$2.mpg" >> $FALLBACKDIR/ScriptsLastRun.log
############## Work out if MP4 or MP2 code needs to run ###############
#check exit code
if [ $? = 0 ]
then
echo "Lossless transcode on MP2 using mythtranscode worked fine" >> $FALLBACKDIR/ScriptsLastRun.log
# next two lines are for a work around to a bug.
#cp $FALLBACKDIR/$1_$2.bup $MYTHRECORDINGDIR/$7 >> $FALLBACKDIR/ScriptsLastRun.log
#rm $FALLBACKDIR/$1_$2.bup >> $FALLBACKDIR/ScriptsLastRun.log
else
#rm $FALLBACKDIR/$1_$2.bup >> $FALLBACKDIR/ScriptsLastRun.log
echo "File must be a mp4 recording as we can not do lossless transcoding with mythtranscode" >> $FALLBACKDIR/ScriptsLastRun.log
CUTLIST=`$INSTALLPREFIX/mythutil --chanid "$1" --starttime "$2" --getcutlist | grep Cutlist | sed 's/Cutlist: //' | \
sed 's/-/ /g' | sed 's/^\|$/-/g' | sed 's/,/-/g'`
echo "The cutlist is:" >> $FALLBACKDIR/ScriptsLastRun.log
echo $CUTLIST >> $FALLBACKDIR/ScriptsLastRun.log
keepframes=""
keepsecond=""
clipcount=0
for i in ${CUTLIST}
do
start=`echo $i | sed 's/ //g' | sed 's/^\(.*\)-.*$/\1/'`
end=`echo $i | sed 's/ //g' | sed 's/^.*-\(.*\)$/\1/'`
#if $start is empty, deal with it
if [ -z $start ]; then
start=0
fi
#If the end is empty then guess how long the file is.
if [ -z $end ]; then
end=500000
fi
#if the start and end are the same then skip the cut
if [ $start -eq $end ]; then
continue
fi
#if the clipcount is greater than 0 we need to add ,+
if [ $clipcount -gt 0 ]; then
keepframes="$keepframes,+"
keepsecond="$keepsecond,+"
fi
keepframes="$keepframes$start-$end"
clipcount=$((clipcount+1))
echo "clipcount=$clipcount" >> $FALLBACKDIR/ScriptsLastRun.log
echo "Using frames:" >> $FALLBACKDIR/ScriptsLastRun.log
echo "Start=$start-End=$end" >> $FALLBACKDIR/ScriptsLastRun.log
#convert start into time in seconds (divide frames by frames per second)
start=$(echo "scale=2; $start / $fps" | bc -l)
#Use the program bc to create a floating point value in seconds with 2 decimal places.
end=$(echo "scale=2; $end / $fps" | bc -l)
echo "Using Seconds:" >> $FALLBACKDIR/ScriptsLastRun.log
echo "Start=$start-End=$end" >> $FALLBACKDIR/ScriptsLastRun.log
keepsecond="$keepsecond$(date -d@$start -u +%H:%M:%S)-"
keepsecond="$keepsecond$(date -d@$end -u +%H:%M:%S)"
done
echo "The keepframes string contains:$keepframes" >> $FALLBACKDIR/ScriptsLastRun.log
echo "The keepsecond string contains:$keepsecond" >> $FALLBACKDIR/ScriptsLastRun.log
echo "Using mkvmerge to create a single file without pieces from the cutlist" >> $FALLBACKDIR/ScriptsLastRun.log
mkvmerge --split parts:$keepsecond -o "$FALLBACKDIR/$TVTITLE-$2.mkv" "$MYTHRECORDINGDIR/$7" >> $FALLBACKDIR/ScriptsLastRun.log
#Script assumes if both season and episode is 0 then it must be a movie. Not the best but it works for me.
if [ "$SeasonEpisode" = "s0e0" ]
then
echo "Attempting to send file to the NAS now" >> $FALLBACKDIR/ScriptsLastRun.log
mkdir -p "$MOVIEOUTDIR/$TVTITLE" >> $FALLBACKDIR/ScriptsLastRun.log
mv -u "$FALLBACKDIR/$TVTITLE-$2.mkv" "$FALLBACKDIR/$TVTITLE.mkv" >> $FALLBACKDIR/ScriptsLastRun.log
mv -u "$FALLBACKDIR/$TVTITLE.mkv" "$MOVIEOUTDIR/$TVTITLE/$TVTITLE.mkv" >> $FALLBACKDIR/ScriptsLastRun.log
else
echo "Creating folder now:" >> $FALLBACKDIR/ScriptsLastRun.log
mkdir -p "$TVOUTDIR/$TVTITLE" >> $FALLBACKDIR/ScriptsLastRun.log
if [ $TVShowQuality = 0 ]
then
mv -u "$FALLBACKDIR/$TVTITLE-$2.mkv" "$FALLBACKDIR/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.mkv" #mv -ud in case move fails.
mv -u "$FALLBACKDIR/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.mkv" "$TVOUTDIR/$TVTITLE/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.mkv"
else
echo "Running Handbrake on mkv input file now as this is not a movie." >> $FALLBACKDIR/ScriptsLastRun.log
/usr/bin/HandBrakeCLI -i "$FALLBACKDIR/$TVTITLE-$2.mkv" -o "$FALLBACKDIR/$TVTITLE - $SeasonEpisode.m4v" -e x264 -q $TVShowQuality -O -r 30 --pfr -x ref=6:bframes=5:vbv-maxrate=62000:vbv-bufsize=62000 -X 720 --decomb --loose-anamorphic --modulus 2 --x264-tune film --x264-preset medium --h264-profile high --h264-level 4.1 -a 1,1 -E copy:ac3,faac -B auto,160 -R auto,auto -6 auto,dpl2 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 -f mp4 >> $FALLBACKDIR/ScriptsLastRun.log
echo "Attempting to send file to the NAS now" >> $FALLBACKDIR/ScriptsLastRun.log
mv -u "$FALLBACKDIR/$TVTITLE - $SeasonEpisode.m4v" "$FALLBACKDIR/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.m4v"
mv -u "$FALLBACKDIR/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.m4v" "$TVOUTDIR/$TVTITLE/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.m4v"
rm "$FALLBACKDIR/$TVTITLE-$2.mkv" #This file is not named right and is before handbrake ran.
fi
fi
#cleanup the 0 byte mpg file which is created even thou mythtranscode failed.
echo "Removing the temporary mpg file now:" >> $FALLBACKDIR/ScriptsLastRun.log
rm "$FALLBACKDIR/$TVTITLE-$2.mpg" >> $FALLBACKDIR/ScriptsLastRun.log
echo "Script ran to the very end." >> $FALLBACKDIR/ScriptsLastRun.log
exit 0
fi
######## End of MP4 code ############
#################### Code that processes mp2 files ###################
echo "Running Handbrake" >> $FALLBACKDIR/ScriptsLastRun.log
#Need a better way as the following assumes that it is a Movie if the season and episode is both 0
#Current solution is to run different script for TV Shows but left ability in script.
if [ "$SeasonEpisode" = "s0e0" ]
then
echo "Creating folder now:" >> $FALLBACKDIR/ScriptsLastRun.log
mkdir -p "$MOVIEOUTDIR/$TVTITLE" >> $FALLBACKDIR/ScriptsLastRun.log
if [ $MovieQuality = 0 ]
then
mv -u "$FALLBACKDIR/$TVTITLE-$2.mpg" "$FALLBACKDIR/$TVTITLE.mpg" >> $FALLBACKDIR/ScriptsLastRun.log
mv -u "$FALLBACKDIR/$TVTITLE.mpg" "$MOVIEOUTDIR/$TVTITLE/$TVTITLE.mpg" >> $FALLBACKDIR/ScriptsLastRun.log
else
/usr/bin/HandBrakeCLI -i "$FALLBACKDIR/$TVTITLE-$2.mpg" -o "$FALLBACKDIR/$TVTITLE-$2.m4v" -e x264 -q $MovieQuality -O -r 30 --pfr -x ref=6:bframes=5:vbv-maxrate=62000:vbv-bufsize=62000 -X 720 --decomb --loose-anamorphic --modulus 2 --x264-tune film --x264-preset medium --h264-profile high --h264-level 4.1 -a 1,1 -E copy:ac3,faac -B auto,160 -R auto,auto -6 auto,dpl2 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 -f mp4 >> $FALLBACKDIR/ScriptsLastRun.log
echo "Attempting to send file to the NAS now" >> $FALLBACKDIR/ScriptsLastRun.log
mv -u "$FALLBACKDIR/$TVTITLE-$2.m4v" "$FALLBACKDIR/$TVTITLE.m4v" >> $FALLBACKDIR/ScriptsLastRun.log
mv -u "$FALLBACKDIR/$TVTITLE.m4v" "$MOVIEOUTDIR/$TVTITLE/$TVTITLE.m4v" >> $FALLBACKDIR/ScriptsLastRun.log
fi
else
echo "Creating folder now:" >> $FALLBACKDIR/ScriptsLastRun.log
mkdir -p "$TVOUTDIR/$TVTITLE" >> $FALLBACKDIR/ScriptsLastRun.log
if [ $TVShowQuality = 0 ]
then
mv -u "$FALLBACKDIR/$TVTITLE-$2.mpg" "$FALLBACKDIR/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.mpg"
mv -u "$FALLBACKDIR/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.mpg" "$TVOUTDIR/$TVTITLE/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.mpg"
else
/usr/bin/HandBrakeCLI -i "$FALLBACKDIR/$TVTITLE-$2.mpg" -o "$FALLBACKDIR/$TVTITLE-$2.m4v" -e x264 -q $TVShowQuality -O -r 30 --pfr -x ref=6:bframes=5:vbv-maxrate=62000:vbv-bufsize=62000 -X 720 --decomb --loose-anamorphic --modulus 2 --x264-tune film --x264-preset medium --h264-profile high --h264-level 4.1 -a 1,1 -E copy:ac3,faac -B auto,160 -R auto,auto -6 auto,dpl2 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 -f mp4 >> $FALLBACKDIR/ScriptsLastRun.log
echo "Attempting to send file to the NAS now" >> $FALLBACKDIR/ScriptsLastRun.log
mv -u "$FALLBACKDIR/$TVTITLE-$2.m4v" "$FALLBACKDIR/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.m4v" >> $FALLBACKDIR/ScriptsLastRun.log
mv -u "$FALLBACKDIR/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.m4v" "$TVOUTDIR/$TVTITLE/$TVTITLE - $SeasonEpisode - $TVSUBTITLE.m4v" >> $FALLBACKDIR/ScriptsLastRun.log
fi
fi
echo "Cleaning up tempory files:" >> $FALLBACKDIR/ScriptsLastRun.log
rm "$FALLBACKDIR/$TVTITLE-$2.mpg" >> $FALLBACKDIR/ScriptsLastRun.log
rm "$FALLBACKDIR/$TVTITLE-$2.mpg.map" >> $FALLBACKDIR/ScriptsLastRun.log
echo "Script ran to the very end." >> $FALLBACKDIR/ScriptsLastRun.log
exit 0