You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取YouTube直播流统计数据并将直播视频统计数据保存至文件?API参数选择咨询

Hey there! Let's break down how to grab those YouTube live stream stats (both real-time and post-stream) and save them to a file—you’re already ahead by checking out the YouTube Data API, so let’s fill in the parameter and workflow gaps for you.

1. First: Get Your API Set Up

Before diving into endpoints, make sure you have:

  • A Google Cloud Project with the YouTube Data API v3 enabled
  • An API key (for public streams) or OAuth 2.0 credentials (for private/unlisted streams, since API keys only work for public content)

2. Fetching Real-Time Live Stream Stats

To get real-time data like concurrent viewers and cumulative views while the stream is active, use the videos.list endpoint with these critical parameters:

Required Parameters

  • id: The video ID of your live stream (you can find this in the YouTube Studio URL, or fetch it via liveBroadcasts.list with broadcastStatus=active to get active streams automatically)
  • part: Must include liveStreamingDetails (for real-time viewer count) and statistics (for cumulative views, likes, etc.)
  • key: Your API key (skip this if using OAuth)

Example cURL Request

curl "https://www.googleapis.com/youtube/v3/videos?id=YOUR_LIVE_VIDEO_ID&part=snippet,liveStreamingDetails,statistics&key=YOUR_API_KEY"

Key Data Points in the Response

  • items[0].liveStreamingDetails.concurrentViewers: Current number of people watching the stream
  • items[0].statistics.viewCount: Total cumulative views for the stream so far
  • items[0].liveStreamingDetails.actualStartTime: When the stream went live

3. Fetching Post-Stream Video Stats

Once the stream ends, it becomes a regular YouTube video. You can still pull stats like total views, likes, comments using the same videos.list endpoint—just focus on the statistics part:

Parameters

  • id: Same video ID from the live stream
  • part: statistics (add snippet if you want video title/metadata too)
  • key: Your API key

Example Response Snippet

{
  "items": [
    {
      "statistics": {
        "viewCount": "12500",
        "likeCount": "420",
        "commentCount": "85"
      }
    }
  ]
}

4. Saving Stats to a File

Once you have the API response, you can use any language to parse and save the data. Here’s a quick Python example to save both JSON and CSV formats:

import requests
import json
import csv

# Replace with your credentials and video ID
API_KEY = "YOUR_API_KEY"
VIDEO_ID = "YOUR_LIVE_VIDEO_ID"

# Fetch data from API
response = requests.get(
    "https://www.googleapis.com/youtube/v3/videos",
    params={
        "id": VIDEO_ID,
        "part": "snippet,liveStreamingDetails,statistics",
        "key": API_KEY
    }
)
stream_data = response.json()

# Save full response to JSON file
with open("live_stream_full_stats.json", "w") as json_file:
    json.dump(stream_data, json_file, indent=4)

# Extract key fields and save to CSV
stream_stats = stream_data["items"][0]
with open("live_stream_summary.csv", "w", newline="") as csv_file:
    writer = csv.writer(csv_file)
    # Write header
    writer.writerow(["Video Title", "Concurrent Viewers", "Total Views", "Stream Start Time"])
    # Write data (handle cases where live data might not exist post-stream)
    writer.writerow([
        stream_stats["snippet"]["title"],
        stream_stats["liveStreamingDetails"].get("concurrentViewers", "Stream Ended"),
        stream_stats["statistics"]["viewCount"],
        stream_stats["liveStreamingDetails"].get("actualStartTime", "N/A")
    ])

Quick Tips to Avoid Headaches

  • API Quotas: Each videos.list call uses 1 quota unit—don’t spam requests (e.g., limit real-time checks to once every 30-60 seconds) to avoid hitting quota limits.
  • Real-Time Updates: For ongoing monitoring, set up a loop with a timer (like time.sleep(60) in Python) to pull data periodically and append it to your file.
  • Private Streams: If your stream is private/unlisted, use OAuth 2.0 instead of an API key to authenticate—this lets the API access content you have permissions for.

内容的提问来源于stack exchange,提问作者Arindam Deka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 11:12:31