Skip to content

Instantly share code, notes, and snippets.

@lisamelton
Created March 21, 2019 22:39
Show Gist options
  • Save lisamelton/534a27540d9eb2c78c8b17fb5019f42c to your computer and use it in GitHub Desktop.
Save lisamelton/534a27540d9eb2c78c8b17fb5019f42c to your computer and use it in GitHub Desktop.
Bash script to scan input video for commentary tracks and output `transcode-video` options to add those tracks.
#!/bin/bash
#
# commentary-tracks.sh
#
# Copyright (c) 2013-2019 Don Melton
#
die() {
echo "$(basename "$0"): $1" >&2
exit ${2:-1}
}
readonly input="$1"
if [ ! "$input" ]; then
die 'missing argument'
fi
if [ ! -f "$input" ]; then
die "no such file: $input"
fi
last_track_name=''
echo "$(HandBrakeCLI --scan --previews 2:0 --input "$input" 2>&1)" |
sed -n '/^ Stream #0[.:][0-9]\{1,\}[^ ]* Audio: /,$p' |
sed '/^[^ ]\{1,\}.*$/,$d' |
sed '/^ Stream #0[.:][0-9]\{1,\}[^ ]* Subtitle: /,$d' |
sed '/^ Metadata:$/d' |
sed -e :a -e '$!N;s/\n / /;ta' -e 'P;D' |
sed -n '/^ Stream #[^(]\{1,\}(eng): Audio: .* Commentary/p' |
sed 's/^ Stream #[0-9]\{1,\}[.:]\([0-9]\{1,\}\).* \{1,\}title \{1,\}: \(.*Commentary\).*$/\1=\2/' |
while read line; do
track_name="$(echo "$line" | sed 's/^[0-9]\{1,\}=//')"
if [ "$track_name" == "$last_track_name" ]; then
continue
fi
last_track_name="$track_name"
echo "$line"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment