Skip to content

Instantly share code, notes, and snippets.

@melmatsuoka
Last active May 3, 2024 03:52
Show Gist options
  • Save melmatsuoka/b87c4dc9374c52827582 to your computer and use it in GitHub Desktop.
Save melmatsuoka/b87c4dc9374c52827582 to your computer and use it in GitHub Desktop.
Generates thumbnail contact-sheets of all video files in the current working directory
#!/bin/bash
#
# Generates thumbnail contact sheets of all video files in current working directory.
#
# Script defaults to writing PNG contact sheets to the same folder, using the original
# video filename as the basename for the contact sheet
#
# More details: https://trac.ffmpeg.org/wiki/Create%20a%20thumbnail%20image%20every%20X%20seconds%20of%20the%20video
#
# NOTE: 'montage' requires that Ghostscript be installed, in order to be able to generate titles
# on the contact sheet: run 'brew install ghostscript' to install
function cleanup () {
rm -rf .snapshots/*
}
SNAPSHOT_INTERVAL="15" # time interval (in seconds) to grab each frame
if [ ! -d ".snapshots" ] # check for existence of screengrab temp folder in CWD
then mkdir .snapshots
fi
shopt -s nocaseglob # forces case insensitive extension globbing in the following for loop
for FILE in *.{wmv,avi,rm,ram,mpg,mpeg,mov,mp4,flv,asf};
do [ -e "$FILE" ] || continue;
trap cleanup SIGHUP SIGINT SIGTERM
# extract thumbnail frames every $SNAPSHOT_INTERVAL seconds. By telling FFmpeg to set the output files FPS option to
# a very low value, we made FFmpeg drop a lot of frames at the output, in order to achieve such a low frame
# rate, effectively having our thumbnails generated every X seconds
#
# thumbnails are written to a hidden .snapshots temp folder in the same folder the script was run in.
echo "------------------------------------------------"
echo -e "\nExtracting thumbnails for \"${FILE}\""
ffmpeg -loglevel warning -i "${FILE}" -f image2 -vf fps=fps=1/$SNAPSHOT_INTERVAL .snapshots/._"${FILE}"_%03d.png 2> /dev/null
# assemble the contact sheet, using ImageMagick's "montage" util
echo -e "Compiling contact sheet for \"${FILE}\""
montage .snapshots/"._${FILE}"_*.png -geometry +3+1 -title "${FILE}" "${FILE}.png"
# purge the temporary .snapshots folder
echo -e "Cleaning up tempfiles...\n"
rm -f .snapshots/._"${FILE}"_*.png
done
@Al3601
Copy link

Al3601 commented Nov 24, 2018

hi, thank you for this script, i have a problem when adapting it to check each subdirectories:

for d in *; do
#if [ -d "$d" ]; then
( cd "$d" && LINE 28 here)
fi
done

how can i adapt this to my need ?

@GaryStar
Copy link

A bit late, but still:

To search also in subfolders:

shopt -s globstar  # expand double asterisk to any number of directories
for FILE in **/*.{wmv,avi,rm,ram,mpg,mpeg,mov,mp4,flv,asf};

also extract just the name of the video file to be used when saving/merging images:

NAME="${FILE##*/}"

and use it in lines 41, 46, 51: replace _"${FILE}"_ with _"${NAME}"_

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