Skip to content

Instantly share code, notes, and snippets.

@Suleman-Elahi
Created April 16, 2025 14:54
Show Gist options
  • Save Suleman-Elahi/c65d5864bc09f7b6cfc421b05178a0c3 to your computer and use it in GitHub Desktop.
Save Suleman-Elahi/c65d5864bc09f7b6cfc421b05178a0c3 to your computer and use it in GitHub Desktop.
Generate Vertical Scrolling Video from Long Images using FFmpeg
#!/bin/bash
# Check if an input image is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <input_image>"
exit 1
fi
INPUT_IMAGE="$1"
OUTPUT_VIDEO="output_$(basename "${INPUT_IMAGE%.*}").mp4"
# Check if the input image exists
if [ ! -f "$INPUT_IMAGE" ]; then
echo "Error: Image '$INPUT_IMAGE' not found"
exit 1
fi
# Get image dimensions using ffprobe
DIMENSIONS=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$INPUT_IMAGE")
WIDTH=$(echo "$DIMENSIONS" | cut -d'x' -f1)
HEIGHT=$(echo "$DIMENSIONS" | cut -d'x' -f2)
# Ensure height is even for libx264 compatibility
EVEN_HEIGHT=$(( ($HEIGHT + 1) / 2 * 2 ))
# Calculate scroll parameters
CROP_HEIGHT=1920 # Instagram Reels height
DURATION=20 # 20 seconds
SCROLL_END=$(( $EVEN_HEIGHT - $CROP_HEIGHT )) # Where the crop ends (bottom of image)
# Run FFmpeg command with dynamic dimensions
ffmpeg -loop 1 -i "$INPUT_IMAGE" \
-vf "scale=1080:${EVEN_HEIGHT}:force_original_aspect_ratio=decrease,pad=1080:${EVEN_HEIGHT}:(ow-iw)/2:(oh-ih)/2,crop=1080:${CROP_HEIGHT}:0:${SCROLL_END}*t/${DURATION}" \
-t "$DURATION" -c:v libx264 -pix_fmt yuv420p "$OUTPUT_VIDEO"
# Check if FFmpeg succeeded
if [ $? -eq 0 ]; then
echo "Video generated: $OUTPUT_VIDEO"
else
echo "Error: Video generation failed"
exit 1
fi
@Suleman-Elahi
Copy link
Author

How It Works:

Dimension Detection:

Uses ffprobe to extract the image’s width and height.

Adjusts the height to be even (e.g., 6581 → 6582) for libx264.

Dynamic FFmpeg Command:

scale=1080:${EVEN_HEIGHT}: Scales the image to a width of 1080 (Reels-compatible) and the adjusted height.

pad=1080:${EVEN_HEIGHT}: Ensures the dimensions are exactly as specified.

crop=1080:${CROP_HEIGHT}:0:${SCROLL_END}*t/${DURATION}: Crops to 1080x1920, scrolling from y=0 (top) at t=0 to 
y=EVEN_HEIGHT-1920 (bottom) at t=20 seconds.

Output:

Generates a video named output_<original_filename>.mp4

Reports success or failure.

Prerequisites:

FFmpeg : Must be installed (ffmpeg and ffprobe commands available). Install via:
Ubuntu/Debian: sudo apt-get install ffmpeg

Steps to Use:

Save the Script : Create a file named create_reels_video.sh and paste the script above.

Make it executable : chmod +x create_reels_video.sh

Run the Script For a single image : bash ./create_reels_video.sh yourimage.png

This generates output_yourimage.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment