我想从python执行一个命令。这是原始命令:
yt-dlp -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best --downloader ffmpeg --downloader-args "ffmpeg_i:-ss 00:19:10.00 -to 00:19:40.00" --no-check-certificate https://youtu.be/YXfnjrbmKiQ
所以我要这么做:
import subprocess
result = subprocess.call(['yt-dlp','-f','bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best','--downloader','ffmpeg','--downloader-args','"ffmpeg_i:-ss 00:00:10.00 -to 00:00:40.00"','--no-check-certificate','https://youtu.be/YXfnjrbmKiQ'])
print(result)
但它给了我一个错误:
ffmpeg_i:-ss 00:00:10.00 -to 00:00:40.00: Invalid argument
我怎么才能修好它?
发布于 2022-08-25 18:32:51
在不使用shell=True
的情况下运行时,元素'"ffmpeg_i:-ss 00:00:10.00 -to 00:00:40.00"'
中不需要" "
,因为通常只有shell
才需要它来识别哪个元素保持为一个字符串,但后来shell
将这个字符串发送到没有" "
的系统中。
import subprocess
result = subprocess.call([
'yt-dlp',
'-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best'
'--downloader', 'ffmpeg'
'--downloader-args', 'ffmpeg_i:-ss 00:19:10.00 -to 00:19:40.00'
'--no-check-certificate',
'https://youtu.be/YXfnjrbmKiQ'
])
print(result)
或者您可以使用shell=True
运行它,然后需要单个字符串,并且必须使用" "
。
import subprocess
result = subprocess.call(
'yt-dlp -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best --downloader ffmpeg --downloader-args "ffmpeg_i:-ss 00:19:10.00 -to 00:19:40.00" --no-check-certificate https://youtu.be/YXfnjrbmKiQ',
shell=True
)
print(result)
BTW:
有标准模块shlex
,它可以将带有命令的字符串转换为参数列表。
import shlex
cmd = 'yt-dlp -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best --downloader ffmpeg --downloader-args "ffmpeg_i:-ss 00:19:10.00 -to 00:19:40.00" --no-check-certificate https://youtu.be/YXfnjrbmKiQ'
cmd = shlex.split(cmd)
print(cmd)
结果显示该元素没有" "
。
['yt-dlp',
'-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
'--downloader', 'ffmpeg',
'--downloader-args', 'ffmpeg_i:-ss 00:19:10.00 -to 00:19:40.00',
'--no-check-certificate', 'https://youtu.be/YXfnjrbmKiQ']
https://stackoverflow.com/questions/73488730
复制相似问题