Skip to content

Instantly share code, notes, and snippets.

@eddieparker
Created September 2, 2019 21:25
Show Gist options
  • Save eddieparker/3d96a8e9c26659a96d3915179c05cacf to your computer and use it in GitHub Desktop.
Save eddieparker/3d96a8e9c26659a96d3915179c05cacf to your computer and use it in GitHub Desktop.
Recursive handbrake ripping python script
# Searches for all .mkv files in the current directory and below, and converts them using handbrake.
import subprocess
import os
import sys
g_preset = "Super HQ 1080p30 Surround"
g_output_extension = "m4v"
g_output_root_dir = "e:/VideoConverted"
def convert_source(source_filename):
output_filename = os.path.join(g_output_root_dir, source_filename)
output_filename = os.path.splitext(output_filename)[0] + f'.{g_output_extension}'
output_filename = os.path.abspath(output_filename)
# Build output directory
output_dir = os.path.dirname(output_filename)
os.makedirs(output_dir, exist_ok=True)
command = f'HandBrakeCLI.exe -i "{source_filename}" -o "{output_filename}" -Z "{g_preset}"'
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = []
for line in iter(process.stdout.readline, b''):
line = line.decode('utf-8')
output.append(line)
if process.wait() != 0:
for line in output:
sys.stdout.write(line)
print(f'Failed to issue command line: {command}')
sys.exit(-1)
files_to_convert = []
for root, dirs, files in os.walk('.'):
for filename in files:
filename = os.path.join(root, filename)
if filename.lower().endswith('.mkv'):
files_to_convert.append(filename)
num_files = len(files_to_convert)
print(f'Converting #{num_files} files')
for i, filename in enumerate(files_to_convert):
print(f"Processing file {i+1}/{num_files}: {filename}")
convert_source(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment