是否可以从 YouTube 视频中提取隐藏式字幕记录?
我们在 YouTube 上有 200 多个网络广播,每个至少一小时。YouTube 为所有视频提供了隐藏式字幕,但用户似乎无法获得它。
我尝试了此博客中的 URL,但它不适用于我们的视频。
http://googlesystem.blogspot.com/2010/10/download-youtube-captions.html
是否可以从 YouTube 视频中提取隐藏式字幕记录?
我们在 YouTube 上有 200 多个网络广播,每个至少一小时。YouTube 为所有视频提供了隐藏式字幕,但用户似乎无法获得它。
我尝试了此博客中的 URL,但它不适用于我们的视频。
http://googlesystem.blogspot.com/2010/10/download-youtube-captions.html
以下是获取 YouTube 视频脚本(如果可用)的方法:
虽然语法可能有点傻,但这是一个很好的解决方案。
来源:http ://ccm.net/faq/40644-youtube-how-to-get-the-transcript-of-a-video
另一种选择是使用youtube-dl
:
youtube-dl --skip-download --write-auto-sub $youtube_url
默认格式是vtt
,其他可用格式是ttml
( --sub-format ttml
)。
--write-sub
Write subtitle file
--write-auto-sub
Write automatically generated subtitle file (YouTube only)
--all-subs
Download all the available subtitles of the video
--list-subs
List all available subtitles for the video
--sub-format FORMAT
Subtitle format, accepts formats preference, for example: "srt" or "ass/srt/best"
--sub-lang LANGS
Languages of the subtitles to download (optional) separated by commas, use --list-subs for available language tags
您可以使用ffmpeg
将字幕文件转换为另一种格式:
ffmpeg -i input.vtt output.srt
在 VTT 字幕中,每个字幕文本重复 3 次,每八行有一个新的字幕文本:
WEBVTT
Kind: captions
Language: en
00:00:01.429 --> 00:00:04.249 align:start position:0%
ladies<00:00:02.429><c> and</c><00:00:02.580><c> gentlemen</c><c.colorE5E5E5><00:00:02.879><c> I'd</c></c><c.colorCCCCCC><00:00:03.870><c> like</c></c><c.colorE5E5E5><00:00:04.020><c> to</c><00:00:04.110><c> thank</c></c>
00:00:04.249 --> 00:00:04.259 align:start position:0%
ladies and gentlemen<c.colorE5E5E5> I'd</c><c.colorCCCCCC> like</c><c.colorE5E5E5> to thank
</c>
00:00:04.259 --> 00:00:05.930 align:start position:0%
ladies and gentlemen<c.colorE5E5E5> I'd</c><c.colorCCCCCC> like</c><c.colorE5E5E5> to thank
you<00:00:04.440><c> for</c><00:00:04.620><c> coming</c><00:00:05.069><c> tonight</c><00:00:05.190><c> especially</c></c><c.colorCCCCCC><00:00:05.609><c> at</c></c>
00:00:05.930 --> 00:00:05.940 align:start position:0%
you<c.colorE5E5E5> for coming tonight especially</c><c.colorCCCCCC> at
</c>
00:00:05.940 --> 00:00:07.730 align:start position:0%
you<c.colorE5E5E5> for coming tonight especially</c><c.colorCCCCCC> at
such<00:00:06.180><c> short</c><00:00:06.690><c> notice</c></c>
00:00:07.730 --> 00:00:07.740 align:start position:0%
such short notice
00:00:07.740 --> 00:00:09.620 align:start position:0%
such short notice
I'm<00:00:08.370><c> sure</c><c.colorE5E5E5><00:00:08.580><c> mr.</c><00:00:08.820><c> Irving</c><00:00:09.000><c> will</c><00:00:09.120><c> fill</c><00:00:09.300><c> you</c><00:00:09.389><c> in</c><00:00:09.420><c> on</c></c>
00:00:09.620 --> 00:00:09.630 align:start position:0%
I'm sure<c.colorE5E5E5> mr. Irving will fill you in on
</c>
00:00:09.630 --> 00:00:11.030 align:start position:0%
I'm sure<c.colorE5E5E5> mr. Irving will fill you in on
the<00:00:09.750><c> circumstances</c><00:00:10.440><c> that's</c><00:00:10.620><c> brought</c><00:00:10.920><c> us</c></c>
00:00:11.030 --> 00:00:11.040 align:start position:0%
<c.colorE5E5E5>the circumstances that's brought us
</c>
这会将 VTT 字幕转换为更简单的格式:
sed '1,/^$/d' *.vtt| # remove the lines at the top of the file
sed 's/<[^>]*>//g'| # remove tags
awk -F. 'NR%4==1{printf"%s ",$1}NR%4==3' | # print each new subtitle text and its start time without milliseconds
awk NF\>1 # remove lines with only one field
输出:
00:00:01 ladies and gentlemen I'd like to thank
00:00:04 you for coming tonight especially at
00:00:05 such short notice
00:00:07 I'm sure mr. Irving will fill you in on
00:00:09 the circumstances that's brought us
在我测试过的大约 10% 的视频中(例如p9M3shEU-QM
和aE05_REXnBc
),有时会有一个字幕文本在前一个文本之后 12 行而不是 8 行出现。我的解决方法是每四行打印一次,然后删除只有一个字段的空行。
这是一个函数,它接受一个或多个视频、播放列表或频道的 ID 或 URL 作为参数:
cap()(printf %s\\n "${@-$(cat)}"|parallel -j10 -q youtube-dl -i --skip-download --write-auto-sub -o '%(upload_date)s.%(title)s.%(uploader)s.%(id)s.%(ext)s' --;for f in *.vtt;do sed '1,/^$/d' -- "$f"|sed 's/<[^>]*>//g'|awk -F. 'NR%4==1{printf"%s ",$1}NR%4==3'|awk NF\>1>"${f%.vtt}";rm -- "$f";done)
如果您使用上述功能下载频道或播放列表中的所有视频,有时需要-i
( ) 选项,因为它会导致在下载单个视频出错时不会出错退出。--ignore-errors
youtube-dl
您可以通过访问查看/复制/下载 youtube 的隐藏式字幕文件的时间编码 xml 文件
http://video.google.com/timedtext?lang=[LANGUAGE]&v=[YOUTUBE VIDEO IDENTIFIER]
例如http://video.google.com/timedtext?lang=pt&v=WSVKbw7LC2w
注意:此方法不会下载自动生成的隐藏式字幕,即使您使用正确的语言(可能有自动生成语言的特殊代码)。
以下文件说只有频道的所有者可以通过标准 youtube 界面执行此操作: https ://developers.google.com/youtube/2.0/developers_guide_protocol_captions?hl=en
廉价修复:您可以单击“交互式转录”按钮 - 并以这种方式复制内容。当然,您会以这种方式失去毫秒数。
非常便宜的修复:一个共享的 youtube 帐户 - 以便多人可以编辑和上传字幕文件。
具有挑战性的解决方案:youtube API 允许通过 HTTP 下载和上传字幕文件......您可以编写一个 youtube API 应用程序来提供浏览器用户界面,以便为任何用户或特定用户上传或下载。
这是 java http://apiblog.youtube.com/2011/01/youtube-captions-uploader-web-app.html中的示例项目
这是一个非常简单的上传工作示例:http: //yt-captions-uploader.appspot.com/
(强制性“这可能是一个内部 youtube.com 界面,可能随时中断”)
这里没有链接到执行此操作的另一个工具,而是对“如何执行此操作”问题的答案
使用fiddler或您的浏览器开发工具(例如
Chrome)来检查 youtube.com HTTP 流量,并且有一个响应/api/timedtext
包含作为 XML 的隐藏字幕信息。
似乎是这样的回应:
<p t="0" d="5430" w="1">
<s p="2" ac="136">we've</s>
<s t="780" ac="252"> got</s>
</p>
<p t="2280" d="7170" w="1">
<s ac="243">we're</s>
<s t="810" ac="233"> going</s>
</p>
表示 at time0
是 wordwe've
并且 at time0+780
是 wordgot
并且 at time2280+810
是 wordgoing
等。这个时间以毫秒为单位,因此对于时间 3090,您需要附加&t=3
到 URL。
您可以使用任何工具将 XML 拼接成可读的内容,但这是我的Power BI Desktop脚本,用于查找“特权”之类的词:
let
Source = Xml.Tables(File.Contents("C:\Download\body.xml")),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Attribute:format", Int64.Type}}),
body = #"Changed Type"{0}[body],
p = body{0}[p],
#"Changed Type1" = Table.TransformColumnTypes(p,{{"Attribute:t", Int64.Type}, {"Attribute:d", Int64.Type}, {"Attribute:w", Int64.Type}, {"Attribute:a", Int64.Type}, {"Attribute:p", Int64.Type}}),
#"Expanded s" = Table.ExpandTableColumn(#"Changed Type1", "s", {"Attribute:ac", "Attribute:p", "Attribute:t", "Element:Text"}, {"s.Attribute:ac", "s.Attribute:p", "s.Attribute:t", "s.Element:Text"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Expanded s",{{"s.Attribute:t", Int64.Type}}),
#"Removed Other Columns" = Table.SelectColumns(#"Changed Type2",{"s.Attribute:t", "s.Element:Text", "Attribute:t"}),
#"Replaced Value" = Table.ReplaceValue(#"Removed Other Columns",null,0,Replacer.ReplaceValue,{"s.Attribute:t"}),
#"Filtered Rows" = Table.SelectRows(#"Replaced Value", each [#"s.Element:Text"] <> null),
#"Added Custom" = Table.AddColumn(#"Filtered Rows", "Time", each [#"Attribute:t"] + [#"s.Attribute:t"]),
#"Filtered Rows1" = Table.SelectRows(#"Added Custom", each ([#"s.Element:Text"] = " privilege" or [#"s.Element:Text"] = " privileged" or [#"s.Element:Text"] = " privileges" or [#"s.Element:Text"] = "privilege" or [#"s.Element:Text"] = "privileges"))
in
#"Filtered Rows1"
随着 2020 年 6 月更新的 YouTube 视频,它非常简单
您将获得 .sbv 文件
Open Transcript
从...
向上/向下投票右侧的下拉列表中选择并共享链接。
这将Transcript
在右侧打开一个滚动 div。
然后您可以使用Copy
. 请注意,您不能使用Select All
但需要单击顶行,然后使用滚动拇指滚动到底部,然后按住 shift 单击最后一行。
请注意,您也可以使用普通网页搜索在此文本中进行搜索。
我只是通过在视频开头打开脚本并在时间 00:00 标记处单击并拖动鼠标左键并在开始时在几行上按下 shift 键来轻松手动完成此操作。
然后我将视频推进到接近尾声。当视频停止时,我点击了最后一句话的结尾,同时再次按住 shift 键。使用 CTRL-C 我将文本复制到剪贴板并将其粘贴到编辑器中。
完毕!
警告:确保没有 RDP-Windows 共享剪贴板或 Teamviewer 等软件同时运行,因为此过程将溢出其缓冲区,复制大量文本。