通过Youtube API v3的YouTube.PlaylistItems.List请求,我如何检索视频的持续时间?

huangapple go评论66阅读模式
英文:

How do I retrieve durations of videos through Youtube API v3 YouTube.PlaylistItems.List request?

问题

我正在使用Youtube API v3(Java)来访问Youtube视频和播放列表。以下是示例代码:

    YouTube.PlaylistItems.List request = 
    youtube.playlistItems().list("id,contentDetails,snippet");
    request.setPlaylistId(/*播放列表ID*/);                
    request.setFields("items(contentDetails/videoId,snippet/title,snippet/publishedAt, 
    snippet/description),nextPageToken,pageInfo");

    PlaylistItemListResponse result = request.execute();

现在我可以像这样从播放列表中获取每个视频的标题:

    for (PlaylistItem item : result.getItems()) {
        item.getSnippet().getTitle();
        // 但是我如何获取视频的时长呢?
    }

我尝试了不同的方法来获取视频的时长,但它们都返回了null。我如何获取时长?

英文:

I'm using Youtube API v3 (Java) to access Youtube videos, playlists. Below is a sample code:

YouTube.PlaylistItems.List request = 
youtube.playlistItems().list("id,contentDetails,snippet");
request.setPlaylistId(/*playlist id*/);                
request.setFields("items(contentDetails/videoId,snippet/title,snippet/publishedAt, 
snippet/description),nextPageToken,pageInfo");

PlaylistItemListResponse result = request.execute();

Now I can get a title of each video from a playlist like this:

for (PlaylistItem item : result.getItems()) {
    item.getSnippet().getTitle();
    // But how can I retrieve a duration now?
}

I have tried different methods to retrieve duration of the videos, but they all returned null. How do I get a duration?

答案1

得分: 0

请看这个:我找到了一个博客

  for (Video playlistItem : playlistItemList) {
    YouTubeData map = new YouTubeData();

    map.mVideo = playlistItem.getId();
    map.mDuration = Utils.durationToDuration((String) playlistItem.getContentDetails()
        .get("duration"));
    map.mTitle = playlistItem.getSnippet().getTitle();
    map.mDescription = Utils.condenseWhiteSpace(playlistItem.getSnippet().getDescription());
    map.mThumbnail = thumbnailURL(playlistItem.getSnippet().getThumbnails());
    map.mPublishedDate = playlistItem.getSnippet().getPublishedAt().getValue();

    result.add(map);
  }
英文:

Check out this: I found a blog

  for (Video playlistItem : playlistItemList) {
    YouTubeData map = new YouTubeData();

    map.mVideo = playlistItem.getId();
    map.mDuration = Utils.durationToDuration((String) playlistItem.getContentDetails()
        .get("duration"));
    map.mTitle = playlistItem.getSnippet().getTitle();
    map.mDescription = Utils.condenseWhiteSpace(playlistItem.getSnippet().getDescription());
    map.mThumbnail = thumbnailURL(playlistItem.getSnippet().getThumbnails());
    map.mPublishedDate = playlistItem.getSnippet().getPublishedAt().getValue();

    result.add(map);
  }

答案2

得分: 0

视频时长可以在 .videos().list()contentDetails 部分找到。不幸的是,通过你的示例,你只能通过 PlayListItem 获取 video_id,无法直接获取视频时长。

因此,你可以提取播放列表中的所有 video_id,然后进行额外的调用来接收视频数据,例如:

https://www.googleapis.com/youtube/v3/videos?id=9bZkp7q19f0&part=contentDetails&key={YOUR_API_KEY}

作为结果,你将会得到类似于:

"contentDetails": {
    "duration": "PT4M13S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": true,
    "regionRestriction": {
     "blocked": [
      "DE"
     ]
 }

其中有一个字段 duration

你可以在这里使用所有的 YouTube API 链接

英文:

Video duration can be found inside contentDetails part of .videos().list(). Unfortunatelly, you can get video duration through PlayListItem as in your example, you can get only video_id.

So, you can extract all video_id that playlist has and make an additional call to receive video data, like:

https://www.googleapis.com/youtube/v3/videos?id=9bZkp7q19f0&part=contentDetails&key={YOUR_API_KEY}

as a result, you will get something like:

"contentDetails": {
    "duration": "PT4M13S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": true,
    "regionRestriction": {
     "blocked": [
      "DE"
     ]
 }

There is a field duration inside.

You can play with all YouTube API here

huangapple
  • 本文由 发表于 2020年4月11日 01:02:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/61145082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定