Created
November 12, 2021 14:50
-
-
Save thearrow/c6c6e1a6fa8e419d6199587882a5bf51 to your computer and use it in GitHub Desktop.
Add a downmixed stereo track to a video file using ffmpeg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# USAGE: | |
# add_stereo_track *.mkv | |
# | |
# Will downmix a 5.1 track and add a new stereo audio track to the file. | |
# Will create a new file (same format as original, with other tracks copied, including subs) | |
# and rename the original with a .bak extension. | |
set -o nounset | |
set -o errexit | |
trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR | |
set -o errtrace | |
set -o pipefail | |
IFS=$'\n\t' | |
shopt -s nullglob | |
############################################################################### | |
# Program Functions | |
############################################################################### | |
_add_track_for_all_files() { | |
FILES=("$@") | |
for f in "${FILES[@]}" | |
do | |
echo "Processing $f..." | |
BACKUP_FILENAME="$f.bak" | |
# create backup copy | |
cp "$f" "$BACKUP_FILENAME" | |
TEMP_FILENAME="stereo.$f" | |
ffmpeg -i "$f" -y -ignore_unknown -map 0:v -c:v copy \ | |
-map "0:a:0?" -c:a:0 copy \ | |
-map "0:a:0?" -c:a:1 aac -b:a:1 192k -ac 2 -metadata:s:a:1 title="2.0 Stereo" \ | |
-map "0:a:1?" -c:a:2 copy \ | |
-map "0:a:2?" -c:a:3 copy \ | |
-map "0:a:3?" -c:a:4 copy \ | |
-map "0:a:4?" -c:a:5 copy \ | |
-map "0:a:5?" -map "0:s?" -c:s copy \ | |
"$TEMP_FILENAME" | |
echo "Deleting original file and replacing it with processed version..." | |
rm "$f" | |
mv "$TEMP_FILENAME" "$f" | |
echo "=====================================================" | |
echo "Finished processing $f" | |
echo "=====================================================" | |
done | |
} | |
############################################################################### | |
# Main | |
############################################################################### | |
# Description: | |
# Entry point for the program, handling basic option parsing and dispatching. | |
_main() { | |
_add_track_for_all_files "$@" | |
} | |
# Call `_main` after everything has been defined. | |
_main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment