Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

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

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:UsersarunPycharmProjectsYTDownloadervenvlibsite-packagespytubeextract.py", line 297, in apply_descrambler
    for format_item in formats
  File "C:UsersarunPycharmProjectsYTDownloadervenvlibsite-packagespytubeextract.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:UsersarunAppDataLocalProgramsPythonPython37-32libkinter\__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:UsersarunPycharmProjectsYTDownloadervenvlibsite-packagespytube\__main__.py", line 92, in __init__
    self.descramble()
  File "C:UsersarunPycharmProjectsYTDownloadervenvlibsite-packagespytube\__main__.py", line 132, in descramble
    apply_descrambler(self.player_config_args, fmt)
  File "C:UsersarunPycharmProjectsYTDownloadervenvlibsite-packagespytubeextract.py", line 301, in apply_descrambler
    parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
  File "C:UsersarunPycharmProjectsYTDownloadervenvlibsite-packagespytubeextract.py", line 301, in <listcomp>
    parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
KeyError: 'cipher'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

pip show package

We can see Location: c:usersiagoanaconda3libsite-packages.

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

pythube location

  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]["cipher"]) for i, data in enumerate(formats)
                 ]
    

    but it should be

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

pytube extract cypher error fixed

  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

How to download video from YouTube in Python


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...