Skip to content

Instantly share code, notes, and snippets.

@CrispyBaguette
Last active May 9, 2020 07:18
Show Gist options
  • Save CrispyBaguette/f55ea67796ae2e47e93a66cc4d0d19b9 to your computer and use it in GitHub Desktop.
Save CrispyBaguette/f55ea67796ae2e47e93a66cc4d0d19b9 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Create the target dir
mkdir Completed
for mkvFile in *.mkv; do
# Get the file name without the extension
filename=$(basename -- "$mkvFile")
filename="${filename%.*}"
# Count the vobsub tracks
numTracks=$(mkvinfo "$mkvFile" | grep -c "S_VOBSUB")
if [ $numTracks -eq 0 ]; then
echo "No tracks to convert !"
continue
fi
# Get the vobsub part of the output, and parse to get the track number
for subTrack in $(mkvinfo "$mkvFile" | grep -B4 "VOBSUB" | grep "mkvextract" | cut -d ":" -f 3 | tr -d -c 0-9); do
# Try to parse the track language
trackLang=$(mkvinfo "$mkvFile" | grep -B4 "S_VOBSUB" | grep -C3 "Track UID: $subTrack" | grep "Language:" | cut -d ":" -f 2 | tr -d '\040\011\012\015')
if [ -z "$trackLang" ]; then
trackLang="eng"
fi
# Extract the vobsub track
mkvextract tracks "$mkvFile" $subTrack:"$filename"
# Convert to .srt
/mnt/c/Program\ Files/Subtitle\ Edit/SubtitleEdit.exe /convert "$filename".sub srt
if [ $? -ne 0 ]; then
echo "Failed to convert subtitle to SRT!"
continue
fi
# Merge the .srt into the .mkv
mkvmerge -o "_$mkvFile" "$mkvFile" --language 0:$trackLang --default-track "0:yes" "$filename.srt"
if [ $? -ne 0 ]; then
echo "Failed to merge subtitle to _$mkvFile!"
continue
fi
# Move and cleanup
mv "_$mkvFile" "Completed/$mkvFile"
rm "$filename.sub" "$filename.idx" "$filename.srt"
done
done
@CrispyBaguette
Copy link
Author

CrispyBaguette commented May 9, 2020

Script I've used to add .srt tracks to old DVD rips with vobsub tracks. I'm using WSL to run SubtitleEdit in a bash script (thanks Microsoft !) because I wasn't able to run it on macOS with mono, due to the Winforms 32 bits dependencies (thanks Apple !).

There's room for improvement, the subtitles might not be perfect (SubtitleEdit uses OCR for the conversion), and I can't guarantee that it won't break, but finding this would have saved me a couple of hours.

It requires MKVToolNix.

It's based on a script I found here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment