Skip to content

Instantly share code, notes, and snippets.

@belgareth
Forked from DrSwagsalot/mkvextractTracks.sh
Last active January 8, 2023 10:14
Show Gist options
  • Save belgareth/cbe0fc3cf572e0f217675e638e06f60d to your computer and use it in GitHub Desktop.
Save belgareth/cbe0fc3cf572e0f217675e638e06f60d to your computer and use it in GitHub Desktop.
batch extract subtitles from mkv files
#!/bin/bash
# Extract all subtitles from every MKV file and save them with the same name as SRT files,
# with an optional prefix or suffix added to the file name.
# Copy to the folder where the MKV files live and run "./mkvextractTracks.sh [prefix] [suffix]" in terminal.
#
# info:
# mkvextract is used to extract the subtitles, so mkvtoolnix app (which contains the mkvextract binary) is used:
# https://mkvtoolnix.download/downloads.html
# please adjust below path to point to mkvextract or this script won't work
extractorPath='/usr/bin/mkvextract'
# Ensure we're running in the location of the script.
cd "`dirname $0`"
# Get the prefix and suffix from the command-line arguments
prefix=$1
suffix=$2
# Set the spinner characters
spin='-\|/'
# Count the number of MKV files
count=$(find . -maxdepth 1 -type f -name "*.mkv" | wc -l)
# Extract subtitles from each MKV file
i=1
for file in *; do
if [[ $file == *.mkv ]]; then
# Extract all subtitles from the file
$extractorPath tracks "$file" --no-video --no-audio "${prefix}${file//mkv/srt}${suffix}"
# Update the progress indicator
printf "\rProcessing file $i/$count"
i=$((i+1))
fi
done
echo "Complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment