将 youtube_dl 基本嵌入到 Python 3.4 脚本中的问题
Issues with basic embedding of youtube_dl into a Python 3.4 script
我一直在修补 youtube_dl 并且在将它实施到我的 Python 3.4 脚本中时遇到了问题。
我只是想创建一个变量来存储输出(使用一些选项进行调整。)
但是,我似乎无法弄清楚如何向函数添加选项,而且无论我做什么,输出似乎都只被打印出来(而不是存储在我的变量中。)
这是我当前的代码:
class MyLogger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
ydl_opts = {
'logger': MyLogger(),
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
目前只下载一个测试视频。这是解释嵌入 youtube_dl:
的 GitHub 链接
这是我正在尝试做的伪代码:
class MyLogger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
ydl_opts = {
'logger': MyLogger(),
'InfoExtractors':[{'simulate','forceduration'}]
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
duration = ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
print('The duration is {0}'.format(duration))
有没有人有什么建议或想法?我被困在这个问题上的时间比我愿意承认的要长。
通过对 youtube_dl
源代码的简要了解,看起来如果不修改 youtube_dl
就无法实现您想做的事情。来自 the source:
def download(self, url_list):
"""Download a given list of URLs."""
outtmpl = self.params.get('outtmpl', DEFAULT_OUTTMPL)
if (len(url_list) > 1 and
'%' not in outtmpl and
self.params.get('max_downloads') != 1):
raise SameFileError(outtmpl)
for url in url_list:
try:
# It also downloads the videos
res = self.extract_info(
url, force_generic_extractor=self.params.get('force_generic_extractor', False))
except UnavailableVideoError:
self.report_error('unable to download video')
except MaxDownloadsReached:
self.to_screen('[info] Maximum number of downloaded files reached.')
raise
else:
if self.params.get('dump_single_json', False):
self.to_stdout(json.dumps(res))
return self._download_retcode
如您所见,它调用 self.to_screen
和 self.to_stdout
以及 return 代码以外的所有内容。您可以修补其中一个函数以重定向输出,但我认为否则不可能。
如果您确实想要修补 self.to_screen
,您应该可以这样做
警告:这可能会破坏某些东西
def patched_to_screen(self, message, skip_eol=False):
return message
def patch_to_stdout(self, message, skip_eol=False, check_quiet=False):
return message
ydl = YoutubeDL()
ydl.to_screen = patched_to_screen
tdl.to_stdout = patched_to_stdout
使用extract_info
方法,它returns一个字典with the video info:
import youtube_dl
class MyLogger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
ydl_opts = {
'logger': MyLogger(),
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info('http://www.youtube.com/watch?v=BaW_jenozKc', download=True)
print('The duration is {0}'.format(info['duration']))
我一直在修补 youtube_dl 并且在将它实施到我的 Python 3.4 脚本中时遇到了问题。
我只是想创建一个变量来存储输出(使用一些选项进行调整。)
但是,我似乎无法弄清楚如何向函数添加选项,而且无论我做什么,输出似乎都只被打印出来(而不是存储在我的变量中。)
这是我当前的代码:
class MyLogger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
ydl_opts = {
'logger': MyLogger(),
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
目前只下载一个测试视频。这是解释嵌入 youtube_dl:
的 GitHub 链接这是我正在尝试做的伪代码:
class MyLogger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
ydl_opts = {
'logger': MyLogger(),
'InfoExtractors':[{'simulate','forceduration'}]
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
duration = ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
print('The duration is {0}'.format(duration))
有没有人有什么建议或想法?我被困在这个问题上的时间比我愿意承认的要长。
通过对 youtube_dl
源代码的简要了解,看起来如果不修改 youtube_dl
就无法实现您想做的事情。来自 the source:
def download(self, url_list):
"""Download a given list of URLs."""
outtmpl = self.params.get('outtmpl', DEFAULT_OUTTMPL)
if (len(url_list) > 1 and
'%' not in outtmpl and
self.params.get('max_downloads') != 1):
raise SameFileError(outtmpl)
for url in url_list:
try:
# It also downloads the videos
res = self.extract_info(
url, force_generic_extractor=self.params.get('force_generic_extractor', False))
except UnavailableVideoError:
self.report_error('unable to download video')
except MaxDownloadsReached:
self.to_screen('[info] Maximum number of downloaded files reached.')
raise
else:
if self.params.get('dump_single_json', False):
self.to_stdout(json.dumps(res))
return self._download_retcode
如您所见,它调用 self.to_screen
和 self.to_stdout
以及 return 代码以外的所有内容。您可以修补其中一个函数以重定向输出,但我认为否则不可能。
如果您确实想要修补 self.to_screen
,您应该可以这样做
警告:这可能会破坏某些东西
def patched_to_screen(self, message, skip_eol=False):
return message
def patch_to_stdout(self, message, skip_eol=False, check_quiet=False):
return message
ydl = YoutubeDL()
ydl.to_screen = patched_to_screen
tdl.to_stdout = patched_to_stdout
使用extract_info
方法,它returns一个字典with the video info:
import youtube_dl
class MyLogger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
ydl_opts = {
'logger': MyLogger(),
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info('http://www.youtube.com/watch?v=BaW_jenozKc', download=True)
print('The duration is {0}'.format(info['duration']))