youtube-dl仅采用URL的首字母吗?

Gam*_*ude 2 youtube url python-3.x youtube-dl

堆栈溢出!

我在youtube-dl上遇到一些问题。我最近有这个工作,但是我做了一些修改,现在它拒绝工作。这是我的代码:

import youtube_dl
import os

class InvalidURL(Exception):
    pass

class SongExists(Exception):
    pass

def download(url):
    try:
        options = {
            'format': 'bestaudio/best',
            'quiet': False,
            'extractaudio': True,  # only keep the audio
            'audioformat': "wav",  # convert to wav
            'outtmpl': '%(id)s.wav',  # name the file the ID of the video
            'noplaylist': True,  # only download single song, not playlist
        }
        with youtube_dl.YoutubeDL(options) as ydl:
            r = ydl.extract_info(url ,download=False)
            if os.path.isfile(str(r["id"])):
                raise SongExists('This song has already been requested.')
            print("Downloading", r["title"])
            print(str(url))
            ydl.download(url)
            print("Downloaded", r["title"])
            return r["title"], r["id"]
    except youtube_dl.utils.DownloadError:
        raise InvalidURL('This URL is invalid.')

if __name__ == "__main__":
    download("https://www.youtube.com/watch?v=nvHyII4Dq-A")
Run Code Online (Sandbox Code Playgroud)

据我所见,看来我的脚本正在从我的URL中取出第一个字母。有人知道为什么吗?作为“旁观者”,有人知道如何进行搜索而不是使用URL?

这是我的输出:

[youtube] nvHyII4Dq-A: Downloading webpage
[youtube] nvHyII4Dq-A: Downloading video info webpage
[youtube] nvHyII4Dq-A: Extracting video information
WARNING: unable to extract uploader nickname
[youtube] nvHyII4Dq-A: Downloading MPD manifest
Downloading MagnusTheMagnus - Area
https://www.youtube.com/watch?v=nvHyII4Dq-A
ERROR: 'h' is not a valid URL. Set --default-search "ytsearch" (or run  youtube-dl "ytsearch:h" ) to search YouTube
Traceback (most recent call last):
  File "C:\Users\tyler\AppData\Local\Programs\Python\Python36-32\lib\site-packages\youtube_dl\YoutubeDL.py", line 776, in extract_info
    ie_result = ie.extract(url)
  File "C:\Users\tyler\AppData\Local\Programs\Python\Python36-32\lib\site-packages\youtube_dl\extractor\common.py", line 433, in extract
    ie_result = self._real_extract(url)
  File "C:\Users\tyler\AppData\Local\Programs\Python\Python36-32\lib\site-packages\youtube_dl\extractor\generic.py", line 1993, in _real_extract
    % (url, url), expected=True)
youtube_dl.utils.ExtractorError: 'h' is not a valid URL. Set --default-search "ytsearch" (or run  youtube-dl "ytsearch:h" ) to search YouTube

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/tyler/PycharmProjects/PythonSpeakerThing/downloader.py", line 26, in download
    ydl.download(url)
  File "C:\Users\tyler\AppData\Local\Programs\Python\Python36-32\lib\site-packages\youtube_dl\YoutubeDL.py", line 1958, in download
    url, force_generic_extractor=self.params.get('force_generic_extractor', False))
  File "C:\Users\tyler\AppData\Local\Programs\Python\Python36-32\lib\site-packages\youtube_dl\YoutubeDL.py", line 799, in extract_info
    self.report_error(compat_str(e), e.format_traceback())
  File "C:\Users\tyler\AppData\Local\Programs\Python\Python36-32\lib\site-packages\youtube_dl\YoutubeDL.py", line 604, in report_error
    self.trouble(error_message, tb)
  File "C:\Users\tyler\AppData\Local\Programs\Python\Python36-32\lib\site-packages\youtube_dl\YoutubeDL.py", line 574, in trouble
    raise DownloadError(message, exc_info)
youtube_dl.utils.DownloadError: ERROR: 'h' is not a valid URL. Set --default-search "ytsearch" (or run  youtube-dl "ytsearch:h" ) to search YouTube

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/tyler/PycharmProjects/PythonSpeakerThing/downloader.py", line 33, in <module>
    download("https://www.youtube.com/watch?v=nvHyII4Dq-A")
  File "C:/Users/tyler/PycharmProjects/PythonSpeakerThing/downloader.py", line 30, in download
    raise InvalidURL('This URL is invalid.')
__main__.InvalidURL: This URL is invalid.
Run Code Online (Sandbox Code Playgroud)

phi*_*hag 7

作为记载,该ydl.download功能需要一个列表的URL。所以代替

ydl.download(url)
Run Code Online (Sandbox Code Playgroud)

你想打电话

ydl.download([url])
Run Code Online (Sandbox Code Playgroud)

要运行搜索,首先,通过运行查找关键字youtube-dl --extractor-descriptions | grep search。例如,Soundcloud搜索scsearch的关键字为,而YouTube默认搜索的关键字为ytsearch

然后,只需传递关键字和搜索词,并用冒号(:)作为URL。

例如,URL为,ytsearch:fluffy bunnies将使用默认搜索条件为您找到YouTube上蓬松兔子的顶级视频。