python - youtube-dl 在 pycharm 中无法按预期工作
问题描述
我已经制作了这段代码来仅从 youtube 上的 url 下载音频,但某些链接不起作用
video_url = "https://www.youtube.com/watch?v=5yijULf4Hko" # doesn't work for example
#video_url = "https://www.youtube.com/watch?v=gKK3Y6iDk_Q" # does work
filename = "song.mp3"
options = {
'format': 'bestaudio/best',
'keepvideo': False,
'outtmpl': filename,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '251',
}],
}
with youtube_dl.YoutubeDL(options) as ydl:
ydl.download([video_url])
当我尝试使用第一个链接时,程序输出:
[youtube] 5yijULf4Hko: Downloading webpage
ERROR: No video formats found; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see https://yt-dl.org/update on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.
Traceback (most recent call last):
File "C:\Python\Python38\lib\site-packages\youtube_dl\YoutubeDL.py", line 797, in extract_info
ie_result = ie.extract(url)
File "C:\Python\Python38\lib\site-packages\youtube_dl\extractor\common.py", line 530, in extract
ie_result = self._real_extract(url)
File "C:\Python\Python38\lib\site-packages\youtube_dl\extractor\youtube.py", line 2364, in _real_extract
self._sort_formats(formats)
File "C:\Python\Python38\lib\site-packages\youtube_dl\extractor\common.py", line 1327, in _sort_formats
raise ExtractorError('No video formats found')
youtube_dl.utils.ExtractorError: No video formats found; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see https://yt-dl.org/update on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/Scripts/Python/a.py", line 20, in <module>
ydl.download([video_url])
File "C:\Python\Python38\lib\site-packages\youtube_dl\YoutubeDL.py", line 2018, in download
res = self.extract_info(
File "C:\Python\Python38\lib\site-packages\youtube_dl\YoutubeDL.py", line 820, in extract_info
self.report_error(compat_str(e), e.format_traceback())
File "C:\Python\Python38\lib\site-packages\youtube_dl\YoutubeDL.py", line 625, in report_error
self.trouble(error_message, tb)
File "C:\Python\Python38\lib\site-packages\youtube_dl\YoutubeDL.py", line 595, in trouble
raise DownloadError(message, exc_info)
youtube_dl.utils.DownloadError: ERROR: No video formats found; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see https://yt-dl.org/update on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.
Process finished with exit code 1
我正在使用最新版本的 pycharm (2020.2.1) 已经尝试更新 youtube-dl 我尝试在命令提示符下下载并成功下载了音频文件
youtube-dl -f 251 https://www.youtube.com/watch?v=5yijULf4Hko
起初以为这是版权问题,但它也不适用于一些我确定没有版权的网址
解决方案
我找到了解决它的方法,但感觉我做错了,我改变了这一行:
with youtube_dl.YoutubeDL(options) as ydl:
ydl.download([video_url])
至
import os
try:
with youtube_dl.YoutubeDL(options) as ydl:
ydl.download([video_url])
except:
os.system(f"""youtube-dl -o "song.%(ext)s" --extract-audio -x --audio-format mp3 {video_url}""")
我只是使用了在程序的命令提示符下工作的命令,但我无法让 python 库像这样工作,如果有人知道如何正确解决这个问题,我将不胜感激
推荐阅读
- c++ - 为什么 g++ 不优化本地 constexpr 数组访问?
- android - deleteAllFromRealm(RealmList : 此方法仅在托管模式下可用
- r - 闪亮的服务器/UI 文件无法呈现所需的输出 - 日志没有帮助
- python - 规范化深度嵌套列表Python中的所有元素
- spring - 如何获取 Spring 初始化错误列表?
- django - 如何在 sql-query 末尾向所有查询集添加过滤器?
- javascript - 如何在 ReactJs 中将语义 ui 图标颜色更改为定义的颜色?
- java - log4j 的记录器变量始终为空
- nfc - 在 Desfire EV1 卡中创建标准数据文件时,出现 0x0E 错误
- javascript - html,css,隐藏表格列的边框但保留内容