Skip to content

Instantly share code, notes, and snippets.

@patgmac
Created June 15, 2017 00:06
Show Gist options
  • Save patgmac/fa1b46f4bc4f56161231e9dfa550370d to your computer and use it in GitHub Desktop.
Save patgmac/fa1b46f4bc4f56161231e9dfa550370d to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# handbrake_convert.py
# Author: Patrick Gallagher
# Purpose: Use HandBrakeCLI to convert media files to mp4
#
# https://handbrake.fr/docs/en/latest/cli/cli-guide.html
# https://handbrake.fr/downloads2.php
import os
import time
import subprocess
import sys
# This should be changed to the appropriate path
# Should work for Windows and Linux too.
handbrake_cli_path = '/Applications/handbrake/HandBrakeCLI'
handbrake_preset = 'Fast 1080p30' # I couldn't get this to work with a space
fileList = []
rootdir = raw_input("Root Dir: ")
for root, subFolders, files in os.walk(rootdir):
for file in files:
theFile = os.path.join(root,file)
fileName, fileExtension = os.path.splitext(theFile)
if fileExtension.lower() in ('.avi', '.divx', '.flv', '.m4v', '.mkv', '.mov', '.mpg', '.mpeg', '.wmv'):
print 'Adding',theFile
fileList.append(theFile)
# You can customize options here, see cli-guide link above
runstr = handbrake_cli_path + ' -i "{0}" -o "{1}" --preset="Fast 1080p30"' + ' --two-pass --turbo'
while fileList:
inFile = fileList.pop()
fileName, fileExtension = os.path.splitext(inFile)
outFile = fileName+'.mp4'
print 'Processing',inFile
returncode = subprocess.call(runstr.format(inFile,outFile), shell=True)
time.sleep(5)
print 'Removing',inFile
# Uncomment this if you're ok with deleting the files after conversion.
# Use with caution. Make sure script works before uncommenting this.
#os.remove(inFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment