Skip to content

Instantly share code, notes, and snippets.

@Thomashighbaugh
Created November 22, 2023 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thomashighbaugh/9fbeed86e02895307ae183b39867fb8c to your computer and use it in GitHub Desktop.
Save Thomashighbaugh/9fbeed86e02895307ae183b39867fb8c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Check if the required commands are available
if ! command -v ffmpeg &>/dev/null; then
echo "Error: FFmpeg is not installed. Please install FFmpeg."
exit 1
fi
if ! command -v convert &>/dev/null; then
echo "Error: ImageMagick's 'convert' command is not installed. Please install ImageMagick."
exit 1
fi
# Get user inputs
read -p "Enter the path to the directory containing audio files (mp3/m4a): " audio_dir
read -p "Enter the path to the directory containing images: " image_dir
# Navigate to the audio directory
cd "$audio_dir" || exit
# Loop through audio files
for audio_file in *.mp3 *.m4a; do
if [ -f "$audio_file" ]; then
# Get the audio file name without extension
audio_name="${audio_file%.*}"
# Find a random image from the user-supplied directory
image_list=("$image_dir"/*)
random_image="${image_list[RANDOM % ${#image_list[@]}]}"
# Create a temporary video file name
temp_video="${audio_name}_temp.mp4"
# Combine audio with image using FFmpeg
ffmpeg -loop 1 -i "$random_image" -i "$audio_file" -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -c:a copy -shortest "$temp_video"
# Clean up image file
rm "$random_image"
# Rename temporary video to match the audio file name
mv "$temp_video" "${audio_name}.mp4"
echo "Converted $audio_file to video."
fi
done
echo "Conversion complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment