Skip to content

Instantly share code, notes, and snippets.

@LeonB
Last active August 17, 2016 22:07
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 LeonB/0a9c7f944d0e1bf2c8e6fd2de0bbecea to your computer and use it in GitHub Desktop.
Save LeonB/0a9c7f944d0e1bf2c8e6fd2de0bbecea to your computer and use it in GitHub Desktop.
#!/bin/bash
# Tested with ffmpeg 2.8.4
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
echo "Please provide audio file, video file and output file"
echo "For example: audio.mp3 video.mp4 output.mp4"
exit
fi
audio=$(realpath -q $1)
video=$(realpath -q $2)
output=$(realpath -q $3)
if [ -z "$audio" ]; then
echo "Couldn't find audio file ${1}"
exit
fi
if [ -z "$video" ]; then
echo "Couldn't find audio file ${2}"
exit
fi
# Get audio & video lengths
audio_duration=$(ffprobe -i ${audio} -show_entries format=duration -v quiet -of csv="p=0")
video_duration=$(ffprobe -i ${video} -show_entries format=duration -v quiet -of csv="p=0")
# Check to how many audio loops should be done
if [ $(echo " $audio_duration > $video_duration" | bc) -eq 1 ]; then
# Audio is longer
n_loops=$(echo "(${audio_duration} / ${video_duration}) + 1"|bc)
else
# Video is shorter or equal to audio, just do one loop
n_loops=1
fi
ffmpeg -i ${audio} -f concat -i <(for i in $(seq 1 ${n_loops}); do printf "file '%s'\n" ${video}; done) -c:v copy -c:a copy -shortest ${output}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment