Skip to content

Instantly share code, notes, and snippets.

@fergbrain
Created February 9, 2022 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fergbrain/23687b285f2479145eba891ccdac99ef to your computer and use it in GitHub Desktop.
Save fergbrain/23687b285f2479145eba891ccdac99ef to your computer and use it in GitHub Desktop.
Plex Post Processing
#!/bin/bash
#set path='$PATH:/usr/local/bin'
lockFile='/tmp/dvrProcessing.lock'
origFile="$1"
tmpFile="$1.tmp"
tmpEncode="$1.mkv"
tmpSrt="$1.srt"
dvrPostLog='/tmp/dvrProcessing.log'
comcutPath='/opt/comchap/comskip'
#Wait if post processing is already running
if [ -f $lockFile ]; then
echo "'$lockFile' exists, sleeping processing of '$origFile'" | tee $dvrPostLog
exit 1
fi
#Create lock file to prevent other post-processing from running simultaneously
echo "Creating lock file for processing '$origFile'" | tee -a $dvrPostLog
touch $lockFile
#Mark and cut commercials
echo "cut from '$origFile'" | tee -a $dvrPostLog
/opt/comchap/comcut --lockfile=/tmp/comchap.lock --comskip=/opt/comskip/comskip --comskip-ini=/opt/comchap/comskip.ini "$origFile"
#Pull CC from file to SRT file
echo "Pulling Closed captions from '$origFile' to SRT file" | tee -a $dvrPostLog
ccextractor "$origFile" -o "$tmpSrt"
#Encode file to H.264 with mkv container using ffmpeg and mux in CC srt
echo "Re-encoding '$origFile' to MKV file ($tmpEncode) while adding cc data ($tempSrt)" | tee -a $dvrPostLog
/usr/local/bin/ffmpeg -hwaccel cuda -hwaccel_output_format cuda -extra_hw_frames 10 -i "$origFile" -i "$tmpSrt" -c:a copy -c:v h264_nvenc -c:s copy -movflags +faststart -map 0 -map 1 "$tmpEncode"
#Remove SRT file
echo "Remove SRT file" | tee -a $dvrPostLog
rm -f "$tmpSrt"
#Rename first transcoded file to temp file in case no subtitles
echo "Rename transcoded file to tmp file" | tee -a $dvrPostLog
mv -f "$tmpEncode" "$tmpFile"
#Overwrite original ts file with the transcoded file
echo "Removing '$origFile'" | tee -a $dvrPostLog
mv -f "$tmpFile" "$origFile"
#Remove lock file
echo "Done processing '$origFile' removing lock" | tee -a $dvrPostLog
rm $lockFile
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment