youtube-dl,作为子进程调用,link 无法识别

youtube-dl, calling as subprocess, link not recognized

所以,我正在编写一个基本的 python 脚本来使用 youtube-dl 从视频中下载高质量的缩略图。使用命令行 youtube-dl,你可以 运行 "youtube-dl --list-thumbnails [LINK]" 它会输出不同质量的列表 links 到缩略图图片。通常最高分辨率的 link 中有 'maxresdefault'。我希望能够使用 wget 从命令行下载此图像。这是我到目前为止实现它的代码。我不熟悉正则表达式,但根据此站点:regexr.com,它应该在 link 中与 'maxresdefault'.

匹配
import subprocess
import sys
import re
youtubeoutput = subprocess.call(['youtube-dl', '--list-thumbnails', 'https://www.youtube.com/watch?v=t2U2mUtTnzY'], shell=True, stdout=subprocess.PIPE)
print(str(youtubeoutput))
imgurl = re.search("/maxresdefault/g", str(youtubeoutput)).group(0)
print(imgurl)
subprocess.run('wget', str(imgurl))

我把打印语句放在那里看看输出是什么。当我 运行 代码时,我可以看到 youtube-dl 无法识别其中的 link。 youtube-dl: error: You must provide at least one url。由于输出中没有 links,re.search 变成了 NoneType,它给了我一个错误。我不知道为什么 youtube-dl 无法识别 link。我什至不确定它是否识别 --list-thumnails。有人可以帮忙吗?

您已要求 subprocess 使用 shell (shell=True),因此您通常会将整个命令传递给 call,如下所示:

youtubeoutput = subprocess.call("youtube-dl --list-thumbnails https://www.youtube.com/watch?v=t2U2mUtTnzY", shell=True, stdout=subprocess.PIPE)

但实际上,您可能不需要 shell。尝试类似的东西:

youtubeoutput = subprocess.check_output(['youtube-dl', '--list-thumbnails', 'https://www.youtube.com/watch?v=t2U2mUtTnzY'])

注意 call 实际上并不是 return 程序的标准输出; check_output 确实如此。

Reference