【问题标题】:youtube-dl not accepting playlist urlyoutube-dl 不接受播放列表网址
【发布时间】:2017-09-04 22:09:25
【问题描述】:
使用此代码
import os
with open('urls.txt') as f:
for line in f:
os.system("youtube-dl "+"--write-thumbnail "+"--skip-download "+"--yes-playlist " +line)
播放列表中的第一张图片下载后,我收到一条错误消息,提示“列表”未被识别为内部或外部命令、可运行程序或批处理文件。在“urls.txt”中,我只有一个 Youtube 播放列表的 url。网址是这样的:
https://www.youtube.com/watch?v=GA3St3Rf9Gs&list=PL-uc0GihCvU9s24BT_mvTzt3zm7e2uDGm
它在 & 符号后切断输入。如果我用 'foo' 替换 url 中的 'list',我会收到相同的消息。我该怎么做才能让 youtube-dl 接受播放列表 URL?
【问题讨论】:
-
尝试查看this。也尝试更新youtube-dl。
标签:
python
youtube
youtube-dl
【解决方案1】:
您的程序有一个主要的command injection security vulnerability。你偶然触发了这个(使用无害的代码)。你正在执行
youtube-dl --write-thumbnail --skip-download --yes-playlist \
https://www.youtube.com/watch?v=GA3St3Rf9Gs&list=PL-uc0GihCvU9s24BT_mvTzt3zm7e2uDGm
由于与号是command character in shell scripts,因此您正在运行两个命令
youtube-dl --write-thumbnail --skip-download --yes-playlist \
https://www.youtube.com/watch?v=GA3St3Rf9Gs
和
list=PL-uc0GihCvU9s24BT_mvTzt3zm7e2uDGm
由于没有该名称的程序,第二个命令可能会失败。
要解决此问题,请使用带有subprocess 的正确子进程调用:
import subprocess
with open('urls.txt') as f:
for line in f:
subprocess.check_call([
"youtube-dl",
"--write-thumbnail", "--skip-download", "--yes-playlist",
line])
【解决方案2】:
您可以直接在脚本中使用youtube_dl 库并传递要从中下载的网址。
import os
import youtube_dl
ydl_opts = {
'writethumbnail': True,
'skip_download': True,
'noplaylist': False
}
with open('urls.txt') as f:
sources = f.readlines()
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(sources)
【解决方案3】:
在 commandprompt 中,您只需要引用 url(带 & 符号)。您可以尝试在 python 中以相同的方式转义 url(使用 & 号)。参考youtube-dl FAQ