You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过YouTube Data API v3获取频道最新视频并排除Shorts短视频?

Hey there! The problem with your current YouTube Data API request is that the Search endpoint doesn’t have a direct way to filter out Shorts specifically—YouTube doesn’t expose a dedicated "isShort" flag in the API for this. But we’ve got two solid workarounds, depending on how strict you need your filtering to be:

方案一:快速近似过滤(使用videoDuration参数)

If you’re okay with excluding all short videos (not just Shorts) that are under 60 seconds, this is the simplest fix. Just add the videoDuration parameter to your existing request, specifying medium,long to only return videos that are 60 seconds or longer:

https://www.googleapis.com/youtube/v3/search?part=id&order=date&channelId=CHANNELID&maxResults=1&type=video&key=KEY&videoDuration=medium,long

注意事项

  • This will exclude any non-Short videos that are under 60 seconds too, so if your channel regularly posts short non-Short content, this might not be ideal.
  • Most Shorts fall into the short duration category, so this works for the vast majority of cases.

方案二:精确过滤(验证视频时长)

If you need to be more precise (e.g., you want to keep short non-Short videos but exclude only Shorts), you’ll need to fetch video details and filter based on duration explicitly. Here’s how to do it step-by-step:

  1. 获取频道的官方上传播放列表ID
    每个YouTube频道都有一个专属的上传视频播放列表。调用Channels接口获取它的ID:

    https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=CHANNELID&key=KEY
    

    从响应中提取items[0].contentDetails.relatedPlaylists.uploads的值(格式类似UUxxxxxx)。

  2. 从上传播放列表获取最新视频
    使用PlaylistItems接口拉取最新的5个视频(多取几个以防前几个都是Shorts):

    https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&playlistId=UPLOADS_PLAYLIST_ID&maxResults=5&order=date&key=KEY
    

    收集响应中所有contentDetails.videoId的值。

  3. 获取视频时长并过滤
    调用Videos接口获取这些视频ID的时长信息:

    https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=VIDEO_ID_1,VIDEO_ID_2,VIDEO_ID_3,VIDEO_ID_4,VIDEO_ID_5&key=KEY
    

    解析contentDetails.duration字段(采用ISO 8601格式,例如PT1M20S代表1分20秒),过滤掉总时长不足60秒的视频。过滤后的列表中第一个视频(因为我们按日期排序)就是你要找的最新非Short视频。

为什么用上传播放列表而不是Search接口?

上传播放列表能确保你只获取频道直接上传的视频,而Search接口可能包含频道分享或提及的其他视频——这让这个方法对你的需求来说更准确。

内容的提问来源于stack exchange,提问作者Paul Berger

火山引擎 最新活动