Skip to content

Instantly share code, notes, and snippets.

@ChronoMonochrome
Last active April 16, 2023 10:43
Show Gist options
  • Save ChronoMonochrome/2e7b9950611f3d004b017bdedc938281 to your computer and use it in GitHub Desktop.
Save ChronoMonochrome/2e7b9950611f3d004b017bdedc938281 to your computer and use it in GitHub Desktop.
Make a looping animation from video with ffmpeg
  1. Trim video

ffmpeg -i input.mkv -ss 00:11:57 -to 00:12:27 -c:v copy -c:a copy trim.mkv

  1. Extract frames

ffmpeg -i trim.mkv frame_%05d.png

  1. Copy frames in a reverse order to make a loop
import os
import shutil

def loop(path):
	c = int(os.listdir(path)[-1][6:-4])

	for i in os.listdir(path)[::-1][1:]:
		if not i.startswith("frame_"):
			continue
		c += 1

		out_name = ("frame_%05d.png" % c)
		print(i, out_name)
		shutil.copy(os.path.join(path, i), os.path.join(path, out_name)
    
loop("path/to/dir/with/frames")
  1. Assemble webm animation from frames

ffmpeg -y -framerate 60 -start_number 1392 -i "frame_%5d.png" -vcodec libvpx -qmin 0 -qmax 50 -crf 18 -b:v 40M result.webm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment