Skip to content

Instantly share code, notes, and snippets.

@jacoduplessis
Last active October 26, 2016 12:39
Show Gist options
  • Save jacoduplessis/935451421d733ed2978a121734384bc9 to your computer and use it in GitHub Desktop.
Save jacoduplessis/935451421d733ed2978a121734384bc9 to your computer and use it in GitHub Desktop.
import subprocess
import sys
"""
get single frame at specific time
=================================
ffmpeg -i "input.mp4" -ss <time> -vframes 1 single.png
get file length in seconds
==========================
ffprobe -i "input.mp4" -show_format 2>&1 | sed -n 's/duration=//p'
"""
def generate_gif():
subprocess.run(["ffmpeg",
"-f", "image2",
"-framerate", "1",
"-i", "thumb_%02d.jpg",
"thumb.gif"])
def generate_thumbs(filename, thumbs=10, padding=0.1):
seconds = get_length_in_seconds(filename)
first = int(seconds * padding * 0.5)
interval = int(seconds/thumbs)
frames = range(first,int(seconds),interval)
counter = 1
for frame in frames:
subprocess.run(["ffmpeg",
"-ss", str(frame),
"-i", filename,
"-vframes", "1",
"thumb_{:02d}.jpg".format(counter)
])
counter += 1
def get_length_in_seconds(filename):
probe = subprocess.Popen(('ffprobe', '-i', filename, '-show_format'), stdout=subprocess.PIPE)
output = subprocess.check_output(('sed', '-n', 's/duration=//p'), stdin=probe.stdout, universal_newlines=True)
probe.wait()
return float(output)
if __name__ == "__main__":
filename = sys.argv[1]
generate_thumbs(filename)
generate_gif()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment