Extract subtitle from mkv files
The problem is that videoplayers in Ubuntu have a problem with integrated central european subtitles.The solution is to extract them. Does anyone knows if there is a command in the terminal or a program to extract the subtitle from a mkv file?
Install mkvtoolnix
with sudo apt-get install mkvtoolnix
.
Run from terminal: mkvextract tracks <your_mkv_video> <track_numer>:<subtitle_file.srt>
Use mkvinfo
to get information about tracks.
Using this utility you can extract any track, even audio or video.
you can use mkvtoolnix .
sudo apt-get install mkvtoolnix
Another tip now because mkv files may contain many subtitles , so the tip is this script that you can search for the language you want , so for example if you want English it will download just English .
Script :
#!/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
done
done
Save this script nameyouwant.sh and make it executable
Now in terminal change directory to script folder and write
./nameyouwant.sh /pathtosave
Answer of Cornelius finally worked for me, but some things were not obvious.
Install mkvtoolnix
sudo apt-get install mkvtoolnix
Detect track numbers
mkvmerge -i some_movie.mkv
Sample output:
File 'some_movie.mkv': container: Matroska
Track ID 0: video (MPEG-4p10/AVC/h.264)
Track ID 1: audio (AC-3/E-AC-3)
Track ID 2: subtitles (SubRip/SRT)
Chapters: 12 entries
Global tags: 2 entries
Extract tracks
Syntax:
mkvextract tracks <your_mkv_video> <track_number>:<subtitle_file.srt>
Note, that you can't have any spaces between <track_number>:
and <subtitle_file.srt>
.
Example:
mkvextract tracks "some_movie.mkv" 2:some_movie_subs.srt