fant0men

Extract subtitles from MKV (script)

Jan 20th, 2020
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # This script is meant to extract all the subtitles from a Matroska (.mkv) file.
  3. #
  4. # The output filename is the same as the input filename, only a random number is added to the name.
  5.  
  6. usage () {
  7.     msg[0]="You need mkvtoolnix installed to run this script.\n"
  8.     msg[1]="Usage: $(basename "$0") [MKV]\n"
  9.     msg[2]="There are no subtitles in:\n${if}\n"
  10.     echo -e "${msg[${1}]}"
  11.     exit
  12. }
  13.  
  14. cmd=$(command -v mkvmerge 2>&-)
  15. if=$(readlink -f "$1" 2>&-)
  16. of_tmp="${if%.mkv}"
  17. of="${of_tmp}-${RANDOM}.mkv"
  18.  
  19. if [[ -z $cmd ]]; then
  20.     usage 0
  21. fi
  22.  
  23. if [[ ! -f $if ]]; then
  24.     usage 1
  25. fi
  26.  
  27. mapfile -t if_subs < <(mkvinfo "${if}" 2>&- | grep 'Track type: subtitles')
  28.  
  29. if [[ -z ${if_subs[0]} ]]; then
  30.     usage 2
  31. fi
  32.  
  33. mkvmerge --title "" -o "${of}" --no-video --no-audio --no-chapters "${if}"
RAW Paste Data