如何使用YouTube API的playlistItems来获取视频时长。

huangapple go评论63阅读模式
英文:

How to get videos duration using youtube api playlistItems

问题

我想从播放列表中获取所有视频及其URL。目前这个方法可以运行,但我遇到一个问题,YouTube已经移除了使用playlistItems().list()来获取视频时长的方式。
以前可以在'contentDetails'中找到,但现在只有'videoId'和'videoPublishedAt'。
我们可以使用videos().list()来请求每个单独的视频,但我担心这种方法的速度...
也许有一种方法可以避免请求每个视频以获取它的时长吗?

对不起我的英语:)
你可以用任何语言写。

以下是我用来从播放列表中获取视频的Python代码:

youtube = build("youtube", "v3", developerKey=API_KEY)

def get_videos(url):

    # ........
    
    collected_data = []
    next_page_token = None

    while True:
        request = youtube.playlistItems().list(
            part='snippet,contentDetails',
            playlistId=playlist_id,
            maxResults=50,
            pageToken=next_page_token
        )
        response = request.execute()
        collected_data.append(response)
        next_page_token = response.get('nextPageToken')
        if not next_page_token:
            break

    return collected_data[0]
英文:

I wanna get all videos from playlist with it's url. So, it works, but I have a problem, that youtube removed a way to get videos duration using playlistItems().list().
It used to be in 'contentDetails', but now there are only 'videoId' and 'videoPublishedAt'.
We can request every single video using videos().list(), but I'm afraid of the speed of this way…
May be, there is a way to avoid using request to every video to get it's duration?

Sorry for my English 如何使用YouTube API的playlistItems来获取视频时长。
You may write in any language you want.

There is a python code that I'm using for getting videos from playlist:

youtube = build("youtube", "v3", developerKey=API_KEY)

def get_videos(url):

    # ........

    collected_data = []
    next_page_token = None

    while True:
        request = youtube.playlistItems().list(
            part='snippet,contentDetails',
            playlistId=playlist_id,
            maxResults=50,
            pageToken=next_page_token
        )
        response = request.execute()
        collected_data.append(response)
        next_page_token = response.get('nextPageToken')
        if not next_page_token:
            break

    return collected_data[0]

答案1

得分: 0

使用YouTube数据API v3视频:列表端点,只需通过向该端点发出另一个请求,就可以将您的算法速度减慢一半。实际上,似乎您并不知道这个端点支持在每个请求中提供高达50id

英文:

Using YouTube Data API v3 Videos: list endpoint it only divides the speed of your algorithm by two by making another request to this endpoint. Indeed it seems that you aren't aware that this endpoint supports up to 50 provided ids within each request.

huangapple
  • 本文由 发表于 2023年3月8日 16:37:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定