1

I have a BUNCH of mkv files that I want to play on my Blue-ray player. However it won't read the subs unless they are out of the file. I need a way to extract and rename them. I want to give them the same name as the mkv files they came out of so my player will read them. The subtitles are in .ass format.

I saw a script floating around that was supposed to do this. It did not work.

I have mkvExtractor and all that installed, but I have WAY too many files to do it all by hand.

This is the script I tried.

#!/bin/bash
# Extract subtitles from each MKV file in the given directory

# If no directory is given, work in local dir
if [ "$1" = "" ]; then
  DIR="."
else
  DIR="$1"
fi

# Get all the MKV files in this dir and its subdirs
find "$DIR" -type f -name '*.mkv' | while read filename
do
  # Find out which tracks contain the subtitles
  mkvmerge -i "$filename" | grep 'subtitles' | while read subline
  do
    # Grep the number of the subtitle track
    tracknumber=`echo $subline | egrep -o "[0-9]{1,2}" | head -1`

    # Get base name for subtitle
    subtitlename=${filename%.*}

    # Extract the track to a .tmp file
    `mkvextract tracks "$filename" $tracknumber:"$subtitlename.srt.tmp" > /dev/null 2>&1`
    `chmod g+rw "$subtitlename.srt.tmp"`


    # Do a super-primitive language guess: ENGLISH
    langtest=`egrep -ic ' you | to | the ' "$subtitlename".srt.tmp`
    trimregex=""



    # Check if subtitle passes our language filter (10 or more matches)
    if [ $langtest -ge 10 ]; then
      # Regex to remove credits at the end of subtitles (read my reason why!)
      `sed 's/\r//g' < "$subtitlename.srt.tmp" \
        | sed 's/%/%%/g' \
        | awk '{if (a){printf("\t")};printf $0; a=1; } /^$/{print ""; a=0;}' \
        | grep -iv "$trimregex" \
        | sed 's/\t/\r\n/g' > "$subtitlename.srt"`
      `rm "$subtitlename.srt.tmp"`
      `chmod g+rw "$subtitlename.srt"`
    else
      # Not our desired language: add a number to the filename and keep anyway, just in case
      `mv "$subtitlename.srt.tmp" "$subtitlename.$tracknumber.srt" > /dev/null 2>&1`
    fi

Am I doing something wrong?

  • It might be best to include the script you tried. Maybe somebody here can take a look at the script and tell you why it doesn't work. – Galgalesh Dec 30 '14 at 17:54
  • What do you mean by 'it did not work'? Did it produce an error? Did it name the files incorrectly? Did it put them in the wrong directory? Did it format your computer? – v010dya Jan 4 '15 at 16:03
  • One thing that i found is that your question asks about ASS, but you're writing .SRT subtitles. Is this the error? – v010dya Jan 4 '15 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.