Python script that recursively searches through a user-defined file path and converts all videos of user-specified file types to MP4 with H264 video and AAC audio using ffmpeg. If a conversion failure is detected, the script re-encodes the file with HandbrakeCLI. Upon successful encoding, Plex libraries are refreshed and source file is deleted. The purpose of this script is to reduce the amount of transcoding CPU load on a Plex server.

BrianDMG BrianDMG Last update: Apr 10, 2022

conv2mp4-py

Pythonl script that recursively searches through a user-defined file path and convert all videos of user-specified file types to MP4 with H264 video and AAC audio as needed using ffmpeg. If a conversion failure is detected, the script re-encodes the file with HandbrakeCLI. Upon successful encoding, Plex libraries are refreshed and source file is deleted. The purpose of this script is to reduce the amount of transcoding CPU load on a Plex server and increase video compatibility across platforms.

PowerShell version can be found here: conv2mp4-ps

Dependencies
This script requires Python 2.7+, ffmpeg, ffprobe (included with ffmpeg), and Handbrake (HandbrakeCLI.exe) to be installed. You can download them from here:
Python 2.7+
ffmpeg
Handbrake

Usage
Run this script the same way you would run any other Python script on your system.

User-defined variables
There are several user-defined variables you will need to edit using notepad or a program like Notepad++.

media_path = the path to the media you want to convert
file_types = the extensions of the files you want to convert in the format ".ex1", ".ex2"
log_path = path you want the log file to save to.
log_name = the filename of the log fileplex_ip = the IP address and port of your Plex server (for the purpose of refreshing its libraries)
plex_token = your Plex server's token (for the purpose of refreshing its libraries).
NOTE: See https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-account-token-X-Plex-Token
Plex server token is also easy to retrieve with PlexPy, Ombi, Couchpotato, or SickRage

ffmeg_bin_dir = path to ffmpeg bin folder. This is the directory containing ffmpeg and ffprobe executables.
ffmpeg_exe = name of the ffmpeg executable (eg. ffmpeg.exe, ffmpeg.sh, etc.)
ffprobe_exe = name of the ffprobe executable (eg. ffprobe.exe, ffprobe.sh, etc.)
handbrake_dir = path to Handbrake directory (no trailing ""). This is the directory containing HandBrakeCLI.exe
handbrakecli_exe = name of the handbrakecli executable (eg. handbrakecli.exe, handbrake.sh)
garbage = the extensions of the files you want to delete in the format ".ex1", ".ex2".

Subscribe to our newsletter