Jan-27-2022, 04:20 AM
I am using Python to compress video files via Handbrake and subprocess. When I run my current code the HandbrakeCli output still outputs to stdout. Is there a better way to contain and control the output of HandbrakeCli in Python?
import subprocess
from tqdm import tqdm
handbrake_cli = "HandBrakeCLI.exe"
input_path = ""
json_preset = ""
output_path = ""
command = [handbrake_cli, "-i", input_path, "--json", json_preset, "-o", output_path]
p = subprocess.Popen(command, stdout=subprocess.PIPE, text=True)
with tqdm(total=100, dynamic_ncols=True, desc="Compressing file... ") as prbar:
while (line := p.stdout.readline()) != "":
line = line.rstrip()
if '"Progress"' in line:
progress = line.split(":")[1].lstrip(".").split(".")[0]
# print(f"Progress: {progress}")
prbar.update(progress)
print(f"End of output. Return code: {p.wait()}")
