How to Get Handbrake to Update Plex
I am always looking to automate aspects of maintaining my Plex server. One maintenance item I have been looking at over the past year is to automatically scan libraries when new media items have been added.
This has been working, for the most part, but sometimes when I rip my Blu-ray discs the movies aren't automatically added to my Plex library.
I use Handbrake to encode my movies after they have been ripped using MakeMKV, so I wanted to see if Handbrake can let Plex know when it has completed the encoding.
The good news is that Handbrake has an option to send the completed movie file to another file, so I figured I would use that option.
This post will outline how I get Handbrake to perform a partial scan in Plex when it completes an encoding.
The thought process
I am using Handbrake on Windows. I needed a way to have Handbrake send a request to the Scan a Partial Library endpoint on my Plex server.
The best option was to use the cURL command from the command line. Unfortunately, using the cURL command directly from Handbrake was not feasible for the following reasons:
- I needed to remove the movie file name so I only passed in the folder path where the movie is located.
- Windows doesn't play nice with some special characters in commands - such as ampersands. The folder can contain those special characters so I first needed to URL encode the folder path before sending it to the API endpoint.
Since I can only execute a single file from Handbrake when it has completed an encode, I decided to create a batch file that would solve the above two issues.
I couldn't have written a simple C# application, but a batch file was easy to create and can be changed as needed.
With that in mind, let's have a look at the batch file I use in Handbrake.
The batch file
The batch file isn't too complex but does resolve the two issues I explained earlier.
Before explaining how I go about getting Handbrake to call the partial scan endpoint on my Plex server, I'll show you the batch file, and then explain how it works.
@ECHO OFF :: Check if a parameter was provided if "%~1"=="" ( echo Usage: handbrake-update-plex.bat "full_file_path" exit /b 1 ) :: Extract the directory path from the parameter safely for %%A in ("%~1") do set "DIRECTORY=%%~dpA" :: Remove the trailing backslash if needed (optional) SET "DIRECTORY=%DIRECTORY:~0,-1%" SETLOCAL ENABLEDELAYEDEXPANSION SET "DIRECTORY=!DIRECTORY: =%%20!" SET "DIRECTORY=!DIRECTORY:&=%%26!" SET "DIRECTORY=!DIRECTORY:{=%%7B!" SET "DIRECTORY=!DIRECTORY:}=%%7D!" SET "DIRECTORY=!DIRECTORY:-=%%2D!" :: Set the range of numbers for the libraries SET START_ID=1 SET END_ID={end_library_id} :: Loop through the range of libraries and run a partial scan FOR /L %%I IN (%START_ID%,1,%END_ID%) DO ( %SystemRoot%\System32\curl.exe -X GET "%PLEX_URL%/library/sections/%%I/refresh?path=%DIRECTORY%&X-Plex-Token=%PLEX_TOKEN%" )
The batch file can be run with the following command (assuming you name the batch file handbrake-update-plex.bat
):
handbrake-update-plex.bat "{full path to the movie file}"
Replace {full path to the movie file}
with the actual path of the movie file. The path should include the file name, as this is what Handbrake will send to the batch file.
The batch file does the following:
if "%~1"=="" ( ... )
- Checks if the first argument (
%~1
) passed to the batch file is empty. - If no argument is provided, it displays a usage message (
Usage: batchfile.bat "full_file_path"
) and exits the script with a non-success exit code (exit /b 1
).
- Checks if the first argument (
for %%A in ("%~1") do set "DIRECTORY=%%~dpA"
- Extracts the directory path (
%%~dpA
) from the full file path (%~1
) passed as an argument. - Sets the extracted directory path to the variable
DIRECTORY
.
- Extracts the directory path (
SET "DIRECTORY=%DIRECTORY:~0,-1%"
- Removes the trailing backslash (
\
) from the directory path stored in theDIRECTORY
variable.
- Removes the trailing backslash (
SETLOCAL ENABLEDELAYEDEXPANSION
- Enables delayed variable expansion, allowing the use of
!
instead of%
to safely modify variables in the script.
- Enables delayed variable expansion, allowing the use of
Replacing Special Characters
SET "DIRECTORY=!DIRECTORY: =%%20!"
- Replaces all spaces (
%20
).
SET "DIRECTORY=!DIRECTORY:&=%%26!"
- Replaces all ampersands (
&
) in the directory path with the URL-encoded equivalent (%26
).
SET "DIRECTORY=!DIRECTORY:{=%%7B!"
- Replaces all left parenthesis (
{
) in the directory path with the URL-encoded equivalent (%7B
).
SET "DIRECTORY=!DIRECTORY:}=%%7D!"
- Replaces all right-parenthesis (
}
) in the directory path with the URL-encoded equivalent (%7D
).
SET "DIRECTORY=!DIRECTORY:-=%%2D!"
- Replaces all dashes (
-
) in the directory path with the URL-encoded equivalent (%2D
).
- Replaces all spaces (
Setting the library ID values
SET START_ID=1
- Set the starting library ID for looping through the libraries.
SET END_ID={end_library_id}
- Set the ending library ID for looping through the libraries.
Running the partial scan for all libraries
FOR /L %%I IN (%START_ID%,1,%END_ID%) DO ( ... )
- Loop through all the libraries where the ID is between the start and end Id (inclusive) and then run the cURL command.
%SystemRoot%\System32\curl.exe
- Calls the cURL utility (located in the Windows system directory).
-X GET
- Specifies the HTTP
GET
method.
"%PLEX_URL%/library/sections/%%I/refresh?path=%DIRECTORY%&X-Plex-Token=%PLEX_TOKEN%"
- Sends a request to the Plex API to refresh the library section for the given directory path (
%DIRECTORY%
). %PLEX_URL%
is the base URL of your Plex server, and%PLEX_TOKEN%
is the authentication token required for the API call.
The batch file will now automate the process of performing a partial scan in Plex. The next step is to call the batch file from Handbrake whenever it finishes processing a file.
One thing to note, is that the batch file will loop through all the libraries on a Plex server and then call the partial scan endpoint.
The scan will only work for the library that contains the folder.
The entire process shouldn't take more than a couple of seconds to complete as only a partial scan is done for the library that contains the folder being scanned.
Handbrake settings
Once the batch file has been created, the next step is to have Handbrake call the batch file when it has completed the encoding of a file.
The steps to have Handbrake call the batch file are outlined below.
- Open Handbrake and then click Tools->Preferences from the top menu.
- In the Preferences dialog, click the When Done option from the left menu, and then check the Send file to: option. Click the Browse button and then select the batch file, and then click the Open button to select the file. Finally, enter
{destination}
in the Arguments field. - Click the Back button at the top-left to close the Preferences dialog.
Every time Handbrake has completed a file encoding, it will call the batch file, which will then send a request to the Plex server to scan that file and make it available in Plex.