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


Python YoutubeDL.build_format_selector方法代码示例

本文整理汇总了Python中youtube_dl.YoutubeDL.build_format_selector方法的典型用法代码示例。如果您正苦于以下问题:Python YoutubeDL.build_format_selector方法的具体用法?Python YoutubeDL.build_format_selector怎么用?Python YoutubeDL.build_format_selector使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在youtube_dl.YoutubeDL的用法示例。


在下文中一共展示了YoutubeDL.build_format_selector方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_media_stream_url

# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import build_format_selector [as 别名]
def get_media_stream_url(media_url):
    """Extract stream URL from the media URL."""
    from youtube_dl import YoutubeDL
    from youtube_dl.utils import DownloadError, ExtractorError

    ydl = YoutubeDL({'quiet': True, 'logger': _LOGGER})

    try:
        all_media_streams = ydl.extract_info(media_url, process=False)
    except DownloadError:
        # This exception will be logged by youtube-dl itself
        raise YDException()

    if 'entries' in all_media_streams:
        _LOGGER.warning("Playlists are not supported, "
                        "looking for the first video")
        try:
            selected_stream = next(all_media_streams['entries'])
        except StopIteration:
            _LOGGER.error("Playlist is empty")
            raise YDException()
    else:
        selected_stream = all_media_streams

    try:
        media_info = ydl.process_ie_result(selected_stream, download=False)
    except (ExtractorError, DownloadError):
        # This exception will be logged by youtube-dl itself
        raise YDException()

    format_selector = ydl.build_format_selector('best')

    try:
        best_quality_stream = next(format_selector(media_info))
    except (KeyError, StopIteration):
        best_quality_stream = media_info

    return best_quality_stream['url']
开发者ID:Khabi,项目名称:home-assistant,代码行数:40,代码来源:media_extractor.py


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