Tuesday, November 28, 2017

Plex DVR Postprocessing Script



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 my 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

  1. Remove commercials
  2. 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

  1. Comcut
    1. Comskip
    2. ffmpeg
  2. 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.

Shout out
Original inspiration for this script came from this post

Usage
  • 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!
Share:

Follow by Email