以下是一些链接,以xml格式提供了YouTube视频的字幕列表。
'https://www.youtube.com/api/timedtext?lang=en&v=6dlr-1Qk8Uc'
'http://video.google.com/timedtext?type=track&v=zenMEj0cAC4&id=0&lang=en'
但它并不适用于所有视频。 某些视频在视频底部带有字幕(cc)图标,单击该字幕会出现,但是此链接无法返回字幕数据。
然后,我在单击cc图标后检查了重新放置的数据,它返回了所有带有字幕的视频的数据。
但是我不知道如何使用节点js调用此api。
答案 0 :(得分:0)
代替使用YouTube Data API-caption 1 来检索视频的字幕,您还可以使用AJAX回调获取字幕。
1 如果您想使用YouTube数据API查询字幕,我建议您read the documentation carefully。
在此示例中,AJAX回调用于从给定的YouTube视频中检索字幕。
使用XML parser,在for循环中迭代前一个AJAX回调的响应以获取标题:
// Ajax callback to the YouTube channel:
$.ajax({
type: "GET",
url: "http://video.google.com/timedtext?type=track&v=zenMEj0cAC4&id=0&lang=en",
crossDomain: true,
}).done(function(data) {
getCaption(data);
});
// Variables.
var parser, xmlDoc;
var HTML_captions = "";
// Parse the AJAX response and get the captions.
function getCaption(data) {
try {
// Loop the results of the ajax:
for (var i = 0; i < data.getElementsByTagName("transcript")[0].childNodes.length; i++) {
HTML_captions += data.getElementsByTagName("transcript")[0].childNodes[i].innerHTML + "<br/>";
}
// Preparing captions...
fillData();
} catch (err) {
console.log(err);
alert('Error at getCaption function - see console form more details.');
}
}
// Fill the data "captions" in a HTML "div" control.
function fillData() {
try {
document.getElementById("demo").innerHTML = HTML_captions;
} catch (err) {
console.log(err);
alert('Error at fillData function - see console form more details.');
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<div class="infoVideo">
<span>These are the captions of the following YouTube video:</span>
<br/>
<span>Title: How Turbochargers Work</span>
<br/>
<span>URL: https://www.youtube.com/watch?v=zenMEj0cAC4</span>
</div>
<br/>
<div id="demo"><i>Loading captions...</i></div>