Convert all ASS subtitles to SRT for all mkv in a folder

This script will create a copy of your mkv files with all subtitles tracks in ASS converted to SRT

Prerequisites: sudo apt-get install mkvtoolnix ffmpeg

#!/bin/bash
# ===========================================================================
# Title       : ass-to-srt.sh
# Description : Convert all ASS subtitles to SRT for all mkv in a folder
# Author      : https://thelazyfox.xyz
# ===========================================================================
for x in *.mkv
do
    trackIds=""
    filename="${x%.*}"
    if mkvinfo "$x" | grep -q "S_TEXT/ASS"
    then
        echo "## Converting $x"
    	#GET ALL ASS TRACKS AND REMOVE THEM
        for subTrack in $(mkvinfo "$x" | grep -B6 "S_TEXT/ASS" | grep "Track UID" | cut -d ":" -f 2)
        do
            trackId=$(mkvinfo "$x" | grep -B1 "$subTrack" | grep "Track number" | cut -d ":" -f 3)
            trackId=${trackId::-1}
            trackIds+="$trackId,"
            trackIds="${trackIds//[[:blank:]]/}"
        done
        trackIds="${trackIds::-1}"
        mkvmerge --quiet -o "$filename-CUSTOM.mkv" --subtitle-tracks !"$trackIds" "$x"

        #NOW GO THROUGH AGAIN TO CONVERT AND MERGE
        for subTrack in $(mkvinfo "$x" | grep -B6 "S_TEXT/ASS" | grep "Track UID" | cut -d ":" -f 2)
        do
            numberOfTracks=$((numberOfTracks+1))
            trackId=$(mkvinfo "$x" | grep -B1 "$subTrack" | grep "Track number" | cut -d ":" -f 3)
            trackId=${trackId::-1}
            trackLanguage=$(mkvinfo "$x" | grep -A7 "Track UID: $subTrack" | grep "Language:" | cut -d ":" -f 2 | tr -d [:blank:])
            trackName=$(mkvinfo "$x" | grep -A9 "Track UID: $subTrack" | grep "Name:" | cut -d ":" -f 2)
            trackIds+="$trackId,"
            trackIds="${trackIds//[[:blank:]]/}"
            ffmpeg -y -loglevel error -i "$x" -map 0:"$subTrack"? "filename-$subTrack".srt
            srtList+="$x-$subTrack.srt "
            if [ $? -eq 1 ]
            then
                echo "Failed to convert subtitle to SRT!"
                exit 1
            else
                mkvmerge --quiet -o "$filename-CUSTOM-$subTrack.mkv" "$filename-CUSTOM.mkv" --language "0:$trackLanguage"  --track-name "0:$trackName"  "filename-$subTrack".srt
                rm "$filename-CUSTOM.mkv"
                mv "$filename-CUSTOM-$subTrack.mkv" "$filename-CUSTOM.mkv"
            fi
        done
        rm *srt
        mv "$x" "$filename.bak"
    else
        echo "## No S_TEXT/ASS track in $x"
    fi
done
Looking for help?
If you are looking for some help or want a quick chat, please head over to the Discord Community!