从 mkv 文件中提取字幕

从 mkv 文件中提取字幕

问题是 Ubuntu 中的视频播放器在集成中欧字幕时出现问题。解决方案是提取它们。有人知道终端中是否有命令或程序可以从 mkv 文件中提取字幕吗?

答案1

mkvtoolnix使用进行安装sudo apt-get install mkvtoolnix

从终端运行:mkvextract tracks <your_mkv_video> <track_numer>:<subtitle_file.srt>

用于mkvinfo获取有关曲目的信息。

使用此实用程序,您可以提取任何曲目,甚至音频或视频。

答案2

Cornelius 的回答最终对我有用,但有些事情并不明显。

安装mkvtoolnix

sudo apt-get install mkvtoolnix

检测轨道号

mkvmerge -i some_movie.mkv

示例输出:

File 'some_movie.mkv': container: Matroska
Track ID 0: video (MPEG-4p10/AVC/h.264)
Track ID 1: audio (AC-3/E-AC-3)
Track ID 2: subtitles (SubRip/SRT)
Chapters: 12 entries
Global tags: 2 entries

提取轨迹

句法:

mkvextract tracks <your_mkv_video> <track_number>:<subtitle_file.srt>

请注意,您不能有任何空格之间。<track_number>:<subtitle_file.srt>

例子:

mkvextract tracks "some_movie.mkv" 2:some_movie_subs.srt

答案3

您可以使用 mkvtoolnix 。

sudo apt-get install mkvtoolnix

另一个提示是,因为 mkv 文件可能包含许多字幕,所以提示是这个脚本,您可以搜索您想要的语言,例如如果您想要英语,它只会下载英语。

脚本 :

#!/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

# Get all the MKV files in this dir and its subdirs
find "$DIR" -type f -name '*.mkv' | while read filename
do
  # 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:"$subtitlename.srt.tmp" > /dev/null 2>&1`
    `chmod g+rw "$subtitlename.srt.tmp"`


    # Do a super-primitive language guess: ENGLISH
    langtest=`egrep -ic ' you | to | the ' "$subtitlename".srt.tmp`
    trimregex=""



    # 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.srt"`
      `rm "$subtitlename.srt.tmp"`
      `chmod g+rw "$subtitlename.srt"`
    else
      # Not our desired language: add a number to the filename and keep anyway, just in case
      `mv "$subtitlename.srt.tmp" "$subtitlename.$tracknumber.srt" > /dev/null 2>&1`
    fi
  done
done

保存此脚本 nameyouwant.sh 并使其可执行

现在在终端中将目录更改为脚本文件夹并写入 ./nameyouwant.sh /pathtosave

相关内容