Skip to content

Instantly share code, notes, and snippets.

@Wikinaut
Last active December 19, 2022 19:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wikinaut/091938db77406bfc0cd45d78477ec5c1 to your computer and use it in GitHub Desktop.
Save Wikinaut/091938db77406bfc0cd45d78477ec5c1 to your computer and use it in GitHub Desktop.
fax-a-video
#!/bin/bash
# Script to create one or more images of a number n of equally spaced frames of video.
# Adding a caption with a prefix and counting number.
# this version 20221219
# init 20210321
# Licence: WTFPL
# see https://github.com/Wikinaut/utils/wiki#How_to_extract_every_nth_frame
# how to convert a video to a sequence of its frames as single images
# ffmpeg -i in.mp4 %04d.jpg
# How to extract every n-th frame, every keyframe, or frames every n-th second:
# ffmpeg -i in.mp4 -vf fps=1/5 %04d.jpg
red="\e[1;31m"
yellow="\e[1;33m"
nocol="\e[0m"
if [ $# -lt 5 ] ; then
echo "Add a prefix text and numbers to images of a video and collage-arrange them"
echo
echo "Usage:"
echo "fax-a-video <videofile> <pick-n-frames> <ColumnxRow (tiles)> <pointsize> <prefix>"
echo
exit
fi
nframes=$2
tiles=$3
pointsize=$4
prefix=$5
# get number of frames
nbframes=$(ffprobe -select_streams v -show_streams "$1" 2>/dev/null | grep nb_frames | sed -e 's/nb_frames=//')
# https://www.reddit.com/r/ffmpeg/comments/feort1/how_to_generate_video_thumbnails_with_evenly/
duration=$(ffprobe -v 0 -show_entries format=duration -of default=nw=1:nk=1 "$1")
echo -e "$red$1: $nbframes ($duration seconds).$nocol"
# ffmpeg -i "$1" \
# -vsync vfr \
# -vf "select=(isnan(prev_selected_t)*gte(t\,$duration/$2))+gte(t-prev_selected_t\,$duration/13),scale=160:120:force_original_aspect_ratio=decrease,tile=4x3" \
# "${1%.*}-%05d.jpg"
ffmpeg -i "$1" \
-vsync vfr \
-vf "select=(isnan(prev_selected_t)*gte(t\,$duration/($nframes+1)))+gte(t-prev_selected_t\,$duration/($nframes+1))" \
"${1%.*}-%05d.jpg"
number=1
for f in "${1%.*}"-*.jpg; do
convert "$f" -gravity south -font Hack-Regular -pointsize $pointsize -undercolor white -fill black -annotate 0 "${prefix}${number}" miff:-
((number++))
done | montage -geometry 'x600>+12+12' -tile $tiles - "${1%.*}.collage.jpg"
# cleaning up
rm "${1%.*}"-*.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment