Last active
April 8, 2021 14:10
-
-
Save vsubhash/534d64ffd23798bfdd09b3071173773e to your computer and use it in GitHub Desktop.
This BASH script uses FFMPEG to create thumbnails from a video. The thumbnails are then stitched together as a gallery using ImageMagick.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# BASH script to create a 3x3 thumbnail gallery for a video | |
# Accepts the pathname of the video as argument ($1) | |
########################################################### | |
# Floating point number functions by Mitch Frazier | |
# Adapted from | |
# https://www.linuxjournal.com/content/floating-point-math-bash | |
########################################################### | |
# Default scale used by float functions. | |
float_scale=2 | |
# Evaluate a floating point number expression. | |
function float_eval() { | |
local stat=0 | |
local result=0.0 | |
if [[ $# -gt 0 ]]; then | |
result=$(echo "scale=$float_scale; $*" | bc -q 2>/dev/null) | |
stat=$? | |
if [[ $stat -eq 0 && -z "$result" ]]; then | |
stat=1 | |
fi | |
fi | |
echo $result | |
return $stat | |
} | |
# Evaluate a floating point number conditional expression. | |
function float_cond() { | |
local cond=0 | |
if [[ $# -gt 0 ]]; then | |
cond=$(echo "$*" | bc -q 2>/dev/null) | |
if [[ -z "$cond" ]]; then | |
cond=0 | |
fi | |
if [[ "$cond" != 0 && "$cond" != 1 ]]; then | |
cond=0 | |
fi | |
fi | |
local stat=$((cond == 0)) | |
return $stat | |
} | |
########################################################### | |
# Floating point number functions end | |
#Prefix for images | |
FILE_NAME=${1%.*} | |
#echo $FILE_NAME | |
NUMBER_OF_THUMBNAILS=9 | |
MOVIE="$1" | |
COUNTER=0 | |
#Number of seconds | |
MOVIE_DURATION=`ffprobe -show_entries "format=duration" -of "default=nokey=1:noprint_wrappers=1" -i $MOVIE 2> /dev/null` | |
echo $MOVIE_DURATION | |
MOVIE_WIDTH=`ffprobe -select_streams v:0 -show_format -show_streams $MOVIE 2> /dev/null | grep "^width=" | sed -e "s/width=//"` | |
MOVIE_HEIGHT=`ffprobe -select_streams v:0 -show_format -show_streams $MOVIE 2> /dev/null | grep "^height=" | sed -e "s/height=//"` | |
echo "$MOVIE_WIDTH x $MOVIE_HEIGHT" | |
TW=`float_eval "$MOVIE_WIDTH/3"` | |
TH=`float_eval "$MOVIE_HEIGHT/3"` | |
THUMB_WIDTH=${TW%.*} | |
THUMB_HEIGHT=${TH%.*} | |
#echo "$THUMB_WIDTH x $THUMB_HEIGHT" | |
for i in `seq $NUMBER_OF_THUMBNAILS` | |
do | |
let COUNTER=COUNTER+1 | |
#echo $COUNTER | |
LOCATION_FLOAT=`float_eval "($i-0.5)*$MOVIE_DURATION/$NUMBER_OF_THUMBNAILS"` | |
#echo $LOCATION_FLOAT | |
LOCATION_INT=${LOCATION_FLOAT%.*} | |
#echo $LOCATION_INT | |
# Create the thumbnails | |
ffmpeg -y -i $MOVIE -ss $LOCATION_INT -vf select='eq(pict_type\,I)' -vframes 1 -s `echo $THUMB_WIDTH`x`echo $THUMB_HEIGHT` "`echo $FILE_NAME`_$COUNTER.jpg" | |
done | |
# Create the gallery | |
montage -density 96 -tile 3x3 -geometry +4+4 -border 1 "`echo $FILE_NAME`*.jpg" "`echo $FILE_NAME`-thumbnail.jpg" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an implementation of the pseudocode found at superuser.com.