Created
July 13, 2012 17:19
-
-
Save leom/3106089 to your computer and use it in GitHub Desktop.
A python script to loop through a directory's video files to create .mp4 files (using HandbrakeCLI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# | |
# A simple python script to convert a directory full of avi, mp4, and mkv to | |
# iPad compatible videos | |
# | |
# Copyright Leo Mendoza (2012), released as is. | |
# | |
import glob | |
import os | |
import re | |
import sys | |
import argparse | |
extensions = ['avi', 'mp4', 'mkv'] | |
transcoder_options = ' --preset "iPad" --format "mp4"' | |
parser = argparse.ArgumentParser(description="Batch convert avi/mp4/mkv to mp4") | |
parser.add_argument("input_dir", action="store", help="Where to find the files") | |
parser.add_argument("--output_dir", dest="output_dir", action="store", default="", help="Where to output the files (if empty, defaults to input_dir)") | |
parser.add_argument("--hbcli", dest="hbcli", action="store", default="/Users/lmendoza/bin/HandBrakeCLI", help="HandbrakeCLI location") | |
parser.add_argument("--delete_old", dest="delete_old", action="store_true", help="Delete processed files") | |
args = parser.parse_args() | |
def main(): | |
transcoder = '%s %s' % (args.hbcli, transcoder_options) | |
if not args.output_dir: | |
args.output_dir = args.input_dir | |
targets = [] | |
target_dirs = ['%s/*.%s' % (args.input_dir, ext) for ext in extensions] | |
for t in [glob.glob(d) for d in target_dirs]: | |
targets += t | |
if len(targets) == 0: | |
return | |
for d in targets: | |
basename = os.path.basename(d) | |
if re.search('sample', basename): | |
continue | |
out_file = '%s%s.mp4' % (args.output_dir, os.path.splitext(basename)[0]) | |
print "---" | |
print "Transcoding %s to %s" % (basename, out_file) | |
os.system('%s --input "%s" --output "%s" > /dev/null' % (transcoder, d, out_file)) | |
print "Completed %s!" % (out_file) | |
if args.delete_old: | |
print "Deleted %s" % d | |
os.unlink(d) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment