英文:
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
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的视频:列表端点,只需通过向该端点发出另一个请求,就可以将您的算法速度减慢一半。实际上,似乎您并不知道这个端点支持在每个请求中提供高达50
个id
。
英文:
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 id
s within each request.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论