如何从YouTube视频中提取隐藏式字幕文字?

Min*_* Yu 48 youtube caption

是否可以从Youtube视频中提取隐藏式字幕脚本?

我们在youtube上有200多个网络广播,每个网络广播至少有一个小时.Youtube已经关闭了所有视频的标题,但似乎用户无法获得它.

我在此博客中尝试过该网址,但它不适用于我们的视频.

http://googlesystem.blogspot.com/2010/10/download-youtube-captions.html

谢谢

小智 36

以下是如何获取YouTube视频的文字记录(如果有):

  • 转到YouTube并打开您选择的视频.
  • 单击"共享"按钮旁边的"更多操作"按钮(3个水平点).
  • 点击"打开成绩单"

虽然语法可能有点傻,但这是一个非常好的解决方案.

资料来源:http://ccm.net/faq/40644-youtube-how-to-get-the-transcript-of-a-video


nis*_*ama 21

另一种选择是使用youtube-dl

youtube-dl --skip-download --write-auto-sub $youtube_url
Run Code Online (Sandbox Code Playgroud)

默认格式为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
Run Code Online (Sandbox Code Playgroud)

您可以ffmpeg用来将字幕文件转换为另一种格式:

ffmpeg -i input.vtt output.srt
Run Code Online (Sandbox Code Playgroud)

这是VTT字幕的样子:

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>
Run Code Online (Sandbox Code Playgroud)

以下是相同的字幕,文件的顶部没有该部分,也没有标签:

00:00:01.429 --> 00:00:04.249 align:start position:0%

ladies and gentlemen I'd like to thank

00:00:04.249 --> 00:00:04.259 align:start position:0%
ladies and gentlemen I'd like to thank


00:00:04.259 --> 00:00:05.930 align:start position:0%
ladies and gentlemen I'd like to thank
you for coming tonight especially at

00:00:05.930 --> 00:00:05.940 align:start position:0%
you for coming tonight especially at


00:00:05.940 --> 00:00:07.730 align:start position:0%
you for coming tonight especially at
such short notice

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 sure mr. Irving will fill you in on

00:00:09.620 --> 00:00:09.630 align:start position:0%
I'm sure mr. Irving will fill you in on


00:00:09.630 --> 00:00:11.030 align:start position:0%
I'm sure mr. Irving will fill you in on
the circumstances that's brought us
Run Code Online (Sandbox Code Playgroud)

您会看到每个字幕文本重复了三遍。每八行(第三,十一,十九和二十七)会有一个新的字幕文本。

这会将VTT字幕转换为更简单的格式:

sed '1,/^$/d' *.vtt| # remove the part at the top
sed 's/<[^>]*>//g'| # remove tags
awk -F. 'NR%8==1{printf"%s ",$1}NR%8==3' # print each new subtitle text and its start time without milliseconds
Run Code Online (Sandbox Code Playgroud)

上面命令的输出如下所示:

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
Run Code Online (Sandbox Code Playgroud)

这将以简化格式输出视频的隐藏式字幕:

cap()(cd /tmp;rm -f *.vtt;youtube-dl --skip-download --write-auto-sub "$1";sed '1,/^$/d' *.vtt|sed 's/<[^>]*>//g'|awk -F. 'NR%8==1{printf"%s ",$1}NR%8==3'|tee cap)

  • 截至 2019 年,这是唯一有效的解决方案。我认为 YouTube 视频下载(我认为是字幕下载)是一个不断变化的目标。youtube-dl 人员是唯一始终能够从 YouTube 自动下载的人,可能是因为他们积极确保它继续工作。 (4认同)
  • 对于那些想要使用相对路径保存 subs 文件的人来说,这是一个详细的 bash 脚本。结果保存为纯文本,删除时间、换行符和其他标记。/sf/answers/4290749361/ (2认同)

ton*_*gil 18

您可以通过访问来查看/复制/下载youtube的隐藏式字幕文件的时间编码的xml文件

http://video.google.com/timedtext?lang=[LANGUAGE]&v=[YOUTUBE VIDEO IDENTIFIER]
Run Code Online (Sandbox Code Playgroud)

例如 http://video.google.com/timedtext?lang=pt&v=WSVKbw7LC2w

注意:此方法不会下载自动生成的隐藏式字幕,即使您使用的语言正确(可能还有自动生成语言的特殊代码).

  • 截至2017年5月,这不再适用(我猜测video.google.com不再适用于Youtube API.任何其他谷歌工具提取字幕? (3认同)
  • 谢谢你的抬头,但是......你必须遇到一些问题或其他问题.这个解决方案仍然有效,只是测试了它.它可能是一些格式化选项(语言,也许?).发布视频链接,我会直接双重检查. (2认同)
  • @nilanjan首先,它在PORTUGUESE中没有字幕.你表示语言为PORTUGUESE(lang = pt).其次,所述视频只有自动生成的字幕,正如我在答案中写的那样,这个脚本不会下载.尝试上传字幕的另一个视频,你会发现它有效. (2认同)

Pet*_*ann 15

以下文档显示,只有渠道所有者才能通过标准YouTube界面执行此操作:https://developers.google.com/youtube/2.0/developers_guide_protocol_captions?hl = zh- CN

廉价修复:您可以单击"交互式转录"按钮 - 并以这种方式复制内容.当然,你会以这种方式失去毫秒.

非常便宜的修复:共享的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 API 2.0已被[API 3.0](https://developers.google.com/youtube/v3/docs/captions)取代,并且根据此API下载字幕[会产生"大约200个单位的配额成本"]( https://developers.google.com/youtube/v3/docs/captions/download).他们没有提到这个配额是如何分配的,也没有提到谁,所以这个解决方案对大多数只想下载字幕而不是赞赏某些API的人来说并没有用. (9认同)

Pal*_*ndo 9

您可以从 YouTube 下载流媒体字幕 保留订阅 DownSubSaveSubs

您可以选择自动抄本或作者提供的隐藏式字幕。它还提供了使用谷歌翻译将英文字幕自动翻译成其他语言的可能性。

  • 截至 2017 年 2 月 19 日,DownSub 推送恶意软件:它下载 Flash 安装程序的黑客版本 (3认同)
  • DownSub (http://downsub.com/) 是 KeepSubs 的替代方案。我只用过一次(今天),它似乎工作得很好。 (2认同)

Car*_*lsh 5

(强制性“这可能是youtube.com内部界面,可能随时中断”)

而不是链接到执行此操作的其他工具,而是对“如何执行此操作”问题的解答

我使用了提琴手来检查youtube.com的HTTP流量,并且有一个响应,/api/timedtext其中包含以XML格式显示的隐藏式字幕信息。

似乎是这样的响应:

    <p t="0" d="5430" w="1">
        <s p="2" ac="136">we&#39;ve</s>
        <s t="780" ac="252"> got</s>
    </p>
    <p t="2280" d="7170" w="1">
        <s ac="243">we&#39;re</s>
        <s t="810" ac="233"> going</s>
    </p>
Run Code Online (Sandbox Code Playgroud)

表示时间0是单词we've,时间0+780是单词got,时间2280+810是单词going,等等。这个时间以毫秒为单位,因此对于时间3090,您要附加&t=3到URL。

您可以使用任何工具将XML拼接在一起以使其易于阅读,但这是我的Power BI Desktop脚本,用于查找诸如“ privilege”之类的词:

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"
Run Code Online (Sandbox Code Playgroud)


Jus*_*ers 5

有一个免费的 Python 工具叫做YouTube 脚本 API

您可以在脚本中或作为命令行工具使用它:

pip install youtube_transcript_api
Run Code Online (Sandbox Code Playgroud)

  • 这是 2021 年终于对我有用的答案 (2认同)