Skip to content

Instantly share code, notes, and snippets.

@madskjeldgaard
Created January 31, 2025 11:25
Show Gist options
  • Save madskjeldgaard/0ba0799926383c4b7634e6753ef4efff to your computer and use it in GitHub Desktop.
Save madskjeldgaard/0ba0799926383c4b7634e6753ef4efff to your computer and use it in GitHub Desktop.
Combine a still image with an audio file into a video file for social media using ffmpeg
#!/bin/bash
# This script combines an image and an audio file into a video using ffmpeg.
# It prompts the user for the paths to the image and audio files, an optional
# fixed length in seconds, and the output file name. The script ensures that
# the paths are properly quoted and constructs the appropriate ffmpeg command.
# Function to add quotes around a path if not already present
add_quotes() {
local path="$1"
if [[ "$path" != \"*\" ]]; then
path="\"$path\""
fi
echo "$path"
}
# Prompt user for image and audio file paths
read -p "Enter the path to the image file: " image_path
read -p "Enter the path to the audio file: " audio_path
# Add quotes around paths if necessary
image_path=$(add_quotes "$image_path")
audio_path=$(add_quotes "$audio_path")
# Prompt user for fixed length in seconds
read -p "Enter the fixed length in seconds (leave empty for full audio length): " length
# Prompt user for output file name
read -p "Enter the output file name (e.g., output.mp4): " output_file
# Construct the ffmpeg command
ffmpeg_command="ffmpeg -loop 1 -i $image_path -i $audio_path -c:v libx264 -c:a aac -b:a 256k -shortest -preset faster -pix_fmt yuv420p -tune stillimage \"$output_file\""
# Add length parameter if provided
if [[ -n "$length" ]]; then
ffmpeg_command="ffmpeg -loop 1 -i $image_path -i $audio_path -c:v libx264 -c:a aac -b:a 256k -shortest -preset faster -pix_fmt yuv420p -tune stillimage -t $length \"$output_file\""
fi
# Execute the ffmpeg command
echo "Executing: $ffmpeg_command"
eval $ffmpeg_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment