Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Extract subtitles from each MKV file in the given directory
- # If no directory is given, work in local dir
- if [ "$1" = "" ]; then
- DIR="./"
- else
- DIR="$1"
- fi
- SUBDIR="$DIR/subs"
- # Get all the MKV files in this dir and its subdirs
- find "$DIR" -maxdepth 1 -type f -name '*.mkv' | while read filename
- do
- subtitlename=${filename%.*}
- plainsubname=${subtitlename/$DIR/}
- tmpsubtitle="/tmp/$plainsubname.srt.tmp"
- `find ./subs -maxdepth 1 -type f -name "$plainsubname-*.srt" 2>/dev/null | grep -q .srt`
- RETVAL=$?
- if [ $RETVAL -ne 0 ];
- then
- echo "no subtitle yet for $filename, scanning"
- # Find out which tracks contain the subtitles
- mkvmerge -i "$filename" | grep 'subtitles' | while read subline
- do
- # Grep the number of the subtitle track
- tracknumber=`echo $subline | egrep -o "[0-9]{1,2}" | head -1`
- # Get base name for subtitle
- subtitlename=${filename%.*}
- # Extract the track to a .tmp file
- `mkvextract tracks "$filename" $tracknumber:"$tmpsubtitle" > /dev/null 2>&1`
- `chmod g+rw "$tmpsubtitle"`
- # Do a super-primitive language guess: ENGLISH
- langtest=`egrep -ic ' you | the | just | what ' "$tmpsubtitle"`
- trimregex=""
- # Check if subtitle passes our language filter (10 or more matches)
- if [ $langtest -ge 10 ]; then
- if [ -f "$tmpsubtitle" ]; then
- # Check the file size for know if it s a forced or a real subtitle
- size_log=$(du "$tmpsubtitle" | cut -f1)
- #echo $size_log
- if [ $size_log -lt 10 ]; then
- # rename forced subtitle for plex
- echo "subtitle forced, $subtitlename"
- `mv "$tmpsubtitle "$SUBDIR/$subtitlename.forced-$tracknumber.srt" > /dev/null 2>&1`
- else
- # Rename for plex
- echo "found sub #track $tracknumber in $subtitlename"
- `mv "$tmpsubtitle" "$SUBDIR/$subtitlename.internal-$tracknumber-en.srt" > /dev/null 2>&1`
- fi
- fi
- else
- if [ -f "$tmpsubtitle" ]; then
- size_log=$(du "$tmpsubtitle" | cut -f1)
- #echo $size_log
- if [ $size_log -lt 10 ]; then
- echo "subtitle forced, $subtitlename"
- `mv "$tmpsubtitle" "$SUBDIR/$subtitlename.forced-$tracknumber.srt" > /dev/null 2>&1`
- else
- # Subtitle in another language --> delete
- `rm "$tmpsubtitle" > /dev/null 2>&1`
- fi
- fi
- fi
- done
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement