ARTICLE AD BOX
I've got a bunch of uncompressed MKVs on a media server that I would like to compress to HEVC. Being a lazy person, I thought I would write a script to do this, instead of manually converting hundreds of files. I downloaded handbrake for linux from the official flatpack as per the instructions on the handbrake website, and got my command working on a test file. However, when I put my command into python, handbrake fails.
I'm running in Ubuntu
Python Code:
import os import subprocess PATH = "/path/to/my/videos" my_env = os.environ.copy() my_env["PATH"] = f"/usr/sbin:/sbin:{my_env['PATH']}" def make_new_name(filename): (name, extension) = filename.split(sep=".") return f"{name} (Compressed).{extension}" def compress_directory(path): dirlist = os.listdir(path) for file in dirlist: # print(file) if (".mkv" in file) and ("Compressed" not in file): subprocess.run([ "flatpak", "run", "--command=HandBrakeCLI", "fr.handbrake.ghb", "-O", "--keep-metadata", "-e x265", "--multi-pass", "--turbo", "-vfr", "--all-audio", "--all-subtitles", f"-i {path}/{file}", f"-o {path}/{make_new_name(file)}" ], env=my_env) print(make_new_name(file)) if os.path.isdir(f"{path}/{file}"): print(f"{path}/{file} is a dir") compress_directory(f"{path}/{file}") compress_directory(PATH)When I run this, I get the following log:
[23:12:50] Compile-time hardening features are enabled Cannot load libnvidia-encode.so.1 [23:12:50] vcn: not available on this system [23:12:50] qsv: not available on this system [23:12:50] hb_init: starting libhb thread [23:12:50] thread 7b1b1ae146c0 started ("libhb") HandBrake 1.10.2 (2025090600) - Linux x86_64 - https://handbrake.fr 8 CPUs detected Opening /path/to/my/videos/S00E00.mkv... [23:12:50] CPU: Intel(R) Xeon(R) W-2123 CPU @ 3.60GHz [23:12:50] - logical processor count: 8 [23:12:50] Intel Quick Sync Video support: no [23:12:50] hb_scan: path= /path/to/my/videos/S00E00.mkv, title_index=1 [23:12:50] hb_stream_open: open /path/to/my/videos/S00E00.mkv failed [23:12:50] scan: unrecognized file type [23:12:50] libhb: scan thread found 0 valid title(s) No title found. HandBrake has exited.For reference, the command that works (manually) is:
flatpak run --command=HandBrakeCLI fr.handbrake.ghb -O --keep-metadata -e x265 --multi-pass --turbo -vfr --all-audio --all-subtitles -i /path/to/my/videos/S00E00.mkv -o output.mkv