Sub

I. Linux

1. mkvtoolnix - Xuất Sub từ file mkv hàng loạt:

Vị trí file srt được xuất ra sẽ nằm cùng với thư mục chứa file mkv.

Lệnh cài đặt mkvtoolnix trên Debian/Ubuntu:

sudo apt-get install mkvtoolnix

Tạo tệp lệnh nameyouwant.sh (ex: extract_subtitles.sh) với kịch bản như bên dưới:

(Đổi đường dẫn thư mục thành thư mục chứa file mkv của bạn: root_dir="/mnt/sda1/00. Media/Movies")

#!/bin/bash

# Extract English subtitles from each MKV file in the given directory and its subdirectories


# Directory containing MKV files and its subdirectories

root_dir="/mnt/sda1/00. Media/Movies"


# Find all MKV files in the root directory and its subdirs

find "$root_dir" -type f -name '*.mkv' | while read -r filename

do

  echo "Processing $filename..."


  # Check if any .srt file already exists in the same directory

  if [[ -n $(find "$(dirname "$filename")" -maxdepth 1 -type f -name "*.srt") ]]; then

    echo "Skipping $filename - .srt file already exists"

    continue

  fi


  # Find out which tracks contain subtitles

  mkvmerge -i "$filename" | grep 'subtitles (SubRip/SRT)' | while read -r subline

  do

    # Extract track number of subtitles

    tracknumber=$(echo "$subline" | egrep -o "[0-9]{1,2}" | head -1)


    # Generate base name for subtitle file

    subtitlename="${filename%.*}"


    # Extract the subtitle track to a .tmp file

    mkvextract tracks "$filename" $tracknumber:"$subtitlename.srt.tmp"

    chmod g+rw "$subtitlename.srt.tmp"


    # Perform language detection (very basic)

    langtest=$(egrep -ic ' you | to | the ' "$subtitlename".srt.tmp)


    # Check if subtitle passes our language filter (10 or more matches)

    if [ $langtest -ge 10 ]; then


      # Regex to remove credits at the end of subtitles (read my reason why!)

      #`sed 's/\r//g' < "$subtitlename.srt.tmp" \

       # | sed 's/%/%%/g' \

       # | awk '{if (a){printf("\t")};printf $0; a=1; } /^$/{print ""; a=0;}' \

       # | grep -iv "$trimregex" \

       # | sed 's/\t/\r\n/g' > "$subtitlename.eng.srt"`

      #`rm "$subtitlename.srt.tmp"`

      #`chmod g+rw "$subtitlename.eng.srt"`



      #If subtitle is English, rename the .tmp file to .srt

      mv "$subtitlename.srt.tmp" "$subtitlename.eng.srt" > /dev/null 2>&1

      chmod g+rw "$subtitlename.eng.srt"


      echo "Successfully extracted English subtitles to $subtitlename.eng.srt"

    else

      # If not English, keep the original subtitle track with a modified filename

      mv "$subtitlename.srt.tmp" "$subtitlename.$tracknumber.srt" > /dev/null 2>&1

      echo "Non-English subtitles detected and kept as $subtitlename.$tracknumber.srt"

    fi

  done

done


echo "Subtitle extraction completed!"


Bây giờ trong Terminal hãy thay đổi thư mục thành thư mục tập lệnh và viết ./nameyouwant.sh
(ex: ./extract_subtitles.sh)