DVR Postprocessing script for Handbrake on Windows --- help? Examples?

Hi, I’ve been trying to setup a simple postprocessing script for Plex DVR to reduce the size of the recordings, but I’m having no luck. I’m on Windows 10 and I’d like to use Handbrake. If someone has a real simple example that would be great. I have no idea what to put in as the input or output file, are there some kind of variables for the postprocessing script field? Or, do I need to write a BAT file to loop through some directory and then run Handbrake? I would hope that I’d be able to just use a command like the one below in the field:

“C:\Program Files\Handbrake\HandBrakeCLI.exe” -i “$FILENAME” -o “$TEMPFILENAME”

Thanks,
Erwin

Search the forum for PowerShell Post Processing. There is a script I saw recently that you should be able to modify. One I saw recently is using several tools including ffmpeg to convert video but you should be able to adapt to HandBrake.

FWIW, I gave up on the post-processing scripts. Instead, I have MCEBuddy (great app - and Windows only - uses the handbreak engine) to watch the folders where the recordings are saved. When a new recording comes in, it grabs the file, processes it, and replaces it in the same spot. Works great.

A couple tricks I’ve found along the way: be sure to set the DVR to remux (to MKV) and then chose MKV profiles in MCEBuddy with no renaming. That way Plex thinks it’s the same file and preserves the metadata.

Secondly, you can set MCEBuddy to only run a given profile on certain locations - I have two sets of locations (TV with commercials, TV without commercials) and another corresponding pair for movies. Since I know by channel if a given recording will have commercials or not, I chose where the recording is stored, and based on that, it pulls commercials out or not. It works great.

1 Like

Thanks for your help. I was able to write a .BAT script file (modified from another Plex user), which I’ve attached as a TXT file. The compression is very slow but it works. The paths used on my machine will most likely be different on your PC.

So in Plex under “DVR Settings” I have entered in my script “D:\Video\Scripts\PlexDVRHandbrake.bat” in the “POSTPROCESSING SCRIPT” field. Apparently when Plex records it saves the files into a sub-folder in the “.grab” folder and doesn’t move the file out until after processing the script. So my script loops through that folder for all .TS files and runs HandBrake on them to create MP4 files (you could save them as another extension). I’m using the “Chromecast 1080p30 Surround” preset compression but experimenting with others. If you open HandBrake you can see a list of the preset compressions on the right hand side of the window or you could run in the command prompt “C:\Program Files\Handbrake\HandBrakeCLI.exe” -z, --preset-list to get a list of the presets.

There are 2 problems with this script. One, the compression is slow and takes almost an hour for 1 hour of video. The other is more of a potential issue, the loop goes through all of the .grab folder and might pick up a file that is still being recorded. I’m not sure what would happen exactly if a second recording was in process and the script was to start.

If you are using Post Processing Plex will send the file to process as a parameter to your script so there should be no need to perform a search for all .ts files to process in the script. Also starting with the 1.7.0 release Plex no longer creates a .ts file, now saving recordings in an mkv file so your script would need to be changed to process mkv files instead.

I had noticed that some of my files were MKV. I had assumed it was an earlier script or maybe a change to the DVR settings?

The attached file is my new script based on the parameter being passed in. It didn’t seem to do anything (on my PC I have a .BAT extension instead of .TXT).

Have you tried running just the handbrakecli command at the command prompt? Use a ts or mkv file as the input with all of the options you are specifying. Running at the command prompt will hopefully display any errors that might be output.

You should also be able to put a pause at the end of the bat file and execute it like Plex would be typing PlexDVRHandbrake “my file to process” at the command prompt.

Thanks johnm_ColaSC. It worked fine at the command prompt, but I was able to make a log file when Plex ran the script. I discovered that the issue was the spaces in the directory name for the new MP4 file. I just needed to add quotes around the new filename and now it works! Attached is what I hope to be my final version.

Glad you got it working.

Hey I was hoping you could help me with my post-processing with Plex. You referenced in your post that you had a working, clean post-processing CLI for Handbrake & that it was attached to the forum post. I’m not sure if Plex pulled it because the post is older or what, but I would love to know exactly what has been working for you.
Thanks for your help

“Thanks johnm_ColaSC. It worked fine at the command prompt, but I was able to make a log file when Plex ran the script. I discovered that the issue was the spaces in the directory name for the new MP4 file. I just needed to add quotes around the new filename and now it works! Attached is what I hope to be my final version”

1 Like

Shortly after getting that to work I bought a Shield TV and migrated over to that as my Plex server. Play back of the files is perfect on the Shield and doesn’t require compressing the files.

Anyway, hopefully below is the code that I used. You have to pass %1 to HandBrake. Let me know if this helps, if not maybe for fun I can write a more generic version.

@echo off
setlocal enabledelayedexpansion
set HANDBRAKE=“C:\Program Files\Handbrake\HandBrakeCLI.exe”
set fnl=%~dpn1.MP4
set tempf=“D:\Video\Video Server\TV shows%nx1”
copy %1 %tempf%
timeout /t 45 /nobreak > NUL
%HANDBRAKE% -i %1 -o “%fnl%” -Z. --preset “Fast 720p30”
rem pint 127.0.0.1 -n 15 >nul
rem del %1
rem del %tempf%
rem move %1 “D:\Video\Video Server\TV shows”
echo
exit

Question if you guys are still looking around here, can I simply do the following in the postprocessing script field in Plex:

handbrakecli -i %1 -o %1.mp4 (possibly a specific transcode type here)

and be done with it, expecting Plex to delete/move files to the right directories as needed?

I am looking for the simplest way to just cut down on file size, nothing else.

Thanks

That might work, but it might rename the file from say “show.mkv” to “show.mkv.mp4”. Try running that at the Windows command prompt and then see if it changes the name as well as compressing the file. Also, you may need or want “–preset”.

I think that the code below would do the trick:
set HANDBRAKE=“C:\Program Files\Handbrake\HandBrakeCLI.exe”
set fnl=%~dpn1.MP4
%HANDBRAKE% -i %1 -o “%fnl%” --preset “Fast 720p30”
exit

You may even be able to shorten it to one line and skip the need for a BAT file.
“C:\Program Files\Handbrake\HandBrakeCLI.exe” -i %1 -o “%~dpn1.MP4” --preset “Fast 720p30”

Thanks! I will try that.

@erwinfj
Still working this… Can I assume, in the long filenames that files are saved with, with multiple spaces, that the %1 parameter Plex passes is enclosed in “quotes” and is the entire path? Otherwise, only the first word of the filename gets passed.

Assuming it is enclosed and is the “full path.ts”, the following postplex.bat batch works at the command line, have not tried in Plex yet.

echo Input file: %~1
echo Output file: %~n1.mp4
e:\handbrakecli\HandBrakeCLI.exe -i %1 -o “%~n1.mp4” --preset “Fast 720p30”

This results in the following handbrakecli line being setup when I pass it “Jimmy Kimmel.ts”

E:\HandbrakeCLI>postplex “Jimmy Kimmel.ts”

E:\HandbrakeCLI>echo Input file: Jimmy Kimmel.ts
Input file: Jimmy Kimmel.ts

E:\HandbrakeCLI>echo Output file: Jimmy Kimmel.mp4
Output file: Jimmy Kimmel.mp4

E:\HandbrakeCLI>e:\handbrakecli\HandBrakeCLI.exe -i “Jimmy Kimmel.ts” -o “Jimmy Kimmel.mp4” --preset “Fast 720p30”

And the file is made successfully.

I tried in Plex post processing field, and failed… The logs show this:

Dec 27, 2018 21:30:01.047 [11868] DEBUG - Job running: e:\handbrakeCLI\PostPlex.bat “E:\RecordedTV.grab\d0c7488df06459e08178f0f99bb46d4665c70eb0\Coach (1989) - S01E01 - Kelly and the Professor.ts”
Dec 27, 2018 21:30:01.063 [11868] DEBUG - Jobs: Starting child process with pid 8256
Dec 27, 2018 21:30:01.352 [5368] DEBUG - EPG[onconnect]: Next thing to start/end is at 2018-12-28 03:50:00 GMT (in 1200 seconds)
Dec 27, 2018 21:30:01.697 [18728] DEBUG - DVR:Segmenter: Stopping idle session.
Dec 27, 2018 21:30:02.855 [20144] DEBUG - Jobs: ‘e:\handbrakeCLI\PostPlex.bat’ exit code for process 8256 is 3 (failure)
Dec 27, 2018 21:30:02.855 [11868] ERROR - DVR:Recorder: Postprocessing script ‘e:\handbrakeCLI\PostPlex.bat’ exited with error code 3.

So the file parameter sent the batch file is in quotes, and is this:
“E:\RecordedTV.grab\d0c7488df06459e08178f0f99bb46d4665c70eb0\Coach (1989) - S01E01 - Kelly and the Professor.ts”

Not clear to me why this would fail? Should be passed to the batch file as expected?


Looks like the output file was not getting the correct full path with only the %~n1.mp4, that would only clip the filename, not the drive letter nor path.

this

e:\handbrakecli\HandBrakeCLI.exe -i %1 -o “%~dpn1.mp4” --preset “Fast 720p30”

%~dpn1.mp4 generates the mp4 in the same grab directory that the original ts file is in. We’ll see if that makes this work.


Damn, all that to get to the same answer as what you originally wrote, which means it probably won’t work since I already tried it.


Hmm, maybe I didn’t try it exactly as you had it originally. It IS making the MP4 file right now after a recording! Not 100% done and transferred to the right directory, but it is doing something right.


And checking now, it did transcode to mp4, did move it to the right directory, but it also moved the .ts file to that directory as well, so now I have 2 of the same recordings in the final directory. But it did work, so maybe then all I need to do is add a delete in the batch file at the end.

@erwinfj

I think that a delete of the .ts file at the end of your BAT file would get what you want.
Example:
del %1

Another option would be to move the .ts file to a backup directory. That way you have a backup. You can use the DOS RENAME command and just change the folder. You could then add code into the BAT file to clean up .ts files older than say for example one month. I’d put this code at the end of the BAT file, just in case it crashes for some odd reason and at least your conversion will have completed. Here’s some pseudo code (I’m not sure how to check file date):

backuppath=“directory of backup ts files”
for /R %backuppath% %%f in (*.ts) do (
if %%f is older than a month then del %%f
)

I also highly recommend using the full path with the filename when using HandBrake. I find it much easier to debug.

I know this is a question for Windows scripting but for what its worth, here is a bash script I’m using on my wife’s Mac to post process .ts files on my Qnap NAS. The source and dest paths are a volume mounted on the Mac, connecting to the Qnap file system.
This script converts the .ts files to .mp4 and deletes the .ts file essentially reducing file size about 50%

#!/bin/bash
IFS=$’\n’

SRC="/Volumes/handbrake/"

DEST="/Volumes/handbrake/"

DEST_EXT=mp4
HANDBRAKE_CLI=HandBrakeCLI
for FILE in $(find SRC -type f -wholename "*.ts") do extension={FILE##*.}
filename={FILE%.*} {HANDBRAKE_CLI} --preset=“Very Fast 1080p30” -i {FILE} -o {filename}.{DEST_EXT} if [ ? -eq 0 ];
then
rm -vf ${FILE}
fi
done

Thanks, all. Here’s what works:

e:\handbrakecli\HandBrakeCLI.exe -i %1 -o “%~dpn1.mp4” --preset “Very Fast 720p30”
del %1

I haven’t made a DOS batch file in, oh, about 25 years. Fun to remember!

Hey guys. Im trying to do the same. Just converting the TS video file to mp4 h.264 to save some disk space.
Plex always gives me the error "not found when i try to use the script:

C:\Handbrake\HandBrakeCLI.exe -i “%1” -o “%~dpn1.mp4” --preset “MCG” del %1

So i try to convert it manually after copying the video file to the handbrake folder.
I get this error. Can somone help me with that please?