Search code examples
pythonpytube

Pytube how to add a progress bar?


I don't know how to do it and whenever i try solutions from other topics about this question i get errors. Mostly "TypeError: show_progress_bar() missing 1 required positional argument: 'bytes_remaining'".

from pytube import YouTube

#took this def from another topic
def progress_function(stream, chunk, file_handle, bytes_remaining):
    percent = round((1-bytes_remaining/video.filesize)*100)

    if( percent%10 == 0):
        print(percent, 'done...')



url = "Any youtube url"

yt = YouTube(url, on_progress_callback=progress_function)

yt.streams[0].download()

for example when i run this exact code it gives me that error.

I really can't comprehend its logic. I also searched the docs from pytube3 website but i can't solve this. Pls help me. Thanks.


Solution

  • Remove the stream then it will work, recently I tried to develop similar logic facing a similar error.

    Here is the code that worked for me:

    def progress(chunk, file_handle, bytes_remaining):
        global filesize
        remaining = (100 * bytes_remaining) / filesize
        step = 100 - int(remaining)
        print("Completed:", step) # show the percentage of completed download 
    

    The filesize can be retrived, once you select which video or audio to download such as

    yt = YouTube(str(link), on_progress_callback=progress) # Declare YouTube
    yt1 = yt.streams.get_by_itag(int(itag)) # itag is given when you list all the streams of a youtube video
    filesize = yt1.filesize
    

    Hope this helps!