Skip to content

Instantly share code, notes, and snippets.

@jeffchannell
Last active November 3, 2021 03:33
Show Gist options
  • Save jeffchannell/2f6d6db875d2b6259c5fdec9bfc5c5fc to your computer and use it in GitHub Desktop.
Save jeffchannell/2f6d6db875d2b6259c5fdec9bfc5c5fc to your computer and use it in GitHub Desktop.
Merge multiple files with chapters using ffmpeg
#!/usr/bin/env bash
# Usage: FFMETADATA=/path/to/metadata FFOUTPUT="My Output File.mp4" ./ffmerge.sh /path/to/*.wav
set -eo pipefail
# arguments required
if [ $# -eq 0 ]
then
>&2 echo "one or more files required"
exit 1
fi
filelist=$(mktemp)
audio=$(mktemp)
files=()
FFMETADATA="${FFMETADATA:?Missing FFMETADATA}"
FFOUTPUT="${FFOUTPUT:?Missing FFOUTPUT}"
while [ -f "$1" ]
do
outfile=$(mktemp)
# add file to array
files+=($outfile)
# extract audio to wav file
ffmpeg -y -i "$1" -vn -c:a copy -f wav "$outfile"
# build file list
echo "file '$outfile'" >> "$filelist"
# remove this file from the argument list
shift 1
done
# combine audio files
ffmpeg -y -f concat -safe 0 -i "$filelist" -c copy -f mp4 "$audio"
# combine audio and chapters
ffmpeg -y -i "$audio" -i "$FFMETADATA" -map_metadata 1 -c:a copy "$FFOUTPUT"
# clean up
rm -v $filelist $audio ${files[@]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment