Search code examples
pythonerror-handlingruntime-error

why I get error-runtime with this python youtubedownloader code


How can I download video from YouTube is there some MWE?? I want to download a playlist from YouTube site: (I want to download a video from YouTube as follows) https://www.youtube.com/watch?v=_oHTzIsLMGc

With this code

from pytube import YouTube

#ask for the link from user
link = input("Enter the link of YouTube video you want to download:  ")
yt = YouTube(link)

#Showing details
print("Title: ",yt.title)
print("Number of views: ",yt.views)
print("Length of video: ",yt.length)
print("Rating of video: ",yt.rating)
#Getting the highest resolution possible
ys = yt.streams.get_highest_resolution()

#Starting download
print("Downloading...")
ys.download()
print("Download completed!!")

I'm getting this ERROR

$ python dl.py
Enter the link of YouTube video you want to download:  https://www.youtube.com/watch?v=_oHTzIsLMGc
Title:  CAER EN EL SUEÑO PROFUNDO,Sanación del Estrés,Ansiedad y Estados Depresivos,Restauración Corporal#16
Number of views:  34789
Length of video:  367200
Rating of video:  None
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/pytube/__main__.py", line 181, in fmt_streams
    extract.apply_signature(stream_manifest, self.vid_info, self.js)
  File "/usr/local/lib/python3.9/site-packages/pytube/extract.py", line 409, in apply_signature
    cipher = Cipher(js=js)
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 43, in __init__
    self.throttling_plan = get_throttling_plan(js)
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 405, in get_throttling_plan
    raw_code = get_throttling_function_code(js)
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 311, in get_throttling_function_code
    name = re.escape(get_throttling_function_name(js))
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 296, in get_throttling_function_name
    raise RegexMatchError(
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/cygdrive/c/Users/hynek0/Desktop/FU/VILLA/dl.py", line 13, in <module>
    ys = yt.streams.get_highest_resolution()
  File "/usr/local/lib/python3.9/site-packages/pytube/__main__.py", line 296, in streams
    return StreamQuery(self.fmt_streams)
  File "/usr/local/lib/python3.9/site-packages/pytube/__main__.py", line 188, in fmt_streams
    extract.apply_signature(stream_manifest, self.vid_info, self.js)
  File "/usr/local/lib/python3.9/site-packages/pytube/extract.py", line 409, in apply_signature
    cipher = Cipher(js=js)
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 43, in __init__
    self.throttling_plan = get_throttling_plan(js)
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 405, in get_throttling_plan
    raw_code = get_throttling_function_code(js)
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 311, in get_throttling_function_code
    name = re.escape(get_throttling_function_name(js))
  File "/usr/local/lib/python3.9/site-packages/pytube/cipher.py", line 296, in get_throttling_function_name
    raise RegexMatchError(
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple

EDIT line 264

$ pytube https://www.youtube.com/watch?v=_oHTzIsLMGc
Loading video...
Traceback (most recent call last):
  File "/usr/local/bin/pytube", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/site-packages/pytube/cli.py", line 53, in main
    _perform_args_on_youtube(youtube, args)
  File "/usr/local/lib/python3.9/site-packages/pytube/cli.py", line 60, in _perform_args_on_youtube
    download_highest_resolution_progressive(
  File "/usr/local/lib/python3.9/site-packages/pytube/cli.py", line 479, in download_highest_resolution_progressive
    _download(stream, target=target)
  File "/usr/local/lib/python3.9/site-packages/pytube/cli.py", line 256, in _download
    filesize_megabytes = stream.filesize // 1048576
  File "/usr/local/lib/python3.9/site-packages/pytube/streams.py", line 157, in filesize
    self._filesize = request.filesize(self.url)
  File "/usr/local/lib/python3.9/site-packages/pytube/request.py", line 204, in filesize
    return int(head(url)["content-length"])
  File "/usr/local/lib/python3.9/site-packages/pytube/request.py", line 268, in head
    response_headers = _execute_request(url, method="HEAD").info()
  File "/usr/local/lib/python3.9/site-packages/pytube/request.py", line 37, in _execute_request
    return urlopen(request, timeout=timeout)  # nosec
  File "/usr/lib/python3.9/urllib/request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.9/urllib/request.py", line 523, in open
    response = meth(req, response)
  File "/usr/lib/python3.9/urllib/request.py", line 632, in http_response
    response = self.parent.error(
  File "/usr/lib/python3.9/urllib/request.py", line 561, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.9/urllib/request.py", line 494, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.9/urllib/request.py", line 641, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 500: Internal Server Error

Solution

  • here is my code to download a video or a playlist:

    from pytube import YouTube, Playlist
    
    # Function to download a single video
    def download_video(url):
        try:
            yt = YouTube(url)
            stream = yt.streams.get_highest_resolution()
            print(f"Downloading video: {yt.title}")
            stream.download()
            print("Download completed!")
        except Exception as e:
            print(f"Error: {str(e)}")
    
    # Function to download a playlist
    def download_playlist(url):
        try:
            playlist = Playlist(url)
            print(f"Total videos in playlist: {len(playlist.video_urls)}")
            for video_url in playlist.video_urls:
                download_video(video_url)
        except Exception as e:
            print(f"Error: {str(e)}")
    
    # Main program
    user_input = input("Enter YouTube video URL or playlist URL: ")
    if "youtube.com/playlist" in user_input:
        download_playlist(user_input)
    else:
        download_video(user_input)
    

    output:

    PS D:\pgm\python\pratice> & C:/Python/Python_3.11.3/python.exe d:/pgm/python/pratice/test2.py
    Enter YouTube video URL or playlist URL: https://youtu.be/WuAAWQshbTM
    Downloading video: How to Do 15 PULLUPS OR MORE in a Row (GUARANTEED!)
    Download completed!
    PS D:\pgm\python\pratice> & C:/Python/Python_3.11.3/python.exe d:/pgm/python/pratice/test2.py
    Enter YouTube video URL or playlist URL: https://youtube.com/playlist?list=PLjqueTFOWIlKOKqeRMB7K-64iGresYwCn
    Total videos in playlist: 2
    Downloading video: 30 Minute Full Body Dumbbell HIIT Workout [ADVANCED]
    Download completed!
    Downloading video: Ultimate Full-Body Dumbbell Workout | Andy Speer
    Download completed!