Skip to content

Instantly share code, notes, and snippets.

@doublerebel
Last active December 11, 2015 09:29
Show Gist options
  • Save doublerebel/4580501 to your computer and use it in GitHub Desktop.
Save doublerebel/4580501 to your computer and use it in GitHub Desktop.
Extract subtitles from video and format them to SRT spec
#/bin/sh
#
# Extract subtitles from video and format them to SRT spec
# Useful for Samsung Smart TVs
#
# Depends on mkvtoolnix package (available for most distros) for mkvmerge and mkvextract
# To list tracks in the video, use command: ./srtextract.sh tracks <filename>
#
# Depends on ass2srt.pl for susbtitle format conversion
# http://code.google.com/p/ass2srt/downloads/list
#
if [ -z "$1" ]; then echo "Error: Video filename required as first argument."; exit 0;
elif [ "$1" = "tracks" ]; then
if [ -z "$2" ]; then echo "Error: Video filename required as second argument."; exit 0;
else mkvmerge -i $2;
fi
else
if [ -z "$2" ]; then
echo "No subtitle track number specified, extracting first one found..."
subtrack=$(mkvmerge -i $1 | grep subtitles | awk -F" " '{print $3}')
else
subtrack="$2:"
fi
subfile=${1%.*}.english.ass
mkvextract tracks $1 $subtrack$subfile
perl ass2srt.pl $subfile
rm $subfile
perl -pi -0 -w -e "s/\n\n\n+/\n\n/g" ${subfile%.*}.srt
fi
# validate with https://github.com/smelc/srtcheck
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment