Youtube-DL 搜索 Return URL 视频

Youtube-DL Search Return URL of Video

所以我想用 youtube-dl 的 ytsearch 功能搜索 Youtube 视频,并给出找到的视频的 url 并将其存储在一个变量中。在 Python 中是否有这样做的选项?

例如,我如何从下面的代码中获取 url?

async def play(ctx):
args = ctx.message.content.split(" ")
os.system('youtube-dl --get-url "ytsearch:{}"'.format(args[1]))

我不确定这是否有效,但如果您是 运行 windows,您可以在 os 参数的末尾添加 > filename.txt cmd 命令的输出。

async def play(ctx):
args = ctx.message.content.split(" ")
os.system('youtube-dl --get-url "ytsearch:{}"'.format(args[1])+ "> filename.txt")

然后您可以读取文件并将结果存储在变量中,方法是写入...

file = open("filename.txt","r")
output = file.read()
file.close()

我假设命令的输出是 url?