当前位置: 首页>>代码示例>>Python>>正文


Python YoutubeDL.process_video_result方法代码示例

本文整理汇总了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())
开发者ID:TickerOfTime,项目名称:snake,代码行数:73,代码来源:api.py


注:本文中的youtube_dl.YoutubeDL.process_video_result方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。