Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

How can I efficiently create a collage of video thumbnails on Linux?

+3
−0

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"
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

2 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+1
−0

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.

Output image of the script, a 5x4 grid of video images

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 of 1..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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

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:

Output image of ffmpeg, a 5x4 grid of video images

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »