如何获取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 vialiveBroadcasts.listwithbroadcastStatus=activeto get active streams automatically)part: Must includeliveStreamingDetails(for real-time viewer count) andstatistics(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 streamitems[0].statistics.viewCount: Total cumulative views for the stream so faritems[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 streampart:statistics(addsnippetif 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.listcall 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

