Created
October 17, 2014 12:23
-
-
Save SharkyRawr/4df204d86cb6e829944c to your computer and use it in GitHub Desktop.
This file contains 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
import os, sys, subprocess, shlex, argparse, multiprocessing | |
def dir(path): | |
if os.path.isdir(path): | |
return path | |
else: | |
raise argparse.ArgumentTypeError("%s is not a valid directory" % (path)) | |
def process_dir(path, dest): | |
files = os.listdir(path) | |
gifs = [] | |
for f in files: | |
name, ext = os.path.splitext(f) | |
if ".gif" in ext.lower(): | |
gifs.append( | |
(os.path.abspath(f), os.path.join(os.path.abspath(dest), name + '.webm')) | |
) | |
return gifs | |
def convert(path): | |
inf, out = path | |
if os.path.lexists(out): | |
return | |
args = shlex.split('ffmpeg -i "%s" -an -c:v libvpx-vp9 -strict experimental -crf 10 -qmin 4 -qmax 20 -b:v 5000k -y "%s"' % (inf, out)) | |
proc = subprocess.Popen(args) | |
proc.wait() | |
if __name__ == '__main__': | |
multiprocessing.freeze_support() | |
pool = multiprocessing.Pool(multiprocessing.cpu_count()) | |
p = argparse.ArgumentParser() | |
p.add_argument("gifpath", type=dir, default=".") | |
p.add_argument("destpath", type=str, default="webm") | |
args = p.parse_args() | |
files = process_dir(args.gifpath, dest=args.destpath) | |
print "Found %d gifs ..." % (len(files)) | |
print "Starting to convert gifs to webm/vp9 with %d threads ..." % (multiprocessing.cpu_count()) | |
pool.map(convert, files) | |
print "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment