PyTube Progressbar

I wrote a video downloader in Python Django and I’m using PyTube. There is an extra function from PyTube which allows you to show an progress bar. I wrote some lines from the bar but this erorr appears:

TypeError: progress_function() missing 1 required positional argument: 'bytes_remaining' 

I’m sure my code isn’t complete yet but I cant figure out what to change that everything works.

This is my code: views.py

def converter(request): 
    download_begins = True
    if request.method == 'POST':
        link = request.POST['link']
        video = YouTube(link)
        format = request.POST['format']
 
        uuid = shortuuid.ShortUUID().random(length=4)
        if format == "3":
            with OpenKey(HKEY_CURRENT_USER, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders') as key:
                downloads = QueryValueEx(key, '{374DE290-123F-4565-9164-39C4925E467B}')[0]
            yt = YouTube(link, on_progress_callback=progress_function)
            audio_file = yt.streams.filter(only_audio=True).first().download(downloads)
            base, ext = os.path.splitext(audio_file)
            new_file = base + uuid + '.mp3'
            os.rename(audio_file, new_file)



        elif format == "4":
            with OpenKey(HKEY_CURRENT_USER, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders') as key:
                downloads = QueryValueEx(key, '{374DE290-123F-4565-9164-39C4925E467B}')[0]
            yt = YouTube(link, on_progress_callback=progress_function)
            ys = yt.streams.filter(res="1080p").first().download(downloads)
            base, ext = os.path.splitext(ys)
            new_file = base + uuid + '.mp4'
            os.rename(ys, new_file)

            

        context = {'format': format, 'begins': download_begins}
        return render(request, 'home/videoconverter.html', context)
    return render(request, 'home/videoconverter.html')

def percent(tem, total):
    perc = (float(tem) / float(total)) * float(100)
    return perc

def progress_function(stream, chunk, file_handle, bytes_remaining):

    size = stream.filesize
    p = 0
    while p <= 100:
        progress = p
        str(p) + '%'
        p = percent(bytes_remaining, size)


and another question is how can I get the progress bar in the template, I mean in the views there is just the algorythm behind the bar but how can I get it into the template.

Thanks for your answers!

1 Like

What’s happening is that YouTube is calling the on_progress_callback with a different set of arguments than your function progress_function expects. In particular, it’s not supplying the fourth argument, AKA bytes_remaining. If you look at the course code for that class, you should be able to see how on_progress_callback is called and what the actual signature will be.

Do you have a link for the PyTube repo/project? The only thing I found was a sourceforge project from 2009.