Jump to content

How to batch convert subtitles inside an mkv from .ass to .srt

Go to solution Solved by WereCatf,
7 minutes ago, elfensky said:

it seems to convert, but it seems not to be able to write the file to disk. I tried running with sudo, no difference

*sigh* No, you don't need sudo to run the script. It seems either the script didn't correctly catch the subtitle-language from mkvinfo, or it's not specified at all. I added a small tweak to the script now:

#!/bin/bash

for x in *.mkv
do

numTracks=$(mkvinfo "$x"|grep -c "S_TEXT/ASS")
if [ $numTracks -ne 1 ]
then
	echo "No tracks to convert or more than one! Aborting."
	exit 1
fi

for subTrack in $(mkvinfo "$x"|grep -B4 "S_TEXT/ASS"|grep "Track UID"|cut -d ":" -f 2)
do
	trackLang=$(mkvinfo "$x"|grep -B4 "S_TEXT/ASS"|grep -C3 "Track UID: $subTrack"|grep "Language:"|cut -d ":" -f 2|tr -d '\040\011\012\015')
	if [ -z "$trackLang" ]
	then
		trackLang="eng"
	fi
	let subTrack--
	ffmpeg -i "$x" -map 0:$subTrack out.srt
	if [ $? -ne 0 ]
	then
		echo "Failed to convert subtitle to SRT!"
		exit 1
	fi
	mkvmerge -o output.mkv "$x" --language 0:$trackLang --default-track "0:yes" out.srt
	if [ $? -ne 0 ]
	then
		echo "Failed to merge subtitle to $x!"
		exit 1
	fi
	rm out.srt
	mkdir -p Converted
	echo "Successfully converted $x to output.mkv with added SRT-subs!"
	mv output.mkv "Converted/$x"
	break
done

done

 

On 10/14/2019 at 3:20 PM, elfensky said:

Sorry to bother you again, but any chance you could add to the script so it would remove the original .ass subs?
Or change it to the second subtitle track?

I tried looking at the documentation for mkvtoolnix, but it honestly confuses the crap outta me.

 

to select the first subtitle track I'd have to do --edit track:s1, and deleting seems to be --delete <name>, so would the line be --delete track:s1?
 

--set track:s1=track:s3
--set track:s1=track:s1
--set track:s3=track:s2? 
That seems super convoluted (if that's even the right way to do it)  

 



And if yes where would I have to put it in the scipt?

Here you go, note - this modified script will drop all existing subtitles and will preserve ONLY the first found "ASS" type subtitle track as an SRT subtitle track.

 

convert.sh

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 months later...

I know this is old but if you're bored and feel like tweaking your script further:

Convert all .ASS to .SRT, whether there are more than one or not. That implies not caring how many subs are embedded in the file and what extension. Simply, for each .ASS create its .SRT and delete de .ASS. Leave the rest as it is.

 

Cheers.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×