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

如何通过YouTube Data API匹配频道公开Videos标签页:排除归档直播并保留首映视频

How to Distinguish Premieres (Shown in /videos Tab) from Archived Streams (Hidden) via YouTube Data API

You've hit a tricky edge case with the YouTube Data API—unfortunately, the liveBroadcastContent and liveStreamingDetails fields don't directly distinguish between premiere videos that show up in a channel's /videos tab and archived streams that get hidden from that view. Both types of content are treated as post-live content by the API, hence the identical none value and presence of liveStreamingDetails.

That said, there are two reliable workarounds based on content patterns that YouTube creators typically follow:

1. Check the Video Category ID

Premiere videos are almost always tagged under the "Entertainment" (Category ID 22) or general video categories, while archived streams are usually categorized under "Live Events" (Category ID 29). This is a quick check, though note it's not 100% foolproof if a creator manually changes the category.

2. Compare Published Time vs. Live End Time

Premieres are scheduled in advance, so a premiere video's snippet.publishedAt timestamp will be set long before the actual broadcast ends (liveStreamingDetails.actualEndTime). For regular archived streams, the publishedAt timestamp is usually very close to (or even matches) the actualEndTime, since the archive is published immediately after the stream ends.

Modified MRE with These Checks

Here's how to update your code to implement these checks:

import requests
from datetime import datetime

YT_API_KEY: str = ""
VIDEO_URL = "https://www.googleapis.com/youtube/v3/videos"

def appears_in_videos_tab(video):
    # First check: Category ID
    category_id = video["snippet"].get("categoryId")
    if category_id == "22":  # General video category
        return True
    if category_id == "29":  # Live event category
        return False
    
    # Fallback check: Time difference between publish and live end
    published_at = datetime.fromisoformat(video["snippet"]["publishedAt"].replace("Z", "+00:00"))
    end_time = datetime.fromisoformat(video["liveStreamingDetails"]["actualEndTime"].replace("Z", "+00:00"))
    time_diff_hours = (end_time - published_at).total_seconds() / 3600
    
    # Assume premiere if published at least 1 hour before the stream ended
    return time_diff_hours > 1

for video_id in ["IcZxVrRrzcM", "6-qDqVXDZbY"]:
    resp = requests.get(
        VIDEO_URL,
        params={
            "part": "snippet,liveStreamingDetails",
            "id": video_id,
            "key": YT_API_KEY,
        },
        timeout=30,
    ).json()
    video = resp["items"][0]
    print(
        video_id,
        video["snippet"].get("liveBroadcastContent"),
        "liveStreamingDetails" in video,
        "Shows in /videos tab" if appears_in_videos_tab(video) else "Hidden from /videos tab"
    )

The Most Accurate (But More Work) Approach

If you need 100% accuracy, pull the channel's official "Uploads" playlist (this is exactly what populates the /videos tab) using the Channels.list API with part=contentDetails. Then, use PlaylistItems.list to iterate through the playlist and check if your target video ID is present. This avoids relying on content patterns, but requires additional API calls (and pagination if the channel has many videos).

It's worth noting that as of now, YouTube doesn't provide a dedicated API field to flag whether a video appears in the /videos tab—these workarounds are the best available options.

内容的提问来源于stack exchange,提问作者FluidMechanics Potential Flows

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 09:17:40