Skip to content

Instantly share code, notes, and snippets.

@gotev
Last active November 14, 2022 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gotev/8ff7875106720f1731ceb344ba271dc2 to your computer and use it in GitHub Desktop.
Save gotev/8ff7875106720f1731ceb344ba271dc2 to your computer and use it in GitHub Desktop.
Extract Frames from a video. Useful to extract presentation slides and key moments.
#!/bin/bash -e
# Extract Key Frames from a video and print timestamp on each frame (works with ffmpeg version 5.1.2)
#
# Credits:
# - https://jdhao.github.io/2021/12/25/ffmpeg-extract-key-frame-video/
# - https://superuser.com/questions/575854/can-ffmpeg-extract-images-from-a-video-with-timestamps
#
# First parameter: input video
# Second parameter: output directory
# Example ./extract-key-frames myvideo.mp4 outdir/
#
# Info about: select='gt(scene,0.01)'
# The value for scene is between 0 and 1, which measures the pixel level difference between
# current frame and previous frame. We can tweak this value to generate different number of
# output images. Intuitively, the smaller the value, the larger number of images we will
# extract. Typically, a value of 0.3 or 0.4 may be enough.
if [ ! -d "$2" ]
then
echo "Creating $2 ..."
mkdir -p "$2"
fi
ffmpeg -i "$1" -vf "select='gt(scene,0.01)', drawtext=fontfile=/usr/share/fonts/TTF/Vera.ttf: text='%{pts\:hms}': x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000000@1" -vsync vfr "$2/frame-%2d.jpg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment