Requires ffmpeg and imagemagick
#!/bin/bash
# Check if input video file is provided and exists
if [ $# -lt 1 ]; then
echo "Usage: $0 <input_video>"
exit 1
fi
input_video="$1"
if [ ! -f "$input_video" ]; then
echo "Error: Input video file not found."
exit 1
fi
# Get video duration in seconds (floating-point)
duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$input_video")
# Check if the duration was successfully extracted
if [ -z "$duration" ]; then
echo "Error: Could not retrieve video duration."
exit 1
fi
# Calculate fps as 20 frames divided by the duration
fps=$(echo "20 / $duration" | bc -l)
# Create a directory to store frames
mkdir -p frames
# Extract 20 frames from the video using the fps filter
ffmpeg -i "$input_video" -vf "fps=$fps,scale=200:-1" -frames:v 20 "frames/frame_%02d.jpg" -loglevel error
# Create the collage using ImageMagick's montage tool
montage -mode concatenate -tile 5x4 -geometry +2+2 frames/frame_*.jpg output_collage.jpg
# Clean up temporary files (delete frames directory)
rm -r frames
echo "Collage created: output_collage.jpg"