Emby

From Hurlster Wiki
Jump to navigation Jump to search

DVR Script

Live tv post processing script
Emby Settings (Advanced)
Post-processing application -> /bin/bash
Post-processing command line arguments -> /usr/local/sbin/post.sh "{path}"

  • /usr/local/sbin/post.sh
#!/bin/bash
#Set these values for your setup
tmpBase=/tmp/                           # Where recordings are to be moved for processing
comProc=comcut                          # comcut or comchap for cut vs chapter marks
comskip=/usr/bin/comskip                # In case you put it somewhere emby won't look
ffmpegProc=/usr/bin/ffmpeg              # Use system ffmpeg if run manually
svcUser=emby                            # Who is the emby service running as
tgtRatio=13                             # H265 w/comcut 18~20% of original, H264 20~22%
                                        # Pick a number just under the target
origFile="$1"
tmpFile="$origFile.tmp"
tgtFile="${origFile%.*}.mp4"
tmpEncode="$origFile.mp4"
tmpEncode2="$origFile.2.mp4"
tmpSrt="$origFile.srt"
dvrPostLog='/var/log/dvrProcessing.log'
dvrLockLog='/var/log/dvrLock.log'
lockFile='/tmp/dvrProcessing.lock'

failed=1
((tryCount=3))

#Check to see if the emby server called this and use the correct ffmpeg
if [ "$USER" = "$svcUser" ];
then
        echo "Running inside of emby as $USER" | tee -a $dvrPostLog
        ffmpegProc=/opt/emby-server/bin/ffmpeg
        #ffmpegProc=/usr/bin/ffmpeg
        echo -e "Moving $origFile to $tmpBase" | tee -a $dvrPostLog
        mv "$origFile" "$tmpBase"

        tgtTemp=`echo $origFile |sed "s|.*\/\(.*\)$|\1|"`
#        origDir=`echo $origFile |sed "s|\(.*\)$tgtTemp$|\1|"`
        origDir=$(dirname "$origFile")
        origFile="$tmpBase$tgtTemp"
        tmpFile="$origFile.tmp"
        tgtFile="${origFile%.*}.mp4"
        tmpEncode="$origFile.mp4"
        tmpEncode2="$origFile.2.mp4"
        tmpSrt="$origFile.srt"

fi

#Get Source file size
origFilesize=`stat -c %s "$origFile"`

#Wait if post processing is already running
while [ -f $lockFile ] ;
do
    echo "`date '+%Y-%m-%d %H:%M:%S'` '$lockFile' exists, sleeping processing of '$origFile'" | tee $dvrLockLog
    sleep 60
done

#Create lock file to prevent other post-processing from running simultaneously
echo "`date '+%Y-%m-%d %H:%M:%S'` Creating lock file for processing '$origFile'" | tee -a $dvrPostLog
touch $lockFile

#Mark and cut commercials
if [ "$comProc" = "comcut" ];
then
        echo "`date '+%Y-%m-%d %H:%M:%S'` Cut commercials from '$origFile'" | tee -a $dvrPostLog
        $comProc --lockfile=/tmp/comchap.lock --comskip-ini=/etc/comskip.ini --comskip=$comskip --comskip=$comskip "$origFile"
fi

#Pull CC from file to SRT file
#echo "`date '+%Y-%m-%d %H:%M:%S'` Pulling Closed captions from '$origFile' to SRT file" | tee -a $dvrPostLog
#/usr/bin/ccextractor "$origFile" -o "$tmpSrt"

#Encode file to H.265/HEVC with NVIDIA HW as mp4 using ffmpeg
echo "`date '+%Y-%m-%d %H:%M:%S'` Re-encoding '$origFile' to mp4 file" | tee -a $dvrPostLog
while [ $tryCount -ne 0 ]
do
         echo -e "$ffmpegProc -nostdin -hide_banner -hwaccel_output_format cuda -i "$origFile" -ignore_unknown -acodec copy -scodec copy -c:v hevc_nvenc -preset medium "$tgtFile"" | tee -a $dvrPostLog
         #$ffmpegProc -nostdin -hide_banner -hwaccel_output_format cuda -i "$origFile" -ignore_unknown -acodec copy -scodec copy -c:v hevc_nvenc -preset medium "$tgtFile"
         $ffmpegProc -nostdin -hide_banner -hwaccel_output_format cuda -i "$origFile" -ignore_unknown -acodec copy -scodec copy -c:v hevc_nvenc -preset medium "$tgtFile"

        if [[ $? -ne 0 || ! -s "$tgtFile" ]];
        then
                rm -f "$tgtFile"
                echo "Trying $tryCount more time(s)..." | tee -a $dvrPostLog
                ((tryCount=tryCount-1))
                sleep 15
        else
                ((tryCount=0))
                failed=0
        fi
done

if [ $failed -eq 1 ];
then
        echo "`date '+%Y-%m-%d %H:%M:%S'` Re-encoding '$origFile' to mp4 file failed!" | tee -a $dvrPostLog
        echo "`date '+%Y-%m-%d %H:%M:%S'` Moving "$origFile"* to "$origDir" " | tee -a $dvrPostLog
        mv   "$origFile" "$origDir"     #Conversion failed

        #Remove lock file
        echo "`date '+%Y-%m-%d %H:%M:%S'` Done processing '$origFile' removing lock" | tee -a $dvrPostLog
        rm -f $lockFile

        exit 1
fi

#Trim off first minute
#echo "`date '+%Y-%m-%d %H:%M:%S'` Remove first 60 sec of file" | tee -a $dvrPostLog
#/opt/emby-server/bin/ffmpeg -ss 00:01:00 -i "$tmpEncode" -vcodec copy -acodec copy -scodec copy "$tmpEncode2"

#Remove SRT file
#echo "`date '+%Y-%m-%d %H:%M:%S'` Remove SRT file" | tee -a $dvrPostLog
#rm -f "$tmpSrt"


#Calculate mp4 size
if [ -f "$tgtFile" ];
then
        newFilesize=`stat -c %s "$tgtFile"`
        mp4ratio=$(( 100*"$newFilesize"/"$origFilesize" ))
fi

#Delete .ts file h265 mp4 is typically 18~20% original
if [[ $failed -eq 0 && $mp4ratio -ge $tgtRatio ]];
then
        #Mark commercials as chapters, must be done after conversion to mp4
        if [ "$comProc" = "comchap" ];
        then
                echo "`date '+%Y-%m-%d %H:%M:%S'` cut from '$origFile'" | tee -a $dvrPostLog
                mv "$tgtFile" "$tgtFile"-tmp.mp4
                $comProc --verbose --lockfile=/tmp/comchap.lock --comskip-ini=/etc/comskip.ini "$tgtFile"-tmp.mp4  > /tmp/comchap.log
                #Comchap seems to have a knack for minor corruption that prtevents playing. Quick repair
                $ffmpegProc -i "$tgtFile"-tmp.mp4 -c copy "$tgtFile"
                rm -f "$tgtFile"-tmp.mp4
        fi

        #mv   "$origFile" "$origFile"-orig | tee -a $dvrPostLog #backup copy

        if [ "$USER" = "$svcUser" ];            #Don't move Season/series if running manually
        then
                echo "`date '+%Y-%m-%d %H:%M:%S'` Moving "$tgtFile" to "$origDir/" " | tee -a $dvrPostLog
                #mv "$origFile-orig" "$origDir" | tee -a $dvrPostLog
                mv "$tgtFile" "$origDir" | tee -a $dvrPostLog
                echo -e "Removing $origFile" | tee -a $dvrPostLog
                rm "$origFile" | tee -a $dvrPostLog
        fi


fi

#Remove lock file
echo "`date '+%Y-%m-%d %H:%M:%S'` Done processing '$origFile' removing lock" | tee -a $dvrPostLog
rm -f $lockFile

exit 0