本文整理汇总了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']