获取YouTube字幕 [英] Get YouTube captions

查看:185
本文介绍了获取YouTube字幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式获取正在播放的YouTube视频的字幕?

How can programmatically get subtitles of a playing YouTube video?

最初我尝试通过 YouTube API ,但似乎 YouTube禁止提取您不是所有者的视频字幕。

Initially I've tried to do it offline via YouTube API, but as it seems YouTube forbids to fetch subtitles of videos you are not the owner.

现在我正在尝试在线进行。我还没有找到 YouTube Player Api 字幕的方法,我也尝试过获取YouTube在 videojs 播放器将Text标记为TextTrack / 3084258>通常的视频可以做的方式,但以下不起作用:

Now I'm trying to do it online. I haven't found YouTube Player Api methods for captions, also I've tried to get YouTube captions as TextTrack with videojs player in the way it could be done for usual videos, but the following doesn't work:

<html>
<head>
<link href="//vjs.zencdn.net/4.12/video-js.css" rel="stylesheet">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="//vjs.zencdn.net/4.12/video.js"></script>
<script type="text/javascript" src="../lib/youtube.js"></script>
</head>

<body>
<video  id="myvideo"
        class="video-js vjs-default-skin vjs-big-play-centered" 
        controls 
        preload="auto" 
        width="640" 
        height="360">
</video>

<script type="text/javascript">
    var myvideo = videojs(
        "myvideo",
        {
            "techOrder": ["youtube"],
            "src": "https://www.youtube.com/watch?v=jNhtbmXzIaM" 
        },
        function() {
            console.log('Tracks: ' + this.textTracks().length); //zero here :(

            /*var aTextTrack = this.textTracks()[0];
            aTextTrack.on('loaded', function() {
                console.log('here it is');
                cues = aTextTrack.cues();
                console.log('Ready State', aTextTrack.readyState()) 
                console.log('Cues', cues);
            });
            aTextTrack.show();*/
        });
</script>
</body>
</html>

我还尝试了解析YouTube播放器IFrame的丑陋解决方案(有其中包含当前字幕行的div,但由于ori而无效杜松子酒不匹配的安全问题。

I've also tried an ugly solution with parsing YouTube Player IFrame (there is a div inside it with current subtitles' line), but it doesn't work because of origin mismatch security issues.

我的目标是否可以通过java(离线解决方案)或javascript实现目标对于在线解决方案)?

Is there any way my goal can be achieved in java (for offline solutions) or javascript (for online solutions)?

推荐答案

我如何设法从YouTube视频获取字幕是通过向此网址发出简单请求 https://video.google.com/timedtext?lang= {LANG}& v = {videoId}

How I managed to get the captions from a youtube video is by making a simple request to this url https://video.google.com/timedtext?lang={LANG}&v={videoId}

我曾尝试使用Youtube API v3但目前它不起作用。当您在某个视频上使用Youtube API v3发出请求时,您需要上传视频的人批准标题的下载,否则您将在控制台中出现403错误。出现错误是正常的,服务器没有收到批准,因此会返回错误。

I have tried to use the Youtube API v3 but at the moment it doesn't work. When you do a request with the Youtube API v3 on a certain video you need that the person which uploaded the video to approve the caption's download, if not you'll have a 403 error in the console. It's normal to have the error, the server doesn't receive the approval so it returns an error.

您可以使用Youtube API v3从自己的视频中下载字幕。

You can download the captions from your own video with the Youtube API v3.

与此类似的东西可以胜任。响应将采用XML格式:

Something similar to this will do the job. The response will come in an XML format:

   $.ajax({
        type: "POST",
        url: "https://video.google.com/timedtext?lang=en&v=5MgBikgcWnY"
    }).done(function (response) {
        console.log(response);
    }).fail(function (response) {
        console.log();
    });

这篇关于获取YouTube字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆