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"
  • NegativeInf@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    2 months ago

    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"
    
    
  • thingsiplay@beehaw.org
    link
    fedilink
    arrow-up
    4
    ·
    2 months ago

    If you already have the screenshot files, then ImageMagick can create collages from multiple images. I’ve done that myself too. So that part can be easily automated through scripts.

    # program input-files frame input-size output-file
    montage shadow*.png -frame 5x4 -geometry 680x160 ./collage.png
    

    In the above command montage is a tool that comes with ImageMagick. First argument is a list of files to as input, here all .png files starting with name shadow*, so it does not load up the existing collage.png file (which is the output). Then you specify the frames, how its ordered for each row. Then the size for the input images. Here all images have the same size. And at last you specify a single filename as output.