Advertisement
Guest User

Untitled

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