ViThumbs/thumbgen.sh
2022-01-19 09:38:28 +01:00

76 lines
2.6 KiB
Bash

#!/bin/bash
if [ -z "$1" ]; then
echo "usage: ./thumbgen.sh INPUT [COLUMNS=5] [ROWS=5] [SIZE=1600]"
exit
fi
# Define variables
INPUT=$1
COLUMNS=$2
if [ -z "$COLUMNS" ]; then
COLUMNS=5
fi
ROWS=$3
if [ -z "$ROWS" ]; then
ROWS=5
fi
SIZE=$4
if [ -z "$SIZE" ]; then
SIZE=1600
fi
NFRAMES=$(echo "scale=0;$COLUMNS*$ROWS" | bc)
DURX=$(ffmpeg -i "$INPUT" 2>&1 | grep Duration | awk '{print $2}' | tr -d ,)
DURATION=$(ffmpeg -i "$INPUT" 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,// | sed 's@\..*@@g' | awk '{ split($1, A, ":"); split(A[3], B, "."); print 3600*A[1] + 60*A[2] + B[1] }')
RES=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$INPUT")
FILESIZE=$(du -sm "$INPUT" | awk '{print $1}')
FONT="/System/Library/Fonts/Supplemental/PTMono.ttc"
METADATA_PX=90
TMPDIR="/tmp/thumbnails-${RANDOM}/"
mkdir $TMPDIR
for (( VARIABLE=0; VARIABLE<NFRAMES; VARIABLE++ ))
do
OFFSET=$(echo "scale=2;$VARIABLE*$DURATION/$NFRAMES+$DURATION/$NFRAMES/2" | bc)
if [ $VARIABLE -gt 9 ];then
ZEROS="00"
if [ $VARIABLE -gt 99 ];then
ZEROS="0"
if [ $VARIABLE -gt 999 ];then
ZEROS=""
fi
fi
else
ZEROS="000"
fi
# Create thumbnails
ffmpeg -start_at_zero -copyts -ss $OFFSET -i "$INPUT" -vf "drawtext=fontfile=$FONT:fontsize=45:fontcolor=white::shadowcolor=black:shadowx=2:shadowy=2:box=1:boxcolor=black@0:x=(W-tw)/40:y=H-th-20:text='%{pts\:gmtime\:0\:%H\\\\\\:%M\\\\\:%S}'" -vframes 1 ${TMPDIR}$ZEROS$VARIABLE.png
done
# Merge thumbnails into tile image
ffmpeg -pattern_type glob -i "${TMPDIR}*.png" -filter_complex tile=${COLUMNS}x${COLUMNS}:margin=4:padding=4:color=white ${TMPDIR}output.png
# Output metadata to file
echo "Filename: $INPUT" >>${TMPDIR}metadata.txt
echo "Resolution: $RES" >>${TMPDIR}metadata.txt
echo "Filesize: $FILESIZE Mb" >>${TMPDIR}metadata.txt
echo "Duration: $DURX" >>${TMPDIR}metadata.txt
# Get dimensions of tile image
thewidth=$(ffmpeg -i ${TMPDIR}output.png 2>&1 |grep Video|awk '{ split( $6, pieces, /[x,]/ ) ; print pieces[1] }')
theheight=$(ffmpeg -i ${TMPDIR}output.png 2>&1 |grep Video|awk '{ split( $6, pieces, /[x,]/ ) ; print pieces[2] }')
# Redefine height & scale (according to $SIZE)
scaledheight=$(echo "scale=0;$theheight*$SIZE/$thewidth" | bc)
ffmpeg -i ${TMPDIR}output.png -vf scale=${SIZE}x${scaledheight} -vframes 1 ${TMPDIR}th.png
# Add space to the top of the image
finalheight=$(echo "$scaledheight+$METADATA_PX" | bc)
# Add Metadata
ffmpeg -f lavfi -i color=black:${SIZE}x${finalheight} -i ${TMPDIR}th.png \
-filter_complex "[0:v][1:v] overlay=0:$METADATA_PX,drawtext=fontfile=$FONT:fontsize=20:fontcolor=white:x=10:y=10:textfile=${TMPDIR}metadata.txt" \
-vframes 1 th${RANDOM}.png