Skip to content

Instantly share code, notes, and snippets.

@guiszk
Created November 15, 2021 00:28
Show Gist options
  • Save guiszk/04fe87de580dd6529c5faf41afb1eb79 to your computer and use it in GitHub Desktop.
Save guiszk/04fe87de580dd6529c5faf41afb1eb79 to your computer and use it in GitHub Desktop.
Extract stills from video files.
from pylab import *
from moviepy.editor import VideoFileClip, ImageSequenceClip
import random
import sys
import os
if(len(sys.argv) != 3):
print(f"{sys.argv[0]} <path> <number of images>")
sys.exit(0)
sdir = sys.argv[1]
imnum = int(sys.argv[2])
ext = 'mkv' #video extension
vstart = 7 #intro time
vcreds = 30 #outro/credits time
for subdir, dirs, files in os.walk(sdir):
for file in sorted(files):
if(file.endswith(ext) and not file.startswith('.')):
vpath = os.path.join(subdir, file)
video = VideoFileClip(vpath)
sint = random.randint(vstart, int(video.duration)-vcreds)
frames = []
for i in range(imnum):
#example output: ./extracted/Adventure.Time.S03E17.Thank.You.1.png
fname = 'extracted/' + '.'.join(file.split('.')[:-1]) + '.' + str(i+1) + '.png'
#grab a random frame between intro and credits
sint = random.randint(vstart, int(video.duration)-vcreds)
sframe = video.get_frame(sint)
print(fname)
frames.append(fname)
imsave(fname, sframe)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment