如何清除YouTube视频下载器中的“密码”错误并以mp3格式下载视频?

问题描述

我正在尝试使用pytube和tkinter在python中创建一个 YouTube视频下载器。大部分视频都可以正常下载,但在某些视频中,我收到的错误代码密码。发生这种情况时,有人可以帮忙吗?

我也尝试以mp3格式下载,但是将'audioonly'属性设置为True会以mp4格式下载我的文件,而没有视频。但我想要mp3中的格式。我该怎么办?

这是我的代码

def startDownload(url):
    global file_size
    path_to_save = askdirectory()
    if path_to_save is None:
        return

    try:
            global MaxFileSize,fileSizeInBytes
            choice = youtubeChoicesLabel.get()
            url=urlField.get()
            yt = YouTube(url)
            nome = yt.title
            # video2 = urlField.get()

            if (choice == download_choices[1]):
                print("720p video is downloading")
                selectVideo = yt.streams.filter(progressive=True,file_extension='mp4').first()

            elif (choice == download_choices[2]):
                print("Audio file is downloading")
                
                # selectVideo =yt.streams.get_audio_only()
                # new_filename= nome + '*.mp3'
                # default_filename=  nome + '*.mp4'
                # ffmpeg = ('ffmpeg -i ' %path_to_save %default_filename + new_filename)
                # subprocess.run(ffmpeg,shell=True)
                selectVideo = yt.streams.filter(only_audio=True).first()
                
                
            elif (choice == download_choices[0]):
                return

            
                fileSizeInBytes = selectVideo.filesize
                MaxFileSize = fileSizeInBytes/1024000
                MB =str(MaxFileSize)+ "MB"
                print("File Size = : {:00.000f}".format (MaxFileSize))

            
            

            
            # yt = YouTube(url)
            # st=yt.streams.first()
            st= selectVideo
            yt.register_on_complete_callback(complete_download)
            yt.register_on_progress_callback(progress_download)
            file_size=st.filesize
            st.download(output_path=path_to_save)
        




    except Exception as e:
        print(e)

解决方法

修复非常简单,它位于GitHub存储库中,但是由于出现了此问题,因此我将对其进行修复。这不是由于文件的工作方式引起的,而是因为YouTube将搜索方式或从密码更改为signatureCipher之类的问题而发生的。

  • 首先,在终端中说出pip install pytube3

  • 第二,转到路径-C:\Users\<user>\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\pytube。请记住,路径会有所不同,因此将<user>替换为Windows用户名,并将python37-32替换为安装pytube的Python版本。

  • 第三,从上面导航的目录中打开extract.py,然后转到301行或搜索以下行:

cipher_url = [
    parse_qs(formats[i]["cipher"]) for i,data in enumerate(formats)
]
  • 现在将其替换为:
cipher_url = [
     parse_qs(formats[i]["signatureCipher"]) for i,data in enumerate(formats)
]

如果在更改文件时损坏了该文件,只需说pip uninstall pytube3,然后再次安装它,然后重复。请记住,您的导入内容仍为import pytube