[BASH] [plexEncode.sh] Plex DVR / Sonarr / Radarr Post Processing Script

Hi Plex Community, I just wanted to share my experience, and a script that might help others with the same problem.

Problem:

Finally pulled the trigger on replacing my cable TV box with a HD HomeRun Prime that allows Plex to record TV. Problem is the files it generates are HUGE. e.g. a 2 hour episode is pushing 22GB.

Solution:

A post-processing script that transcodes video files into a smaller, more efficient format for archival. GitHub - scrathe/plexEncode: BASH post-processing script for Plex DVR, Shell, Sonarr, Radarr

That is all. If you have problems, reach out here or better yet, report a bug on the Github.com page.

Usage:

Plex DVR

Setup: Plex \ Settings \ Live TV & DVR \ DVR Settings \ Postprocessing Script = full path to the script

Shell

plexEncode.sh <file> <encoder> <remove_original>

<encoder>           = ffmpeg    # .mkv file output. modify $ffmpeg_options to your specs.
                    = handbrake # .m4v file output. modify $handbrake_options to your specs.
<remove_original>   = 0         # keep original input file.
                    = 1         # delete original input file.

Shell Examples:

# encode a single file using default encoder and original file handling set in script
plexEncode.sh "file"

# encode a single file using handbrake, remove the original file
plexEncode.sh "file" handbrake 1

# encode a single file using ffmpeg, keep the original file
plexEncode.sh "file" ffmpeg 0

# loop thru a directory containing multiple .ts files
for i in *.ts; do plexEncode.sh "$i" ; done

Sonarr

Radarr

Required:

Script to add handbrake/ffmpeg/mediainfo/etc packages to your plex/sonarr/radarr dockers; docker-add-apps.sh · GitHub

Sample Logging:


g)

I just set up Plex DVR today after getting in my USB Dual Tuner and found this post when looking for scripts for how to best integrate recorded shows with my existing Sonarr library. I haven’t recorded a show yet but have a question.

I’m assuming your script is triggered by Plex via the Postprocessing script field. Once “processed” by your script, how does Sonarr discover it? Is the legacy Drone Factory folder used? I see your screenshots for Sonarr/Radarr, but don’t see how they are grabbed.

I’d try it out myself, but my PMS & Sonarr are currently on Windows.

Thanks for posting! - Interested to find out how this works.


EDIT - Forgot something…
Would your script work with the remove commercials feature or would it have to be re-encoded twice if so? Have you checked out MCEBuddy? Know it can re-encode + remove commercials simultaneously and didn’t know if viable solution. Wish it was possible for the video to just be trimmed instead of full reencode to remove parts. I’m sure re-encoding takes a good chunk of CPU time so considering reconditioning an old PC just for hosting PMS, etc for that purpose. Would then be able to use your script instead of rewriting it as would probably use Linux since less gost resources. Thanks

Hi bzowk, so when it comes to Sonarr/Radarr/Plex - think of them as completely separate processes in terms of the file acquisition and post-processing.

  • Sonarr grabs from usenet/torrent, optional post-process, then store in /media/TV
  • Radarr grabs from usenet/torrent, optional post-process, then store in /media/Movies
  • Plex records from cable/OTA, optional post-process, then stores in either /media/TV or /media/Movies (actually I haven’t tried recording a movie yet so i’m 100% sure about that scenario)

So long as your Sonarr/Radarr/Plex media folders all line-up, they should work together just fine.

My Sonarr/Radarr setup doesn’t use the Drone Factory folder. I believe that functionality is being deprecated/not-best-practice? I use NZBget and I believe the file routing is all controlled by the API.
Example: Perhaps someone can explain this workflow better than I can - it just works :wink:

  1. Sonarr tells NZBget go grab a file
  2. NZBget drops the completed download in /downloads/TV
  3. NZBget tells Sonarr, hey I’m finished, move/rename that file into the correct folder /media/TV/Show/Season/
  4. Sonarr runs the post-process

My script has no awareness of the comskip at all. It takes an input file, transcodes using ffmpeg or handbrake, and outputs a file.

I have heard of MCEBuddy, but I have not used it before. I use Plex’s built-in comskip and it works relatively well, some channels are better than others. I’m still new to Plex DVR (HD HomeRun Prime).
Example:

  1. Plex finishes a recording and starts the comskip post-process. I find this step to be “very quick” in comparison to the full re-encode my script (plexEncode) does. With my hardware a 1 hour recorded show completes the comskip step in roughly 10 minutes.
  2. After comskip, then the optional post-process script does it’s thing. This is very CPU intensive depending on the quality settings. ffmpeg and handbrake are supported by my script. Both options have 100s of knobs to tweak and twiddle around with which can make things go slower/better-quality/smaller-file, or faster/lesser-quality/bigger-file.

For me, I enjoy the challenge of transcoding my media into smaller, but still high quality files for my archivist tendencies. Making the media as compatible with my home theater (HDR, 5.1 audio, etc). My friends think I’m crazy, but I think it’s fun.

My setup:
Chassis: SC847E16-R1K28LPB
M/B: Supermicro - X9DRi-LN4+
CPU: Dual Intel® Xeon® CPU E5-2667 v2 @ 3.30GHz
Memory: 192 GB Multi-bit ECC (this is complete overkill and unnecessary but helps with homelabs)
OS: unRaid / dockers
Usable Storage: 42TB

Shameless plug and shoutout to the Reddit /r/datahoarder /r/homelab /r/unraid /r/plex communities.

Sonarr and Radarr do grab independent of PMS.

If I may offer this “Custom” script for Sonarr. It does the required cleaning just before handing off the files to PMS. This way, iNotify does not have to contend with the file changing after it’s detected it.

You’ll notice I put mkvmerge, mkvpropedit, and dependent libraries in the container.

This is, I believe, the preferred method now. (Event driven) . With a few tweaks, the Radarr companion is easily crafted

[chuck@lizum scripts.105]$ cat clean-episode 
#!/bin/sh

# this script runs when sonarr triggers (a download/rename) to clean subtitles and attachments

# Location of binaries
Bin="/usenet/bin"

# Location of libraries
export LD_LIBRARY_PATH=/usenet/lib

# Sonarr variables
Event=$sonarr_eventtype
Episode="$sonarr_episodefile_path"
Series="$sonarr_series_title"

# User specified log file
Logfile="/usenet/clean-mkv.log"
touch $Logfile


echo `date` Event: \"$Event\" proccesed  for $Series , Episode: $Episode >> $Logfile

DoEvent=0

if [ "$Event" = "Download" ]; then
    DoEvent=1
 elif [ "$Event" = "Upgrade" ]; then
    DoEvent=1
 elif [ "$Event" = "Rename" ]; then
    DoEvent=1
fi

if [ $DoEvent -eq 1 ]; then
  echo  `date` Cleaning: $Episode >> ${Logfile}
  rm -f temp.mkv
  ${Bin}/mkvmerge -o temp.mkv -M -S --no-global-tags --disable-track-statistics-tags "$Episode"
  if [ $? -ne 0 ]; then
    echo `date` Bad file: Error \($?\) "$Episode" >> ${Logfile}
    rm -f temp.mkv
  else
    echo `date` "Keeping: " "$Episode" >> ${Logfile}
   ${Bin}/mkvpropedit -s title="" temp.mkv
   ${Bin}/mkvpropedit --edit track:a1 --set language=eng --edit track:v1 --set language=eng temp.mkv
   mv -f temp.mkv "$Episode"
   rm -f temp.mkv
  fi
fi