调用YouTube视频API时无法获取视频时长

时间:2020-11-10 01:07:40

标签: google-apps-script youtube youtube-api youtube-data-api

我调用YT搜索API,然后获取每个视频结果(视频API)的完整数据。

但是,我无法获得每个视频的时长。

我收到未定义的错误:

  var videoResultsFullData = YouTube.Videos.list('id,snippet', { id : searchResults.map(item => item.id.videoId)})
    var popularChannelIds = YouTube.Channels.list('id,snippet, statistics', {
         id: resultsParentChannelIds
    }).items.filter(ch => ch.statistics.subscriberCount > sh.getRange("D24").getValue() ).map(channel => channel.id);
   
    // .filter(video => popularChannelIds.includes(item.id.channelId));

    var videoPopularResultsFullData = 
    videoResultsFullData.getItems()
    .filter(video => popularChannelIds.includes(video.snippet.channelId));
// };


var range = 3;
for (var i in videoPopularResultsFullData) {
    var item = videoPopularResultsFullData[i];
    var temp = item.getContentDetails().duration;

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为在您的情况下,必须在contentDetails中加入part。那么下面的修改如何?

发件人:

var videoResultsFullData = YouTube.Videos.list('id,snippet', { id : searchResults.map(item => item.id.videoId)})

收件人:

var videoResultsFullData = YouTube.Videos.list('id,snippet,contentDetails', { id : searchResults.map(item => item.id.videoId)})

或者,您也可以如下使用part作为数组。

var videoResultsFullData = YouTube.Videos.list(["id","snippet","contentDetails"], { id : searchResults.map(item => item.id.videoId)})

参考: