将Youtube的WebVTT文件转化为纯文本

7

我正在使用youtube-dl从YouTube下载WebVTT文件。

一个典型的文件长这样:

WEBVTT
Kind: captions
Language: en

00:00:00.730 --> 00:00:05.200 align:start position:0%

[Applause]

00:00:05.200 --> 00:00:05.210 align:start position:0%
[Applause]


00:00:05.210 --> 00:00:11.860 align:start position:0%
[Applause]
hi<00:00:06.440><c> I'm</c><00:00:07.440><c> here</c><00:00:07.740><c> to</c><00:00:08.160><c> talk</c><00:00:08.429><c> to</c><00:00:09.019><c> share</c><00:00:10.019><c> an</c><00:00:10.469><c> idea</c><00:00:10.820><c> to</c>

00:00:11.860 --> 00:00:11.870 align:start position:0%
hi I'm here to talk to share an idea to


00:00:11.870 --> 00:00:15.890 align:start position:0%
hi I'm here to talk to share an idea to
communicate<00:00:12.920><c> but</c><00:00:13.920><c> what</c><00:00:14.790><c> is</c><00:00:14.940><c> communication</c>

00:00:15.890 --> 00:00:15.900 align:start position:0%
communicate but what is communication

我想要一个包含以下内容的文本文件:

hi I'm here to talk to share an idea to
communicate but what is communication

我找到了网上的代码,得到了这个:

cat output.vtt | sed "s/^[0-9]*[0-9\:\.\ \>\-]*//g" | grep -v "^WEBVTT\|^Kind: cap\|^Language" | awk 'BEGIN{ RS="\n\n+"; RS="\n\n" }NR>=2{ print }' > dialogues.txt

但它远非完美。我得到了许多无用的空格,而且所有的句子都显示了两次。你介意帮帮我吗?之前有人问过类似的问题,但提交的答案对我没有用。

谢谢!

4个回答

4

请尝试在单个awk中执行以下操作。

awk 'FNR<=4 || ($0 ~ /^$|-->|\[|\]|</){next} !a[$0]++'  Input_file

解释:现在为上述代码添加解释。

awk '                                     ##Starting awk program here.
FNR<=4 || ($0 ~ /^$|-->|\[|\]|</){        ##Checking condition if line number is less than 4 OR having spaces or [ or ] or --> then go next line.
  next                                    ##next will skip all further statements from here.
  }
!a[$0]++                                  ##Creating an array whose index is $0 and increment its value with 1 with condition that it should NOT be already present in array a, which means it will give only 1 value of each line.
'  Input_file                             ##Mentioning Input_file name here.

1
好的一站式解决方案;如果有一些解释就更好了 +1 - l'L'l
1
当然,感谢您的鼓励,我现在已经添加了解释。 - RavinderSingh13
这个几乎可以工作,但它会删除所有换行符(可能是有意为之,我不确定,但不是我想要的),最重要的是,它会删除所有重复的行,在音乐字幕等场景中,这是一个严重的问题,因为只有合唱的第一次出现会被保留。 - Hashim Aziz

1
你可能能够做类似这样的事情:
sed -e '1,4d' -E -e '/^$|]|>$|%$/d' output.vtt | awk '!seen[$0]++' > dialogues.txt
  • sed 删除前4行。
  • sed 然后删除任何空行,或包含 ] 的行,或以 >% 结尾的行。
  • awk 删除重复的行。
hi I'm here to talk to share an idea to
communicate but what is communication 

你可能需要稍微调整一下,但它应该会更符合你想要的方向。

这将删除第四个换行符之后的所有换行符(这可能是有意的,但不是我想要的),但更重要的是,它似乎没有回答问题,因为它保留了所有时间戳。 - Hashim Aziz
小心使用正则表达式 - 有时这些该死的歌词文件可能会嵌入类似HTML的标签,而没有重复的行可以丢弃(而我还得处理Unicode)。 - RARE Kpop Manifesto

0
在我的情况下,我希望做到以下几点:
  • 删除前四行
  • 删除所有的时间戳行
  • 保留字幕之间的空行

我用以下单一的sed 命令实现了这一点:

sed -En '1,4d;/^[0-9].:[0-9].:[0-9].+$/!p' input.vtt > output.txt

如果像我一样,您经常需要这样做,并且正在使用Bash,您还可以将其转换为Bash函数:
function vtt_to_txt() {
    sed -En '1,4d;/^[0-9].:[0-9].:[0-9].+$/!p' "$1" > "$2"
}

这将允许您随时像这样简单地调用函数:
vtt_to_text input.vtt output.txt

0
如果你分析一下你的 .vtt 文件的模式,基本上你想保留从第10行开始的每8行。因此,算法是删除前两行,然后保留每8行:
$ cat output.vtt | sed '1,2 d' | awk 'NR%8==0'

[Applause]
hi I'm here to talk to share an idea to
communicate but what is communication
  • sed '1,2 d' 删除从第一行到第二行的范围
  • awk 'NR%8==0' 打印每8行

如果你想进一步过滤掉 "[...]" 行,那么你可以添加另一个 grep 命令,例如 grep -v '^\[.*\]$'


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接