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

查看:14
本文介绍了如何使用 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代表Time Duration,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天全站免登陆