Skip to content

Instantly share code, notes, and snippets.

@choffmeister
Last active November 27, 2018 12:09
Show Gist options
  • Save choffmeister/d0caa672d389a05952a44528158ecfd6 to your computer and use it in GitHub Desktop.
Save choffmeister/d0caa672d389a05952a44528158ecfd6 to your computer and use it in GitHub Desktop.
Python script to convert a video into a GIF
#!/usr/bin/python
# python -c "$(curl -fsSL https://gist.githubusercontent.com/choffmeister/d0caa672d389a05952a44528158ecfd6/raw/video-to-gif.py)" --width=720 --fps=1 video.mov
import argparse
import os
import re
import subprocess
from datetime import datetime
parser = argparse.ArgumentParser(description='video to gif')
parser.add_argument('video', help='the video file')
parser.add_argument('--fps', type=int, default=10, help='the frames per second')
parser.add_argument('--width', type=int, default=640, help='the width')
parser.add_argument('--quiet', dest='quiet', default=False, action='store_true')
args = parser.parse_args()
def log(msg):
HEADER = '\033[95m'
BOLD = '\033[1m'
ENDC = '\033[0m'
print('%s[%s]%s %s%s%s' % (HEADER, datetime.now(), ENDC, BOLD, msg, ENDC))
def command(bin, params):
with open(os.devnull, 'w') as DEVNULL:
stderr_target = DEVNULL if args.quiet == True else None
p = subprocess.Popen([bin] + params, stdout=subprocess.PIPE, stderr=stderr_target)
out, err = p.communicate()
if p.returncode != 0:
raise Exception('Command %s exited with code %d' % (bin, p.returncode))
return out
def convert(input, fps, width):
log('Processing video %s (fps=%d, width=%d)' % (input, fps, width))
command('ffmpeg', [
'-y',
'-i', input,
'-vf', 'fps=%d,scale=%d:-1:flags=lanczos,palettegen' % (fps, width),
'%s.palette.png' % input,
'-v', 'warning',
'-stats'
])
command('ffmpeg', [
'-y',
'-i', input,
'-i', '%s.palette.png' % input,
'-filter_complex', 'fps=%d,scale=%d:-1:flags=lanczos[x];[x][1:v]paletteuse' % (fps, width),
'%s.gif' % input,
'-v', 'warning',
'-stats'
])
try:
log('Starting processing')
start=datetime.now()
convert(args.video, args.fps, args.width)
end=datetime.now()
duration = end - start
log('Finished processing (took %d seconds)' % duration.total_seconds())
except BaseException as e:
log('Error: %s' % str(e))
exit(1)
#!/usr/bin/python
# python -c "$(curl -fsSL https://gist.githubusercontent.com/choffmeister/d0caa672d389a05952a44528158ecfd6/raw/video-to-mp4.py)" --width=720 --fps=30 video.mov
import argparse
import os
import re
import subprocess
from datetime import datetime
parser = argparse.ArgumentParser(description='video to gif')
parser.add_argument('video', help='the video file')
parser.add_argument('--fps', type=int, default=30, help='the frames per second')
parser.add_argument('--width', type=int, default=640, help='the width')
parser.add_argument('--quiet', dest='quiet', default=False, action='store_true')
args = parser.parse_args()
def log(msg):
HEADER = '\033[95m'
BOLD = '\033[1m'
ENDC = '\033[0m'
print('%s[%s]%s %s%s%s' % (HEADER, datetime.now(), ENDC, BOLD, msg, ENDC))
def command(bin, params):
with open(os.devnull, 'w') as DEVNULL:
stderr_target = DEVNULL if args.quiet == True else None
p = subprocess.Popen([bin] + params, stdout=subprocess.PIPE, stderr=stderr_target)
out, err = p.communicate()
if p.returncode != 0:
raise Exception('Command %s exited with code %d' % (bin, p.returncode))
return out
def convert(input, fps, width):
log('Processing video %s (fps=%d, width=%d)' % (input, fps, width))
command('ffmpeg', [
'-y',
'-i', input,
'-c:v', 'h264',
'-crf', '20',
'-c:a', 'mp3',
'-vf', 'fps=%d,scale=%d:-1' % (fps, width),
'%s.mp4' % input,
'-v', 'warning',
'-stats'
])
try:
log('Starting processing')
start=datetime.now()
convert(args.video, args.fps, args.width)
end=datetime.now()
duration = end - start
log('Finished processing (took %d seconds)' % duration.total_seconds())
except BaseException as e:
log('Error: %s' % str(e))
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment