python - 使用 python youtube_dl 从 youtube 下载 .mp3/webm 格式的音乐/视频的问题
问题描述
所以,这不是我在 youtube 教程上找到的代码(我现在找不到)。
from __future__ import unicode_literals
import youtube_dl
import os
from sys import argv
# Download data and config
download_options = {
'format': 'bestaudio/best',
'outtmpl': '%(title)s.%(ext)s',
'nocheckcertificate': True,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
# Song Directory
if not os.path.exists('Songs'):
os.mkdir('Songs')
else:
os.chdir('Songs')
# Download Songs
with youtube_dl.YoutubeDL(download_options) as dl:
with open('..\\' + argv[1], 'r') as f:
for song_url in f:
dl.download([song_url])
它首先下载“.webm”文件(视频),然后正确转换为 mp3,然后删除原始“.webm”视频,我不希望它删除它,当我在 CMD 中运行它时显示“删除原始文件 .webm (传递 -k 保留),但我不知道在哪里传递 -k (我不是程序员,我只是复制了代码)
提前致谢!
解决方案
你需要传递一个额外的参数
这是从 youtube_dl github 复制的
--postprocessor-args ARGS Give these arguments to the postprocessor
-k, --keep-video Keep the video file on disk after the post-
processing; the video is erased by default
你可以在这里查看如何传递选项
我已经测试过这个语法,它对我有用(Darude Sandstorm)
download_options = {
'format': 'bestaudio/best',
'outtmpl': '%(title)s.%(ext)s',
'nocheckcertificate': True,
'keepvideo': True,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'
}]
}
推荐阅读
- xml - XSLT:不允许将多个项目的序列作为 fn:substring-after() 的第一个参数
- java - 由于处理方法不正确,Spring IntegrationFlow 失败
- html - 如何从 iPhone X 和 iPad 版本中删除页脚下的空白?
- microsoft-teams - cookie 是否在选项卡之间共享,例如 Microsoft Teams 中的机器人?
- django - Django:为电子商务应用程序中的每个产品创建独特的表单元素集合
- c# - 如何创建具有嵌入式字段的结构?
- java - 通过 JLabel 添加为 jpg 格式的图像在 java swing gui 中不清楚
- azure-devops - 设置分支文件夹而不是特定分支的权限
- ubuntu - 如何为 tesseract 创建 .deb 包?
- javascript - 视频在 Safari 中不可见