获取 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 播放器作为 TextTrack">普通视频可以采用的方式,但以下方法不起作用:
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 Player IFrame 的丑陋解决方案(其中有一个带有当前字幕行的 div),但由于来源不匹配的安全问题,它不起作用.
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 视频中获取字幕是通过向这个 url 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();
});
相关文章