2022-10-07 20:52:55 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
colorprint() {
|
|
|
|
echo -e "\e[31m${1}\e[0m"
|
|
|
|
}
|
|
|
|
SCRIPT_NAME=$(basename "$0")
|
|
|
|
test -z "${1}" && echo "File not specified or doesn't exist, aborting. Usage: $SCRIPT_NAME \$FILENAME \$LANGUAGE_TO_KEEP" && exit 1
|
|
|
|
FILENAME=$1
|
|
|
|
# default language to keep is second parameter, or japanese if unspecified
|
|
|
|
LANGUAGE_TO_KEEP="${2:-jpn}"
|
|
|
|
echo -n "Processing "
|
|
|
|
colorprint "$FILENAME"
|
2022-10-07 22:30:24 +00:00
|
|
|
|
|
|
|
# capture JSON output and remove the excessively large code_private_data field
|
|
|
|
JSON_OUTPUT=$(mkvmerge -J "$FILENAME" | jq '[ .tracks[] | select(.type=="audio") | del(.properties.codec_private_data) ]')
|
|
|
|
TRACK_COUNT=$(echo "$JSON_OUTPUT" | jq length)
|
|
|
|
# check if more than 1 audio track is present
|
|
|
|
[[ $TRACK_COUNT -lt 2 ]] && echo -e "\tFile has only 1 track, skipping." && exit 1
|
|
|
|
# check if tracks other than the one to keep are present
|
2022-10-08 14:23:30 +00:00
|
|
|
UNDESIRED_TRACK_COUNT=$(echo "$JSON_OUTPUT" | jq "[.[] | select(.properties.language!=\"${LANGUAGE_TO_KEEP}\")] | length")
|
|
|
|
[[ $UNDESIRED_TRACK_COUNT -eq 0 ]] && echo -e "\tFile has only '$LANGUAGE_TO_KEEP' tracks, skipping." && exit 1
|
2022-10-07 23:11:44 +00:00
|
|
|
FILENAME_EXT=${FILENAME##*.}
|
2022-10-07 22:30:24 +00:00
|
|
|
echo -e "\tRemoving all audio tracks except '$LANGUAGE_TO_KEEP' tracks..."
|
2022-10-07 23:11:44 +00:00
|
|
|
TMPFILE=$(mktemp)
|
|
|
|
echo -e "\tOutputting to temporary file ${TMPFILE}.${FILENAME_EXT}..."
|
|
|
|
pv "$FILENAME" | ffmpeg -i pipe:0 -map 0:v -map 0:a:m:language:${LANGUAGE_TO_KEEP} -map 0:s -c copy -v error "${TMPFILE}.${FILENAME_EXT}"
|
|
|
|
echo -e "\tReplacing ${FILENAME} with ${TMPFILE}..."
|
|
|
|
mv "${TMPFILE}.${FILENAME_EXT}" "$FILENAME"
|