如何获得YouTube API版本3的视频时长? [英] How do I get video durations with YouTube API version 3?

查看:211
本文介绍了如何获得YouTube API版本3的视频时长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用YouTube API v3搜索YouTube.

I'm using YouTube API v3 to search YouTube.

https://developers.google.com/youtube/v3/docs/search

如您所见,响应JSON不包含视频时长.有没有办法获取视频时长?

As you can see, the response JSON doesn't contains video durations. Is there way to get video durations?

最好不要再次为结果中的每个元素调用API(除非这是获取持续时间的唯一方法).

Preferably not calling an API for each element in the result again (unless that's the only way to get durations).

推荐答案

进行搜索调用后,您将必须调用YouTube数据API的视频资源.您最多可以在搜索中输入50个视频ID,因此不必为每个元素都调用它.

You will have to make a call to the YouTube data API's video resource after you make the search call. You can put up to 50 video IDs in a search, so you won't have to call it for each element.

https://developers.google.com/youtube/v3/docs/视频/列表

您将要设置part=contentDetails,因为持续时间在那里.

You'll want to set part=contentDetails, because the duration is there.

例如,以下调用:

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

给出以下结果:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"XlbeM5oNbUofJuiuGi6IkumnZR8/ny1S4th-ku477VARrY_U4tIqcTw\"",
 "items": [
  {

   "id": "9bZkp7q19f0",
   "kind": "youtube#video",
   "etag": "\"XlbeM5oNbUofJuiuGi6IkumnZR8/HN8ILnw-DBXyCcTsc7JG0z51BGg\"",
   "contentDetails": {
    "duration": "PT4M13S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": true,
    "regionRestriction": {
     "blocked": [
      "DE"
     ]
    }
   }
  }
 ]
}

时间格式为ISO 8601字符串. PT代表时间,4M是4分钟,13S是13秒.

The time is formatted as an ISO 8601 string. PT stands for Time Duration, 4M is 4 minutes, and 13S is 13 seconds.

这篇关于如何获得YouTube API版本3的视频时长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆