youtube-dl taking only first letter of URL?
Asked Answered
S

2

8

StackOverflow!

I am having some problems with youtube-dl. I had this working recently, but I made some changes to it, and it now refuses to work. Here is my code:

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")

As far as I can see, it appears that my script is taking the first letter out of my URL. Does anyone know why? As a "Side-quest", does anyone know how to take a search instead of a URL?

Here is my output:

[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.
Swiss answered 23/9, 2017 at 18:29 Comment(0)
M
18

As documented, the ydl.download function takes a list of URLs. So instead of

ydl.download(url)

you want to call

ydl.download([url])

To run a search, first, look up the keyword by running youtube-dl --extractor-descriptions | grep search. For instance, the keyword for Soundcloud search is scsearch and the keyword for YouTube default search is ytsearch.

Then, simply pass the keyword and search terms, separated by a colon (:) as the URL.

For example, a URL of ytsearch:fluffy bunnies will find you the top video of fluffy bunnies on YouTube with the default search criteria.

Marsiella answered 23/9, 2017 at 18:58 Comment(0)
H
1

Here is the sample :

sound_list = []
# bike sound
sound_list.append('https://www.youtube.com/watch?v=sRdRwHPjJPk')
# car sound
sound_list.append('https://www.youtube.com/watch?v=PPdNb-XQXR8')

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(sound_list)
Haulage answered 6/2, 2020 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.