5

Is there a way to add progress bar in pytube? I don't know how to use the following method:

pytube.Stream().on_progress(chunk, file_handler, bytes_remaining)

My code:

from pytube import YouTube
# from pytube import Stream
from general import append_to_file


def downloader(video_link, down_dir=None):
    try:
        tube = YouTube(video_link)
        title = tube.title
        print("Now downloading,  " + str(title))
        video = tube.streams.filter(progressive=True, file_extension='mp4').first()
        print('FileSize : ' + str(round(video.filesize/(1024*1024))) + 'MB')
        # print(tube.streams.filter(progressive=True, file_extension='mp4').first())
        # Stream(video).on_progress()
        if down_dir is not None:
            video.download(down_dir)
        else:
            video.download()
        print("Download complete, " + str(title))
        caption = tube.captions.get_by_language_code('en')
        if caption is not None:
            subtitle = caption.generate_srt_captions()
            open(title + '.srt', 'w').write(subtitle)
    except Exception as e:
        print("ErrorDownloadVideo  |  " + str(video_link))
        append_to_file('debug', format(e))
    # FILESIZE print(tube.streams.filter(progressive=True, file_extension='mp4').first().filesize/(1024*1024))
xskxzr
  • 10,946
  • 11
  • 32
  • 70
Gayan Jeewantha
  • 115
  • 1
  • 1
  • 7

5 Answers5

6

Call your progress function inside the Youtube class

yt = YouTube(video_link, on_progress_callback=progress_func)

This is your progress function

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

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

This computes the percentage converting the file size and the bytes remaining

def percent(self, tem, total):
        perc = (float(tem) / float(total)) * float(100)
        return perc
clinomaniac
  • 2,202
  • 2
  • 15
  • 22
4

I know this is already answered, but I came across this and for me, the progress was counting down from 100 to 0. Since I wanted to fill a progress bar with the percentage value, I couldn't use this.

So I came up with this solution:

def progress_func(self, stream, chunk, file_handle,bytes_remaining):
    size = self.video.filesize
    progress = (float(abs(bytes_remaining-size)/size))*float(100))
    self.loadbar.setValue(progress)

The loadbar is my Progress Bar from PyQt5. Hope this helps someone.

user14586140
  • 196
  • 1
  • 12
moRa
  • 63
  • 7
4

The callback function takes three arguments, not four: stream, chunk and bytes_remaining.

Run_Script
  • 2,146
  • 2
  • 10
  • 24
  • Why ? The doc for the lastest version show these three arguments : `on_progress(chunk: bytes, file_handler: BinaryIO, bytes_remaining: int)` I used stream and bytes_remaining and it works but I don't understand why ! – Maxouille Dec 13 '20 at 19:24
2

Somewhat shorter option:

yt = YouTube(video_link, on_progress_callback=progress_function)

video = yt.streams.first() # or whatever 

# Prints something like "15.555% done..." 
def progress_function(stream, chunk, file_handle, bytes_remaining):
    print(round((1-bytes_remaining/video.filesize)*100, 3), '% done...')

You can, of course, limit the progress output, for instance, to values like 10, 20, 30%... - just surround the print statement with the required if-clause.

Zubo
  • 1,178
  • 1
  • 15
  • 25
1
from pytube import Playlist
from pytube import YouTube

previousprogress = 0
def on_progress(stream, chunk, bytes_remaining):
    global previousprogress
    total_size = stream.filesize
    bytes_downloaded = total_size - bytes_remaining 

    liveprogress = (int)(bytes_downloaded / total_size * 100)
    if liveprogress > previousprogress:
        previousprogress = liveprogress
        print(liveprogress)


yt = YouTube('https://www.youtube.com/watch?v=4zqKJBxRyuo&ab_channel=SleepEasyRelax-KeithSmith')
yt.register_on_progress_callback(on_progress)
yt.streams.filter(only_audio=True).first().download()
tichra
  • 477
  • 3
  • 18