fant0men

Show all subtitle languages of an MKV file (bash script)

Nov 18th, 2019
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # This script echoes all the subtitle languages of an MKV file
  3. # in a comma-separated list. The MKV file should be given as the 1st
  4. # argument to this script.
  5.  
  6. declare -a mkvinfo_list
  7. declare -a lang_list
  8. declare -a lang_list_sorted
  9.  
  10. if="$1"
  11.  
  12. if [[ ! -f $if ]]; then
  13.     exit
  14. fi
  15.  
  16. command -v mkvinfo 1>&- || exit
  17.  
  18. #tmp_f="/dev/shm/mkvinfo-${RANDOM}.txt"
  19. IFS_bak="$IFS"
  20. switch=0
  21. count=0
  22.  
  23. IFS=$'\n'
  24.  
  25. mkv_info_list=( $(mkvinfo "$if") )
  26.  
  27. IFS="$IFS_bak"
  28.  
  29. for (( i = 0; i < ${#mkv_info_list[@]}; i++ )); do
  30.  
  31.     line="${mkv_info_list[${i}]}"
  32.  
  33.     if [[ $line =~ 'Track type: subtitles' ]]; then
  34.         switch=1
  35.     fi
  36.  
  37.     if [[ $switch -eq 1 ]]; then
  38.         if [[ $line =~ 'Language:' ]]; then
  39.  
  40.             lang_list+=( $(sed 's/^.*Language: //' <<<"$line") )
  41.             switch=0
  42.         fi
  43.  
  44.         if [[ $line =~ 'Name:' ]]; then
  45.  
  46.             lang_list+=( $(sed 's/^.*Name: //' <<<"$line") )
  47.             switch=0
  48.         fi
  49.     fi
  50. done
  51.  
  52. for (( i = 0; i < ${#lang_list[@]}; i++ )); do
  53.     lang_list_v+="${lang_list[${i}]}\n"
  54. done
  55.  
  56. IFS=$'\n'
  57.  
  58. lang_list_sorted=( $(echo -e "$lang_list_v" | sort -u) )
  59.  
  60. IFS="$IFS_bak"
  61.  
  62. for (( i = 0; i < ${#lang_list_sorted[@]}; i++ )); do
  63.  
  64.     line="${lang_list_sorted[${i}]}"
  65.  
  66.     if [[ $count -eq 0 ]]; then
  67.         printf "Subtitles: %s" "${line^}"
  68.     else
  69.         printf ", %s" "${line^}"
  70.     fi
  71.  
  72.     let count++
  73. done
  74.  
  75. echo
RAW Paste Data