gpt4 book ai didi

python - 将 youtube_dl 基本嵌入到 Python 3.4 脚本中的问题

转载 作者:行者123 更新时间:2023-12-01 04:07:50 25 4
gpt4 key购买 nike

我一直在修改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))

有人有什么建议或想法吗?我在这个问题上陷入困境的时间比我愿意承认的还要长。

最佳答案

使用extract_info方法,它返回一个字典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']))

关于python - 将 youtube_dl 基本嵌入到 Python 3.4 脚本中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35342425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com