我的意图是做一个字幕,我需要一个视频。我遇到一个网站,有人提到我如何成为一个干净的文本从vtt文件。
方法如下:
youtube-dl --write-auto-sub --convert-subs=srt --skip-download URL
例如,您正在下载https://www.youtube.com/watch?v=example。标题"example"——convert=srt将输出到一个名为example.en.srt的文件,其中en代表英语,es代表西班牙语等。
文件将是这样的:
00:00:04.259——比;00:00:05.259在比;我是埃隆·马斯克。
00:00:05.259——比;00:00:06.669在比;你凭什么成名的?
00:00:06.669——比;00:00:07.669在比;我是
的创始人00:00:07.669——比;00:00:08.669Tesla.com。
可选-如果你需要清理文本,你可以使用python来清理它:
import re
bad_words = ['-->','</c>']
with open('example.en.vtt') as oldfile, open('newfile.txt', 'w') as newfile:
for line in oldfile:
if not any(bad_word in line for bad_word in bad_words):
newfile.write(line)
with open('newfile.txt') as result:
uniqlines = set(result.readlines())
with open('sub_out.txt', 'w') as rmdup:
mylst = map(lambda each: each.strip(">>"), uniqlines)
print(mylst)
rmdup.writelines(set(mylst))
输出newfile.txt:
我是Elon Musk。你凭什么成名的?我是…的创始人Tesla.com。
好的,然后我下载了Python,在CMD中输入py
,它运行了。
文件在C驱动器下的文档,我改变了文件名与上面一样,我希望这是正确的文件名example.en.vtt
然后输入cmd
f = open("C:\dokumenteexample.en.vtt" , "r" print(f.read())
SyntaxError:语法无效。也许你忘了逗号?
我做错了什么?我希望有人能帮忙。谢谢你
f = open("C:\dokumente\example.en.vtt" , "r") # Properly escaped and closing paren
或
# f = open("C:/dokumente/exapmle.en.vtt", "r") # / rather than
然后
print(f.read()) # On a different line, or separated with ;