本文整理汇总了Python中youtube_dl.YoutubeDL.process_video_result方法的典型用法代码示例。如果您正苦于以下问题:Python YoutubeDL.process_video_result方法的具体用法?Python YoutubeDL.process_video_result怎么用?Python YoutubeDL.process_video_result使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类youtube_dl.YoutubeDL
的用法示例。
在下文中一共展示了YoutubeDL.process_video_result方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import process_video_result [as 别名]
class YoutubeAPI:
def __init__(self):
credentials = _read_json("credentials.json")
self.api_key = credentials["google_key"]
self.http_session = aiohttp.ClientSession()
self.ytdl = YoutubeDL({
"format": "webm[abr>0]/bestaudio/best",
"restrictfilenames": True,
"noplaylist": True,
"nocheckcertificate": True,
"ignoreerrors": False,
"logtostderr": False,
"quiet": True,
"no_warnings": True,
"default_search": "error",
"source_address": "0.0.0.0",
"prefer_ffmpeg": True
})
self.extractor = YoutubeIE()
self.extractor._downloader = self.ytdl
self.raw_video_id_regex = re.compile(r"^([\w-]+)$")
self.video_id_regex = re.compile(r"watch/?\?v=([\w-]+)")
self.playlist_id_regex = re.compile(r"playlist/?\?list=([\w-]+)")
def determine_query_type(self, query):
raw_video_match = self.raw_video_id_regex.search(query)
video_match = self.video_id_regex.search(query)
playlist_match = self.playlist_id_regex.search(query)
print(raw_video_match, video_match, playlist_match, query)
if raw_video_match is not None:
return "video", raw_video_match.group(1)
elif video_match is not None:
return "video", video_match.group(1)
elif playlist_match is not None:
return "playlist", playlist_match.group(1)
else:
return "search", query
async def get_video_from_id(self, video_id):
async with self.http_session.get("https://www.googleapis.com/youtube/v3/videos", params={"part": "snippet", "id": video_id, "fields": "items(id,snippet(channelTitle,description,thumbnails/high/url,title))", "key": self.api_key}) as video_response:
if video_response.status == 200:
video_data = await video_response.json()
ytdl_data = self.ytdl.process_video_result(self.extractor.extract(f"https://youtube.com/watch?v={video_id}"), download=False)
return YoutubeVideo(video_data["items"][0], ytdl_data)
else:
print(video_response.status, await video_response.text())
async def search_videos(self, search_query):
async with self.http_session.get("https://www.googleapis.com/youtube/v3/search", params={"part": "snippet", "order": "viewCount", "fields": "items(id(playlistId,videoId),snippet(channelTitle,title))", "type": "video", "q": search_query, "maxResults": 5, "key": self.api_key}) as search_response:
if search_response.status == 200:
search_data = await search_response.json()
return [{"id": video["id"]["videoId"], "title": video["snippet"]["title"], "channel": video["snippet"]["channelTitle"]} for video in search_data["items"]]
else:
print(search_response.status, await search_response.text())