Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.48 KB | None | 0 0
  1. #!/bin/bash
  2. # Extract subtitles from each MKV file in the given directory
  3.  
  4. # If no directory is given, work in local dir
  5. if [ "$1" = "" ]; then
  6.   DIR="./"
  7. else
  8.   DIR="$1"
  9. fi
  10.  
  11. SUBDIR="$DIR/subs"
  12.  
  13. # Get all the MKV files in this dir and its subdirs
  14. find "$DIR" -maxdepth 1 -type f -name '*.mkv' | while read filename
  15. do
  16. subtitlename=${filename%.*}
  17. plainsubname=${subtitlename/$DIR/}
  18. tmpsubtitle="/tmp/$plainsubname.srt.tmp"
  19. `find ./subs -maxdepth 1 -type f -name "$plainsubname-*.srt" 2>/dev/null | grep -q .srt`
  20. RETVAL=$?
  21.  
  22. if [ $RETVAL -ne 0 ];
  23. then
  24. echo "no subtitle yet for $filename, scanning"
  25. # Find out which tracks contain the subtitles
  26.   mkvmerge -i "$filename" | grep 'subtitles' | while read subline
  27.   do
  28.     # Grep the number of the subtitle track
  29.     tracknumber=`echo $subline | egrep -o "[0-9]{1,2}" | head -1`
  30.  
  31.     # Get base name for subtitle
  32.     subtitlename=${filename%.*}
  33.  
  34.     # Extract the track to a .tmp file
  35.     `mkvextract tracks "$filename" $tracknumber:"$tmpsubtitle" > /dev/null 2>&1`
  36.     `chmod g+rw "$tmpsubtitle"`
  37.  
  38.     # Do a super-primitive language guess: ENGLISH
  39.     langtest=`egrep -ic ' you | the | just | what ' "$tmpsubtitle"`
  40.     trimregex=""
  41.  
  42.     # Check if subtitle passes our language filter (10 or more matches)
  43.     if [ $langtest -ge 10 ]; then
  44.       if [ -f  "$tmpsubtitle" ]; then
  45.         # Check the file size for know if it s a forced or a real subtitle
  46.             size_log=$(du "$tmpsubtitle" | cut -f1)
  47.             #echo $size_log
  48.             if [ $size_log -lt 10 ]; then
  49.               # rename forced subtitle for plex
  50.               echo "subtitle forced, $subtitlename"
  51.               `mv "$tmpsubtitle "$SUBDIR/$subtitlename.forced-$tracknumber.srt" > /dev/null 2>&1`
  52.            else
  53.              # Rename for plex
  54.              echo "found sub #track $tracknumber in $subtitlename"
  55.              `mv "$tmpsubtitle" "$SUBDIR/$subtitlename.internal-$tracknumber-en.srt" > /dev/null 2>&1`
  56.            fi
  57.      fi
  58.    else
  59.      if [ -f  "$tmpsubtitle" ]; then
  60.            size_log=$(du "$tmpsubtitle" | cut -f1)
  61.            #echo $size_log
  62.            if [ $size_log -lt 10 ]; then
  63.              echo "subtitle forced, $subtitlename"
  64.              `mv "$tmpsubtitle" "$SUBDIR/$subtitlename.forced-$tracknumber.srt" > /dev/null 2>&1`
  65.            else
  66.              # Subtitle in another language --> delete
  67.              `rm "$tmpsubtitle"  > /dev/null 2>&1`
  68.            fi
  69.          fi
  70.        fi
  71.  done
  72. fi
  73. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement