【发布时间】:2015-11-15 12:52:33
【问题描述】:
如何以编程方式获取正在播放的 YouTube 视频的字幕?
最初我尝试通过YouTube API 离线进行,但as it seems YouTube 禁止获取非所有者视频的字幕。
现在我正在尝试在线进行。我还没有找到 YouTube Player Api 的字幕方法,我也尝试在 the way it could be done for usual videos 中使用 videojs 播放器将 YouTube 字幕作为 TextTrack,但以下方法不起作用:
<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),但由于来源不匹配的安全问题,它不起作用。
有什么方法可以在 java(离线解决方案)或 javascript(在线解决方案)中实现我的目标?
【问题讨论】:
-
我对 YouTube API 了解不多,我知道这是一个 Ruby 而不是 Java 解决方案,但希望 this GitHub repo 可以引导您走向正确的方向。
-
@NobleMushtak,谢谢,我已经注意到这个解决方案)
标签: javascript java video youtube-api video.js