I am trying to create a YouTube video downloader in python using pytube and tkinter. Most of the videos are getting downloaded fine but in some videos I am getting the error as 'cipher'. Can anyone help while this is happening?
Also I am trying to download in mp3 format but setting the 'audioonly' attribute to True downloads my file in mp4 format without the video. But I want the format in mp3. How can I do so?
Here is my code
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)
The fix is pretty simple, it's there in the GitHub repo, but since this question came up here, this is how I fixed it. It's not due to the way your file works, it's a problem that happened because YouTube changed their search patterns or something from cipher to signatureCipher.
First, say pip install pytube3
in the terminal.
Second, go to the path - C:\Users\<user>\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\pytube
. Keep in mind the path will vary so replace <user>
with your Windows username and python37-32
with your Python version that you install pytube on.
Third, open extract.py
from the above navigated directory and head onto line 301 or search for the line:
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)
]
In case you damage this file while making changes to it, just say pip uninstall pytube3
and then install it again, and repeat. Keep in mind your imports will still be import pytube
.