Last active
September 22, 2018 22:16
-
-
Save ssteinbach/f3bf8efd8c472b223b801ed5a382ae0c to your computer and use it in GitHub Desktop.
Gifify: quick python script to turn movies into gifs with ffmpeg
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
#!/usr/bin/env python | |
"""Use ffmpeg to turn a movie into a .gif.""" | |
import os | |
import argparse | |
import subprocess | |
def parse_args(): | |
""" parse arguments out of sys.argv """ | |
parser = argparse.ArgumentParser( | |
description=__doc__, | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter | |
) | |
parser.add_argument( | |
'-d', | |
'--dryrun', | |
action="store_true", | |
default=False, | |
help="Print out command instead of running it." | |
) | |
parser.add_argument( | |
"input_movie", | |
type=str, | |
help="Movie file that ffmpeg can read to turn into a .gif file." | |
) | |
parser.add_argument( | |
"-m", | |
"--multiple", | |
type=int, | |
default=4, | |
help="Scale multiplier (-m 4 means 1/4 the size)." | |
) | |
parser.add_argument( | |
"-r", | |
"--rate", | |
type=int, | |
default=10, | |
help="Output framerate." | |
) | |
parser.add_argument( | |
'-p', | |
'--pix_fmt', | |
type=str, | |
default="rgb24", | |
help="pixel format." | |
) | |
return parser.parse_args() | |
FFMPEG = ( | |
'ffmpeg -i {input_movie} -vf "scale=iw/{multiplier}:ih/{multiplier}" ' | |
'-pix_fmt {pix_fmt} -r {rate} -f gif {basename}.gif' | |
) | |
def main(): | |
"""main function for module""" | |
args = parse_args() | |
basename = os.path.splitext(args.input_movie)[0] | |
cmd = FFMPEG.format( | |
input_movie = args.input_movie, | |
multiplier = args.multiple, | |
rate = args.rate, | |
pix_fmt = args.pix_fmt, | |
basename = basename | |
) | |
if args.dryrun: | |
cmd = '[DRYRUN] ' + cmd | |
print(cmd) | |
if args.dryrun: | |
return | |
returncode = subprocess.call(cmd, shell=True) | |
if returncode: | |
raise RuntimeError( | |
"ffmpeg returned non-zero error code: {0}".format(returncode) | |
) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed some typos.