Youtube v3 Api 通过 ID 获取视频
Youtube v3 Api Get video by ID
如何使用 Youtube API v3
.
通过其 ID
获取单个视频
到目前为止,我只遇到过搜索机制和其他,但不是专门用于获取单个视频的机制,我目前拥有的代码是:
def youtube_search(q, max_results=1,order="relevance", token=None, location=None, location_radius=None):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
search_response = youtube.search().list(
q=q,
type="video",
pageToken=token,
order = order,
part="id,snippet",
maxResults=max_results,
location=location,
locationRadius=location_radius
).execute()
videos = []
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append(search_result)
try:
nexttok = search_response["nextPageToken"]
return(nexttok, videos)
except Exception as e:
nexttok = "last_page"
return(nexttok, videos)
但我发现这种方法效率不高,反而浪费时间和资源。我该怎么做?
YouTube API 搜索调用用于搜索。您要使用 videos call 并传入要查询的视频 ID。
示例:
https://www.googleapis.com/youtube/v3/videos?part=snippet&id=xE_rMj35BIM&key=YOUR_KEY
在 Google 游戏机和游乐场中配置您的 api。获取 api 密钥和访问令牌,然后调用此 API 通过 Id 获取视频。
你也可以从这里测试 link
GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=vuKRKt7aKpE&key=[YOUR_API_KEY]
Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
访问 Python 中的特定视频可以通过 video()
方法而不是 search()
方法完成,并在 list()
方法中使用参数 id
. id
是一个参数,可以在格式为 https://www.youtube.com/watch?v={video_id}[&{otherParameters}={values}]
的 Youtube 视频的 URL 中找到(例如 https://www.youtube.com/watch?v=qEB0FIBoKAg&t=15s
)。在您的 URL 中,v
的值(在“=”符号之后)是您当前正在观看的视频的 ID。 “&”符号表示添加其他参数(这里,t
是“视频开始时间”,从15秒开始)。
我在代码中的做法是:
...
# Create API service
serv = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
# Make request. part must be "snippet", id is your video, and maxResults is optional
req = serv.videos().list(part = "snippet", id = video_id, maxResults = 1)
# get the JSON file (can be accessed as a dictionary)
response = req.execute()
# Access the (standard) thumbnail (URL)
sd_thumbnail = response["items"][0]["snippet"]["thumbnails"]["standard"]["url"]
# If you want other resolutions than "standard" you can change it into "default", "medium" or "high"
print(sd_thumbnail)
此程序将打印:
https://i.ytimg.com/vi/qEB0FIBoKAg/sddefault.jpg
它的格式是https://i.ytimg.com/vi/{video_id}/{file}.jpg
这里的 file
可以是 "default"、"mqdefault"、"hqdefault" 和 "sddefautl"。
Youtube 有很好的 API 文档。关于您的问题的更多信息可以在 https://developers.google.com/youtube/v3/docs/videos/list
中得到解答
如何使用 Youtube API v3
.
ID
获取单个视频
到目前为止,我只遇到过搜索机制和其他,但不是专门用于获取单个视频的机制,我目前拥有的代码是:
def youtube_search(q, max_results=1,order="relevance", token=None, location=None, location_radius=None):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
search_response = youtube.search().list(
q=q,
type="video",
pageToken=token,
order = order,
part="id,snippet",
maxResults=max_results,
location=location,
locationRadius=location_radius
).execute()
videos = []
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append(search_result)
try:
nexttok = search_response["nextPageToken"]
return(nexttok, videos)
except Exception as e:
nexttok = "last_page"
return(nexttok, videos)
但我发现这种方法效率不高,反而浪费时间和资源。我该怎么做?
YouTube API 搜索调用用于搜索。您要使用 videos call 并传入要查询的视频 ID。
示例:
https://www.googleapis.com/youtube/v3/videos?part=snippet&id=xE_rMj35BIM&key=YOUR_KEY
在 Google 游戏机和游乐场中配置您的 api。获取 api 密钥和访问令牌,然后调用此 API 通过 Id 获取视频。
你也可以从这里测试 link
GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=vuKRKt7aKpE&key=[YOUR_API_KEY]
Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
访问 Python 中的特定视频可以通过 video()
方法而不是 search()
方法完成,并在 list()
方法中使用参数 id
. id
是一个参数,可以在格式为 https://www.youtube.com/watch?v={video_id}[&{otherParameters}={values}]
的 Youtube 视频的 URL 中找到(例如 https://www.youtube.com/watch?v=qEB0FIBoKAg&t=15s
)。在您的 URL 中,v
的值(在“=”符号之后)是您当前正在观看的视频的 ID。 “&”符号表示添加其他参数(这里,t
是“视频开始时间”,从15秒开始)。
我在代码中的做法是:
...
# Create API service
serv = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
# Make request. part must be "snippet", id is your video, and maxResults is optional
req = serv.videos().list(part = "snippet", id = video_id, maxResults = 1)
# get the JSON file (can be accessed as a dictionary)
response = req.execute()
# Access the (standard) thumbnail (URL)
sd_thumbnail = response["items"][0]["snippet"]["thumbnails"]["standard"]["url"]
# If you want other resolutions than "standard" you can change it into "default", "medium" or "high"
print(sd_thumbnail)
此程序将打印:
https://i.ytimg.com/vi/qEB0FIBoKAg/sddefault.jpg
它的格式是https://i.ytimg.com/vi/{video_id}/{file}.jpg
这里的 file
可以是 "default"、"mqdefault"、"hqdefault" 和 "sddefautl"。
Youtube 有很好的 API 文档。关于您的问题的更多信息可以在 https://developers.google.com/youtube/v3/docs/videos/list
中得到解答