Skip to content

Instantly share code, notes, and snippets.

@hsj51
Created December 30, 2022 19:21
Show Gist options
  • Save hsj51/21a97d5f198209d90904077b785200b6 to your computer and use it in GitHub Desktop.
Save hsj51/21a97d5f198209d90904077b785200b6 to your computer and use it in GitHub Desktop.
Snapshot Generator: generate snapshots/screenshots with embedded timestamps from your video using ffmpeg
#!/bin/sh
# Usage:
# script <video file path> <no of snapshots>
# Ex:
# ./snapshot.sh inputvideo.mp4 10
#file name
file=$1
#number of snapshots
number=$2
#seconds to skip from start
#useful to skip intro scenes
skip_seconds=300
#get duration of the input video
duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 $file)
duration=$(echo "$duration - $skip_seconds" | bc -l)
# -copyts => copy timestamp( when we skip second(-ss) the timestamp is still at zero, messes with timestamp on snapshots )
# fps=$number/$duration => generate output file with this fps rate
# tl;dr get a video with $number number of frames each taken at ($duration/$number)th or (1/fps)th second
# drawtext => add timestamp on the snapshots
# %{pts\:gmtime\:0\:%H\\\\\:%M\\\\\:%S}' => hh:mm:ss time format; 5 \(backslash) to skip `:` (see: https://video.stackexchange.com/a/22590)
# can also use %{pts \: hms} for hh:mm:ss:mm format
# image-%03d.png | -f image2 image-%03d.png => extract video frames to separate images
ffmpeg -hide_banner -start_at_zero -copyts -ss $skip_seconds -i $file -vf fps=$number/$duration,drawtext="text='Timestamp \: %{pts\:gmtime\:0\:%H\\\\\:%M\\\\\:%S}':x=(w-tw)/2: y=h-(2*lh): fontsize=30: fontcolor=yellow: box=1: boxcolor=black@0.5" image-%03d.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment