I've been using Plex DVR the last couple months, testing the Plex Pass and Beta capabilities, and figuring ways to make it a solid solution for me and my family. The DVR aspect works well on the latest Beta release 1.10.0.4523, and the GUI is very simple to use. Setting up recordings and having them auto-added to my library works almost like magic.
But amidst the magic, I had some requirements that took more than out-of-the-box configuration. My two requirements were
- Remove commercials
- Reduce the final file size while preserving quality
Through lots of trial and error (lots), I was able to provide Plex with a script that takes care of both of these requirements. This script is provided to Plex via its path on the server in the DVR settings of the web console. Here, you can see I have added the script plex_pp.sh
The Plex Pass beta build mentioned above introduced commercial cutting natively within the Plex GUI. For those just starting out with Plex DVR, this is a fantastic addition they've added, and will greatly reduce the technical challenges of implementing it yourself. This newly built-in feature is still, however, in beta and may come with some unwanted bugs.
This script is a combination of information found online and tweaks for my needs (my household is mostly Apple products, hence the transcoding presets). All of this is maintained on my GitHub page, so check there for the latest bits.
My Environment (not earth shattering by ANY means)
- PMS (latest Plex Pass Beta) running on a virtual Ubuntu 16.04
- 4vCPU / 2GB RAM
- AMD A-10 6800k APU in host running Windows 10 Pro
- HDHomeRun Connect Tuner
Prerequisites
- Comcut
- Comskip
- ffmpeg
- Handbrake-CLI
This script is used to run comcut on completed Plex DVR recordings, then transcode them using handbrake with the Apple TV 1080p preset.
Original inspiration for this script came from this post
- Be sure to change permissions on the .sh file, something like chmod 777 or u+x
- Edit the script to put the paths to your Comcut and Handbrake-CLI locations. I've included mine as examples within the script
--
#! /bin/bash
#
# Plex DVR Postprocessing
# Version 0.0.1
# twitter.com/thatvirtualboy
# www.thatvirtualboy.com
#
# FIRST, RUN COMCUT TO REMOVE COMMERCIALS, THEN TRANSCODE AND COMPRESS
lockFile='/tmp/dvrProcessing.lock'
inFile="$1"
tmpFile="$1.mp4"
dvrPostLog='/tmp/dvrProcessing.log'
time=`date '+%Y-%m-%d %H:%M:%S'`
handbrake=/PATH/TO/YOUR/INSTALL (mine is /usr/bin/HandBrakeCLI)
cut=/PATH/TO/YOUR/COMCUT/INSTALL (mine is /home/ryan/comchap/comcut)
echo "'$time' Plex DVR Postprocessing script started" | tee $dvrPostLog
# Check if post processing is already running
while [ -f $lockFile ]
do
echo "'$time' $lockFile' exists, sleeping processing of '$inFile'" | tee -a $dvrPostLog
sleep 10
done
# Create lock file to prevent other post-processing from running simultaneously
echo "'$time' Creating lock file for processing '$inFile'" | tee -a $dvrPostLog
touch $lockFile
# Run comcut
echo "'$time' Comcut started on '$inFile'" | tee -a $dvrPostLog
$cut "$inFile"
# Encode file to MP4 with handbrake-cli
echo "'$time' Transcoding started on '$inFile'" | tee -a $dvrPostLog
$handbrake -i "$inFile" -o "$tmpFile" --preset="Apple 1080p30 Surround" --encoder-preset="veryfast" -O
# Overwrite original ts file with the transcoded file
echo "'$time' File rename started" | tee -a $dvrPostLog
mv -f "$tmpFile" "$inFile"
#Remove lock file
echo "'$time' All done! Removing lock for '$inFile'" | tee -a $dvrPostLog
rm $lockFile
exit 0
--
Once the recording is complete, the script will kickoff and get to work on the file before adding it to the Plex library. While the script is running, you'll see that the recording sits at a 100% complete status until it's done.
If you have two shows recording back-to-back, this script will also process one recording at a time, to not tax your system.
I hope you find this useful. Thanks for reading!
I copied this, edited to my HandbrakeCLI path, commented out the com skip stuff and am finding out that the resulting file seems to be still named with the .TS extension and Plex won't play it properly, however if I manually rename the file to .MP4 plex plays it fine I'm not sure why it's not renaming the file
ReplyDeleteI know this is late, change
Deletemv -f "$tmpFile" "$inFile"
to
mv -f "$tmpFile" `printf "%s " $inFile:t:r:r`.mp4
rm "$inFile"
Thanks for this!! I haven't tried it yet, but thank you for giving me some kind of information about what the "postprocessing script" should look like, which Plex.TV fails to do (at least a search there turns up nothing but forum questions). In fact, they don't even tell you what LANGUAGE the script should be in, which is not obvious if this isn't the type of scripting you normally do (or if you haven't scripted at all before). So thanks!!
ReplyDeleteThis comment has been removed by the author.
ReplyDeletePlayed around with the code. This is what I ended up doing to get the rename to work correctly. I am not doing comskip.
ReplyDeletemv -f "$tmpFile" "${inFile%.*}.mp4"
rm "$inFile"