Skip to content

Instantly share code, notes, and snippets.

@DefKorns
Last active February 24, 2024 01:25
Show Gist options
  • Save DefKorns/ad94d128810c9ac63daf2b1538bb00b5 to your computer and use it in GitHub Desktop.
Save DefKorns/ad94d128810c9ac63daf2b1538bb00b5 to your computer and use it in GitHub Desktop.
Script to remove unwanted audio/subtitles tracks from MKV
#!/bin/bash
##
# @Description: Script to remove unwanted audio/subtitles tracks from MKV
##
AUDIO_LANGUAGE="und,eng"
SUBTITLE_LANGUAGE="pt,por,pt-PT"
SUBTITLE_ID="false"
LANGUAGE_ID="false"
DELETE_SOURCE="false"
show_usage() {
printf "Usage: $0 [options [parameters]]%s\n"
printf "\n"
printf "Options:\n"
printf " -p|--path, Provide the path.\n"
printf " -a|--audio, audio languages, comma separated (und,eng).\n"
printf " -s|--subtitle, subtitle languages, comma separated (pt,por,pt-PT).\n"
printf " -sid|--subtitleid, set specific subtitle languages as default and Forced.\n"
printf " -d|--delete, delete source files.\n"
printf " -i|--info, check video file properties.\n"
printf " USAGE: -p \"full path to video\" -i.\n"
printf " OR: -p full\ path\ to\ video -i.\n"
printf " -h|--help, Print help.\n"
exit 0
}
while [ -n "$1" ]; do
case "$1" in
--path | -p)
shift
FILES="$1"
;;
--audio | -a)
shift
AUDIO_LANGUAGE="$1"
;;
--subtitle | -s)
shift
SUBTITLE_LANGUAGE="$1"
;;
--subtitleid | -sid)
shift
SUBTITLE_ID="$1"
;;
--languageid | -lid)
shift
LANGUAGE_ID="$1"
;;
--delete | -d)
shift
DELETE_SOURCE="true"
;;
--info | -i)
shift
[ ! -f "$FILES" ] && show_usage
mkvinfo "${FILES}" >dump && grep -E '(Language|Name|track ID|Codec ID)' dump
[ -f dump ] && rm dump
exit 0
;;
*)
show_usage
;;
esac
if [ $# -gt 0 ]; then
shift
fi
done
handle_mkv_individual_files() {
local fullpath="$1"
local output="$2"
local audio_lang="$3"
local audio_lang_id="$4"
local sub_lang="$5"
local sub_lang_id="$6"
local delete="$7"
local path
path="$(dirname "${fullpath}")"
local file
file="$(basename "${fullpath}")"
echo "file $file"
echo "#############################################################################################################"
echo "### Processing $file..."
echo "#############################################################################################################"
if [ "${sub_lang_id}" = "false" ] && [ "${audio_lang_id}" = "false" ]; then
/usr/bin/mkvmerge -o "${output}/$file" -a "$audio_lang" -s "$sub_lang" "$path/$file"
elif [ "${sub_lang_id}" != "false" ] && [ "${audio_lang_id}" = "false" ]; then
/usr/bin/mkvmerge -o "${output}/$file" \
-a "$audio_lang" -s "$sub_lang" \
--default-track-flag "$sub_lang_id":yes \
--forced-display-flag "$sub_lang_id":yes \
"$path/$file"
elif [ "${sub_lang_id}" = "false" ] && [ "${audio_lang_id}" != "false" ]; then
/usr/bin/mkvmerge -o "${output}/$file" \
-a "$audio_lang" \
--default-track-flag "$audio_lang_id":yes \
--forced-display-flag "$audio_lang_id":yes \
-s "$sub_lang" \
"$path/$file"
else
/usr/bin/mkvmerge -o "${output}/$file" \
-a "$audio_lang" \
--default-track-flag "$audio_lang_id":yes \
--forced-display-flag "$audio_lang_id":yes \
-s "$sub_lang" \
--default-track-flag "$sub_lang_id":yes \
--forced-display-flag "$sub_lang_id":yes \
"$path/$file"
fi
echo "### ${output}/$file created..."
}
handle_mkv_files() {
local path="$1"
local output="$2"
local audio_lang="$3"
local audio_lang_id="$4"
local sub_lang="$5"
local sub_lang_id="$6"
local delete="$7"
if [ -f "$path" ]; then
handle_mkv_individual_files "$path" "$output" "$audio_lang" "$audio_lang_id" "$sub_lang" "$sub_lang_id" "$delete"
else
cd "$path" || exit
for file in *.mkv; do
handle_mkv_individual_files "$path/$file" "$output" "$audio_lang" "$audio_lang_id" "$sub_lang" "$sub_lang_id" "$delete"
done
if [ "${delete}" = "true" ]; then
find "${output}" -name '*.mkv' -exec mv -t "${path}/" {} +
[ -d "$output" ] && rm -rf "$output"
fi
exit 0
fi
}
[ -z "$FILES" ] && show_usage
[ ! -d "$FILES" ] && [ ! -f "$FILES" ] && show_usage
FOLDER="${FILES}"
[ -f "$FILES" ] && FOLDER="$(dirname "$FILES")"
OUTPUT_FOLDER="${FOLDER}/fixed"
[ ! -d "$OUTPUT_FOLDER" ] && mkdir -p "$OUTPUT_FOLDER"
handle_mkv_files "$FILES" "$OUTPUT_FOLDER" "$AUDIO_LANGUAGE" "$LANGUAGE_ID" "$SUBTITLE_LANGUAGE" "$SUBTITLE_ID" "$DELETE_SOURCE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment