当前位置: 首页>>技术教程>>正文


从mkv文件中提取字幕

ubuntuer 技术教程 , , 去评论

问题描述

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

最佳解决方法

使用sudo apt-get install mkvtoolnix安装mkvtoolnix

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

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

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

次佳解决方法

你可以使用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

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/2815.html,未经允许,请勿转载。

相关文章