Skip to content

Instantly share code, notes, and snippets.

@dkorunic
Last active December 16, 2022 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkorunic/51d0a2b93d7fcd46d19a86e2b6697d98 to your computer and use it in GitHub Desktop.
Save dkorunic/51d0a2b93d7fcd46d19a86e2b6697d98 to your computer and use it in GitHub Desktop.
Simple tool to strip all languages except the desired one from a mkv file
#!/bin/sh
# Copyright (C) 2022 Dinko Korunic
# Simple tool to strip all languages except the desired one from a bunch of mkv files in a current folder
# Depends: mkvtoolnix
LANG_CODE="hr"
LANG_TRACK_COUNT=$(mkvinfo -s "$1" | head -50 | grep -c '^Track .*: audio')
if [ "${LANG_TRACK_COUNT}" -eq 0 ]; then
echo "Unable to find any language track"
exit 1
fi
if [ "${LANG_TRACK_COUNT}" -eq 1 ]; then
echo "There is only a single language, no need to convert"
exit 1
fi
LANG_TRACK=$(mkvinfo -s "$1" | head -10 | grep "^Track .*: audio.*language: ${LANG_CODE}" | grep -oP '(?<=track ID: )\d+')
if [ -z "${LANG_TRACK}" ]; then
echo "Unable to find ${LANG_CODE} language"
exit 1
fi
mkvmerge -o tmp.mkv --audio-tracks "${LANG_TRACK}" "$1" && mv tmp.mkv "$1"
rm -f tmp.mkv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment