PostProccessing: Usage Examples

Update: I tried adding $PYTHONHOME (/usr/local/bin) to the system’s environment variables, but no dice.

The only thing I can discern is that it works with my account when I run it manually. When the “plex” account runs it via the postprocessing step, I see the above Python error.

Has no one else seen this? If so, how did you get around it? I can’t be the only one.

One thing that I’ve noticed is that Plex (at least on Linux) uses LD_LIBRARY_PATH to locate the pre-packaged libraries that come with the Plex package. Since Plex includes it’s own copy of the Python interpreter odd things happen when trying to execute the system Python interpreter from within Plex (like when Plex invokes the postprocessor). Unless/until Plex fixes that, the easiest solution that I have found is to wrap the Python script in a bash script that unsets LD_LIBRARY_PATH before executing the real script.

#!/bin/bash

unset LD_LIBRARY_PATH
exec realpythonscript "$1"

I’ll try unsetting that in my wrapper! Thanks for the suggestion.

Nah - the environment still appears to be dirty.

Looks like I might have to dig into the depths of how Comskip works to write my own script. Likely in Bash.

@jasonmicron said:
Nah - the environment still appears to be dirty.

It’d be interesting to log all of the environment variables that are set when Plex executes the post processing script. That’s easy enough to do by adding the command set to your script.

@hthighway said:

@wolvesfan08 said:

@hthighway said:

@Minxster said:
Donator for MCEBuddy, not yet for Comskip due to slowness… It’s taking around 4 hours to process my shows with a Xeon E3-1240 (3.6h=ghz / 4 core)

I have a E3-1230 V2 3.3GHz, and with the donated version of Comskip, a 3 hour football game takes a about 15 minutes to complete

How are you using the donater Comskip version on Ubuntu (you mentioned running Ubuntu on another thread I believe) ?

GitHub - erikkaashoek/Comskip: A free commercial detector
./configure --enable-dontator and then make a donation to Comskip Support Forum - Index page
Now I have read where some say the linux version on github ignores that flag but in my usage it seems to work.

From the developer when I asked this question:

The Linux version does not support HW acceleration.
Also under wine HW acceleration will not work.
So the linux version is the full version available

Regardless this part of the PP script doesn’t take that long as is.

Is there a way to insert a transcoding step into the script after the comskip?

Either by tweaking what FFMPEG does or handbrake to convert the giant mpeg2 into a h264

This is what I’ve been using

cmd = [FFMPEG_PATH, '-y', '-f', 'concat', '-i', segment_list_file_path, '-vf', 'scale=-1:720', '-c:v', 'libx264', '-preset', 'superfast', '-strict', '-2', '-c:a', 'aac', '-b:a' ,'192k' , os.path.join(temp_dir, video_basename)]

@gazoo69@gmail.com said:
Is there a way to insert a transcoding step into the script after the comskip?

Either by tweaking what FFMPEG does or handbrake to convert the giant mpeg2 into a h264

I suppose you could use the wrapper script that is calling the python script. After the python script, you could call ffmpeg and have it perform the encode at that stage. $1 is still the variable to use for the input file and output file, and double quotes are likely if the filename has spaces. At least, that’s my untested idea. I think there might be a problem if the format of the video is different than the input (MPEG2 vs H.264). I think I read that in the release notes. You might consider using a different extension other than mkv (maybe use mp4), and remove the .mkv duplicate afterward, which would be the source file.

Keep in mind though, that if you have your Plex server configured to delete items after X number of episodes (or at all, really) you could have some unwanted deletions. I saw a few folks complaining about that in other threads this past week with the beta.

@jcollie said:

@jasonmicron said:
Nah - the environment still appears to be dirty.

It’d be interesting to log all of the environment variables that are set when Plex executes the post processing script. That’s easy enough to do by adding the command set to your script.

Not sure what happened, but it appears to work. I recorded Big Bang Theory last night, and while Comskip sort of botched the intro and the last two commercials remained after the title sequence, everything else appeared to be fine.

For posterity, this was the environment Plex had available to it during the encode. Remember, I’m unsetting LD_LIBRARY_PATH (in my above post):

[jasonmicron@mediasrv tmp]$ head -30 processing.log
SHELL=/sbin/nologin
PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS=6
LC_ALL=en_US.UTF-8
USER=plex
LD_LIBRARY_PATH=/usr/lib/plexmediaserver
PLEX_MEDIA_SERVER_TMPDIR=/tmp
PLEX_MEDIA_SERVER_HOME=/usr/lib/plexmediaserver
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
PWD=/
LANG=en_US.UTF-8
SHLVL=2
HOME=/var/lib/plexmediaserver
LOGNAME=plex
PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR=/var/lib/plexmediaserver/Library/Application Support
_=/usr/bin/env

Setting Language...

The file is being analyzed.

…and the script ran with Comskip (finally). Notice that the Python variable didn’t take hold, even though I added it to /etc/profile. So I suppose since the shell is /sbin/nologin, that file never gets sourced. I suppose I could export it from the BASH wrapper script, but /shrug it works. Maybe Comskip just couldn’t detect commercials at all in my previous tests - they were junk newscasts and 70’s game shows.

@jasonmicron said:

@gazoo69@gmail.com said:
Is there a way to insert a transcoding step into the script after the comskip?

Either by tweaking what FFMPEG does or handbrake to convert the giant mpeg2 into a h264

I suppose you could use the wrapper script that is calling the python script. After the python script, you could call ffmpeg and have it perform the encode at that stage. $1 is still the variable to use for the input file and output file, and double quotes are likely if the filename has spaces. At least, that’s my untested idea. I think there might be a problem if the format of the video is different than the input (MPEG2 vs H.264). I think I read that in the release notes. You might consider using a different extension other than mkv (maybe use mp4), and remove the .mkv duplicate afterward, which would be the source file.

Keep in mind though, that if you have your Plex server configured to delete items after X number of episodes (or at all, really) you could have some unwanted deletions. I saw a few folks complaining about that in other threads this past week with the beta.

Not really a problem, the transcoding is precisely to keep all episodes recorded ad infinitum (not really economical with 4 gig files for a 21 minute show)

@mdpauley said:
This is what I’ve been using

cmd = [FFMPEG_PATH, '-y', '-f', 'concat', '-i', segment_list_file_path, '-vf', 'scale=-1:720', '-c:v', 'libx264', '-preset', 'superfast', '-strict', '-2', '-c:a', 'aac', '-b:a' ,'192k' , os.path.join(temp_dir, video_basename)]

Where do you put that in the code? Just slap it at the end?

@gazoo69@gmail.com said:

Where do you put that in the code? Just slap it at the end?

Comment out line 179 of PlexComskip.py and insert that line below it.

So, i wrote this after banging my head on the wall a bunch…

INFILE="$1"
INFILE_CLEAN="${1//[\\]/}"
BASENAME="${INFILE_CLEAN##*/}"
BASE_WITHOUT_EXT="${BASENAME%.*}"
DIR=$(dirname "${INFILE_CLEAN}")
OUTFILE="$DIR"/"$BASE_WITHOUT_EXT"_h264.mkv
MYPID=$$
LOGFILE=/Volumes/PVR/tmp/postProcess-$MYPID/ppr.log

HandBrakeCLI -i "$INFILE_CLEAN" -o "$OUTFILE" --format mkv --encoder x264 --quality 18 -E copy --width 1280 --loose-anamorphic --decomb fast --x264-preset fast --h264-profile high --h264-level 4.1

rm -f "$INFILE_CLEAN"

And it works perfectly fine if i run it on my own. But when it’s run by plex post processing it deletes the file immediately and does not even run the handbrake cli or anything at all…

Plex Log only says:

MediaRecorder: Postprocessing script ‘/Users/andresungaro/Documents/h264_Conversion.sh’ seems to have deleted the file. Silly script.

So, what am I doing wrong?

@gazoo69@gmail.com said:
HandBrakeCLI -i “$INFILE_CLEAN” -o “$OUTFILE” --format mkv --encoder x264 --quality 18 -E copy --width 1280 --loose-anamorphic --decomb fast --x264-preset fast --h264-profile high --h264-level 4.1
So, what am I doing wrong?

Put the whole path to HandBrakeCLI in there.

@gazoo69@gmail.com said:
Is there a way to insert a transcoding step into the script after the comskip?

Either by tweaking what FFMPEG does or handbrake to convert the giant mpeg2 into a h264

In addition to the examples you were provided, hthighways comskip fork includes encoding to h264. It works well, however it joins the split file and then transcodes it separately. The example above will save some IO - and perhaps speed it up a little bit.

Has anyone tried mencoder to transcode the recording with the comskip .edl?

mencoder infile.mkv -edl video-edl.edl -ovc x264 -oac copy -o outfile.mkv

@mdpauley said:

@gazoo69@gmail.com said:
HandBrakeCLI -i “$INFILE_CLEAN” -o “$OUTFILE” --format mkv --encoder x264 --quality 18 -E copy --width 1280 --loose-anamorphic --decomb fast --x264-preset fast --h264-profile high --h264-level 4.1
So, what am I doing wrong?

Put the whole path to HandBrakeCLI in there.

Worked like a charm, man. Thanx!

Weird… Since HB CLI is in my usr/local/bin folder it usually works without having to give full paths… but i guess it’s a kink in the Plex DVR flow.

@hthighway said:
For ffmpeg, this is the general syntax:
ffmpeg -i inputfile.mkv -crf 18 -map 0 -acodec copy -scodec copy -c:v libx264 -threads 0 -preset veryslow outputfile.mkv

Right now I am testing -preset medium and -crf 20

What did you settle on for your final ffmpeg output? I’m almost done with my linux/bash based version of a comstripping script, but the constant transcode errors have made me have to stop and redo things. If someone has already sorted out the best combo to go from TS to mkv, I’d rather credit/use their work than reinvent the wheel.

@gazoo69@gmail.com said:

@mdpauley said:

@gazoo69@gmail.com said:
HandBrakeCLI -i “$INFILE_CLEAN” -o “$OUTFILE” --format mkv --encoder x264 --quality 18 -E copy --width 1280 --loose-anamorphic --decomb fast --x264-preset fast --h264-profile high --h264-level 4.1
So, what am I doing wrong?

Put the whole path to HandBrakeCLI in there.

Worked like a charm, man. Thanx!

Weird… Since HB CLI is in my usr/local/bin folder it usually works without having to give full paths… but i guess it’s a kink in the Plex DVR flow.

Since the “user” isn’t logged in the PATH variable isn’t set. Thats just a *nix thing