How can I efficiently create a collage of video thumbnails on Linux?
I want to create a collage of 20 screenshots from a video, arranged in a 5x4 grid, regardless of the video’s length. How can I do this efficiently on a Linux system?
Specifically, I’d like a way to automatically generate this collage of 20 thumbnails from the video, without having to manually select and arrange the screenshots. The number of thumbnails should always be 20, even if the video is longer or shorter.
Can you suggest a command-line tool or script that can handle this task efficiently on Linux? I’m looking for a solution that is automated and doesn’t require a lot of manual work.
Here's what I've tried but I only get 20 black boxes:
#!/bin/bash
# Check if input video exists
if [ ! -f "$1" ]; then
echo "Error: Input video file not found."
exit 1
fi
# Get video duration
duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$1")
# Calculate interval between frames
interval=$((duration / 20))
# Extract 20 frames from the video
for i in {1..20}; do
ffmpeg -ss $((interval * ($i - 1))) -i "$1" -vf scale=200:-1 -q:v 2 "${1%.*}_frame$i.jpg"
done
# Create collage
montage -mode concatenate -tile 5x4 -geometry +2+2 "${1%.*}_frame*.jpg" output_collage.jpg
# Clean up temporary files
rm "${1%.*}_frame*.jpg"
echo "Collage created: output_collage.jpg"
2 answers
Of course, being a swiss army knife ffmpeg
already has this capability builtin:
ffmpeg -i Example.mp4 -vf "select=not(mod(n\,20)),scale=200:-1,tile=5x4:padding=2:color=white" -vsync 0 -frames:v 1 output.jpg
The result is about the same:
0 comment threads
bash does not support floating point operations, so at least for a short video $interval
will always be 0
. That way you always get the same frame, which by chance probably was black for your video.
You notice that if you place an echo
in front of your ffmpeg
call to check the actual command. This resulted in this output for me:
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame0.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame1.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame2.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame3.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame4.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame5.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame6.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame7.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame8.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame9.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame10.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame11.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame12.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame13.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame14.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame15.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame16.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame17.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame18.jpg
ffmpeg -ss 0 -i Example.mp4 -vf scale=200:-1 -q:v 2 Example_frame19.jpg
As you can see, -ss
is always 0
.
You can use for example bc
to do the calculations (it's possible that you need to install it):
interval=$(echo "$duration / 20" | bc)
Afterwards the rest of your script mostly worked for me.
More optimizations:
for i in {0..19}; do
ffmpeg -ss $((interval * i)) -i "$1" -vf scale=200:-1 -q:v 2 -frames:v 1 -loglevel 0 "${1%.*}_frame$i.jpg"
done
- By using
0..19
instead of1..20
you can clean up the calculations for the wanted second. -
-frames:v 1
removes an error message -
-loglevel 0
removes unnecessary clutter
rm "${1%.*}"_frame*.jpg
fixes the non working removal of the temporary images.
0 comment threads