Why my YouTube video downloader only downloads some videos and for other videos it shows keyerror like URL and cipher?

Deadly 提交于 2020-06-23 03:31:41

问题


I am trying to make a YouTube video downloader using Python pytube3 but it doesn't download all the videos. Some videos download very easily but some videos won't download and instead of download it shows error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\extract.py", line 297, in apply_descrambler
    for format_item in formats
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\extract.py", line 297, in <listcomp>
    for format_item in formats
KeyError: 'url'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\tarun\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/tarun/PycharmProjects/YTDownloader/YTD.py", line 15, in video_download
    my_video = YouTube(input_user)
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\__main__.py", line 92, in __init__
    self.descramble()
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\__main__.py", line 132, in descramble
    apply_descrambler(self.player_config_args, fmt)
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\extract.py", line 301, in apply_descrambler
    parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\extract.py", line 301, in <listcomp>
    parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
KeyError: 'cipher'

回答1:


This is an error in the file extract.py from pytube.

  1. Go to the location where the package was installed. If you don't know where, run the command

    pip show pytube3
    

    And it'll give you something like this:

We can see Location: c:\users\tiago\anaconda3\lib\site-packages.

  1. Go to that location, open the folder pytube and the file extract.py

  1. In the file, line no. 306 or 301, you will find parse_qs(formats[i]["cipher"]). If yes, then change "cipher" to "signatureCipher" (make sure 'C' is capital).

    So, you'll initially have

    cipher_url = [
                    parse_qs(formats[i]["signatureCipher"]) for i, data in enumerate(formats)
                ]
    

    but it should be

    cipher_url = [
                    parse_qs(formats[i]["signatureCipher"]) for i, data in enumerate(formats)
                ]
    

  1. Run the following script to see it working

    # -*- coding: utf-8 -*-
    """
    Created on Mon Jun 15 12:21:49 2020
    
    @author: tiago
    """
    from pytube import YouTube
    
    video_url = "https://youtu.be/gp5tziO5lXg" # YouTube video URL
    youtube = YouTube(video_url)
    video = youtube.streams.first()
    video.download("C:/Users/tiago/Desktop/videos/") # Path where to store the video
    

You'll then see the video downloaded in that folder




回答2:


This is a problem with pytube3, I believe as of now they have not submitted a fix yet. Here is the link to the issue on github



来源:https://stackoverflow.com/questions/62098925/why-my-youtube-video-downloader-only-downloads-some-videos-and-for-other-videos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!