Python youtube dl不';在pycharm中无法按预期工作

Python youtube dl不';在pycharm中无法按预期工作,python,pycharm,youtube-dl,Python,Pycharm,Youtube Dl,我编写这段代码只是为了从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"

我编写这段代码只是为了从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
[youtube]5yijULf4Hko:下载网页
错误:未找到任何视频格式;请将此问题报告给我们https://yt-dl.org/bug . 确保您使用的是最新版本;看见https://yt-dl.org/update  关于如何更新。确保使用--verbose标志调用youtube dl并包含其完整输出。
回溯(最近一次呼叫最后一次):
文件“C:\Python\Python38\lib\site packages\youtube\u dl\YoutubeDL.py”,第797行,摘录信息
ie_result=ie.extract(url)
文件“C:\Python\Python38\lib\site packages\youtube\u dl\extractor\common.py”,第530行,摘录
ie_result=self._real_extract(url)
文件“C:\Python\Python38\lib\site packages\youtube\u dl\extractor\youtube.py”,第2364行,在\u real\u extract中
self.\u排序\u格式(格式)
文件“C:\Python\Python38\lib\site packages\youtube\u dl\extractor\common.py”,第1327行,格式为
raise EXTRACTOR错误('未找到视频格式')
youtube_dl.utils.ExtractorError:未找到任何视频格式;请将此问题报告给我们https://yt-dl.org/bug . 确保您使用的是最新版本;看见https://yt-dl.org/update  关于如何更新。确保使用--verbose标志调用youtube dl并包含其完整输出。
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“D:/Scripts/Python/a.py”,第20行,在
下载([video_url])
文件“C:\Python\Python38\lib\site packages\youtube\u dl\YoutubeDL.py”,第2018行,下载
res=self.extract\u info(
文件“C:\Python\Python38\lib\site packages\youtube\u dl\YoutubeDL.py”,第820行,摘录信息
self.report\u错误(compat\u str(e),e.format\u traceback())
报告错误中的文件“C:\Python\Python38\lib\site packages\youtube\u dl\YoutubeDL.py”,第625行
自身故障(错误消息,tb)
文件“C:\Python\Python38\lib\site packages\youtube\u dl\YoutubeDL.py”第595行出现问题
引发下载错误(消息,exc_信息)
youtube_dl.utils.DownloadError:错误:未找到任何视频格式;请在上报告此问题https://yt-dl.org/bug 。请确保您使用的是最新版本;请参阅https://yt-dl.org/update  请确保使用--verbose标志调用youtube dl并包含其完整输出。
进程已完成,退出代码为1
我正在使用pycharm的最新版本(2020.2.1) 已尝试更新youtube dl 我尝试在命令提示符下下载,它成功加载了音频文件 youtube dl-f 251https://www.youtube.com/watch?v=5yijULf4Hko


起初我认为这是版权方面的问题,但对于一些我确信不受版权保护的URL也不起作用

我找到了一种解决方法,但感觉我做得非常错误, 我改了台词:

with youtube_dl.YoutubeDL(options) as ydl:
    ydl.download([video_url])


我只是在程序的命令提示符下使用了这个命令,但我无法让python库像这样工作,如果有人知道如何正确地解决这个问题,我将不胜感激

这是我编辑的一个简单脚本,它适合我,但你们可以试试

    @bp.route("/down", methods=['GET', 'POST'])
    def download_file():
    video_info = youtube_dl.YoutubeDL().extract_info(
    url=video_url, download=False
     )
    filename = f"{video_info['title']}.mp3"
    ydl_opts = {
            'format': 'bestaudio/best',
            'keepvideo': False,
            'outtmpl': filename,
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
            }],
        }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        info_dict = ydl.extract_info(video_url, download=False)
        video_title = info_dict.get('title', None)

    path = f'C:\\{video_title}.mp3'

    ydl_opts.update({'outtmpl':path})

    try:
        with youtube_dl.YoutubeDL(options) as ydl:
            ydl.download([video_url])
    except:
        os.system(f"""youtube-dl -o "{filename}.%(ext)s" --extract-audio -x --audio-format mp3 {video_url}""")

        return index()
html代码

    <form action="/down" method="post">
    <button>
    <a type="submit"><i class="fas fa-cloud-download-alt" style="color: white;"> MP3</i></a>
    </button> 
    </form>

MP3

对于损坏的vid,内容似乎不同,库无法处理。我建议您提出错误消息中所述的问题。
    <form action="/down" method="post">
    <button>
    <a type="submit"><i class="fas fa-cloud-download-alt" style="color: white;"> MP3</i></a>
    </button> 
    </form>