![如何使用 shell 循环从 mkv 中删除任意字幕格式](https://linux22.com/image/123895/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20shell%20%E5%BE%AA%E7%8E%AF%E4%BB%8E%20mkv%20%E4%B8%AD%E5%88%A0%E9%99%A4%E4%BB%BB%E6%84%8F%E5%AD%97%E5%B9%95%E6%A0%BC%E5%BC%8F.png)
我制作此脚本是为了从给定路径中的所有 mkv 文件中删除所有子文件。工作正常。但现在我只想删除 PGS 字幕并保留所有 SRT 文件,无论语言如何。
for file in "$@"*mkv; do
mkvmerge -o "${file%.mkv}".nosubs.mkv --no-subtitles "$file"
done
Bonus : 如何保留任意语言的所有 SRT 文件。
答案1
解析 的输出以获取所需的轨道 ID,并使用( ) 选项mkvmerge -I
将结果作为逗号分隔列表传递。--subtitle-tracks
-s
要仅选择 SRT 字幕,请使用sed
:
for file in "$@"*.mkv; do
subs=$(mkvmerge -I "$file" | sed -ne '/^Track ID [0-9]*: subtitles (SubRip\/SRT).*/ { s/^[^0-9]*\([0-9]*\):.*/\1/;H }; $ { g;s/[^0-9]/,/g;s/^,//;p }')
if [[ -n $subs ]]; then subs="-s $subs"; else subs=-S; fi
mkvmerge $subs -o "${file%.mkv}".srt-only.mkv "$file"
done
同样,要仅选择英语 SRT:
for file in "$@"*.mkv; do
subs=$(mkvmerge -I "$file" | sed -ne '/^Track ID [0-9]*: subtitles (SubRip\/SRT).* language:eng.*/ { s/^[^0-9]*\([0-9]*\):.*/\1/;H }; $ { g;s/[^0-9]/,/g;s/^,//;p }')
if [[ -n $subs ]]; then subs="-s $subs"; else subs=-S; fi
mkvmerge $subs -o "${file%.mkv}".eng-srt-only.mkv "$file"
done
更改language:eng
为选择另一种语言。
答案2
-I
中的选项已mkvmerge
被删除并替换为-J
输出 JSON。这是卢马托的脚本适合与一起使用(需要jq
安装,请参阅这个链接有关操作系统的安装说明)
#!/bin/bash
# If no directory is given, work in local dir
if [ -d "$1" ] || [ -f "$1" ]; then
DIR="$1"
else
echo "No target path given, using working directory '$(pwd)'"
DIR="."
fi
# Get all the MKV files in this dir and its subdirs
find "$DIR" -type f -name '*.mkv' | while read filename
do
subs=$(mkvmerge -J "$filename" | jq -r '.tracks[] | select(.type=="subtitles") | select(.properties.language=="eng").id' | sed 'N;s/\n/,/')
if [[ -n $subs ]]
then
subs="-s $subs"
else
subs=-S
fi
echo -e "Extracting tracks \e[1m\e[34m${subs#* }\e[0m from \e[1m\e[34m${filename}\e[0m..."
mkvmerge $subs -o "${filename%.mkv}".eng-srt-only.mkv "$filename" > /dev/null 2>&1
done