是否可以通过YouTube API获得多少人正在观看YouTube实时视频?

时间:2019-07-04 13:04:31

标签: javascript youtube youtube-api youtube-data-api

我将使用YouTube API构建应用程序。我从API那里获取视频事件,并在直播时在网站上播放视频。

我正在获取我需要的所有数据,除了有多少人在观看视频。

有什么办法吗?

2 个答案:

答案 0 :(得分:2)

是的,您可以请求视频对象并通过liveStreamingDetails.concurrentViewers

获取并发观众数量

以下是API文档参考:https://developers.google.com/youtube/v3/docs/videos#liveStreamingDetails.concurrentViewers

例如,这样的请求:

GET https://www.googleapis.com/youtube/v3/videos?part=liveStreamingDetails&id={YT ID}&fields=items%2FliveStreamingDetails%2FconcurrentViewers&key={YOUR_API_KEY}

答案 1 :(得分:2)

您可以通过在请求视频详细信息时请求liveStreamingDetails部分来获取这些统计信息。这是一个示例HTTPS请求(ref):

GET https://www.googleapis.com/youtube/v3/videos?part=snippet%2Cstatistics%2CliveStreamingDetails&id=21X5lGlDOfg&key=[YOUR_API_KEY] HTTP/1.1

Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json

结果:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/ARYNDpdj0hFzJ8zKV9SLVLV4PWE\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#video",
   "etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/_SdqmAfikf6n_Jny6hfytlYz1wY\"",
   "id": "21X5lGlDOfg",
   "snippet": {
    "publishedAt": "2018-12-28T18:40:15.000Z",
    "channelId": "UCLA_DiR1FfKNvjuUpBHmylQ",
    "title": "NASA Live: Official Stream of NASA TV",
    "description": "Direct from America's space program to YouTube, watch NASA TV live streaming here to get the latest from our..."
   },
   "statistics": {
    "viewCount": "8512883",
    "likeCount": "130811",
    "dislikeCount": "6836",
    "favoriteCount": "0",
    "commentCount": "0"
   },
   "liveStreamingDetails": {
    "actualStartTime": "2018-12-28T18:41:30.221Z",
    "scheduledStartTime": "2018-12-28T19:00:00.000Z",
    "concurrentViewers": "108"
   }
  }
 ]
}

看看concurrentViewers元素。