首页 > 解决方案 > 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

起初以为这是版权问题,但它也不适用于一些我确定没有版权的网址

标签: pythonpycharmyoutube-dl

解决方案


我找到了解决它的方法,但感觉我做错了,我改变了这一行:

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 库像这样工作,如果有人知道如何正确解决这个问题,我将不胜感激


推荐阅读