This code works perfectly, but I don't want the lowest quality. I want the highest one. When I tried changing the video = youtube. streams.first()
to video = youtube.streams.last()
I encountered a problem where the video that I downloaded was just a black screen with the audio playing in the background.
from tkinter import *import pytube# Functionsdef download():video_url = url.get()try:youtube = pytube.YouTube(video_url)video = youtube.streams.first()video.download("C:/Users/iwanh/Desktop/MP4_MP3s")notif.config(fg="green", text="Download complete")except Exception as e:print(e)notif.config(fg="red", text="Video could not be downloaded")# Main Screenmaster = Tk()master.title("Youtube Video Downloader")# LabelsLabel(master, text="Youtube Video Converter", fg="red", font=("Calibri", 15)).grid(sticky=N, padx=100, row=0)Label(master, text="Please enter the link to your video below : ", font=("Calibri", 15)).grid(sticky=N, row=1, pady=15)notif = Label(master, font=("Calibri", 12))notif.grid(sticky=N, pady=1, row=4)# Varsurl = StringVar()# EntryEntry(master, width=50, textvariable=url).grid(sticky=N, row=2)# ButtonButton(master, width=20, text="Download", font=("Calibri", 12), command=download).grid(sticky=N, row=3, pady=15)master.mainloop()
Best Answer
Try To Use
get_highest_resolution()
Instant Of Contact With Streams
This Func. Returns Highest Progressive Quality : )
May Be Checking The Documentation Will Help You Next Time : )
There are two types of streams
1- Dynamic Adaptive Streaming over HTTP (DASH): save audio and video on the different tracks so you need to download both of them
2- Progressive Stream: save audio and video on the same track
The Difference is:
Progressive Stream is used only for resolutions 720p and below
DASH for the highest quality streams
so if you need to download a resolution higher than 720p like 1080pyou must use DASH but you require to download both the audio and video tracks and then post-process them with software like FFmpeg to merge them.
According to the Docs:
some streams listed have both a video codec and audio codec, while others have just video or just audio, this is a result of YouTube supporting a streaming technique called Dynamic Adaptive Streaming over HTTP (DASH).
In the context of pytube, the implications are for the highest quality streams; you now need to download both the audio and video tracks and then post-process them with software like FFmpeg to merge them.
The legacy streams that contain the audio and video in a single file (referred to as “progressive download”) are still available, but only for resolutions 720p and below.
so now you can replace your code:
video = youtube.streams.first()
with:
video = youtube.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download()
video = youtube.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
without the attribute "download()" will work well