Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add progress bar?

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))
like image 675
Gayan Jeewantha Avatar asked Mar 09 '18 02:03

Gayan Jeewantha


People also ask

How do I add a progress bar in Excel?

Step 2: Add the Progress Bars Next, highlight the cell range B2:B11 that contains the progress percentages, then click the Conditional Formatting icon on the Home tab, then click Data Bars, then click More Rules: A new window appears that allows you to format the data bars.


2 Answers

Call your progress function inside the Youtube class

yt = YouTube(video_link, on_progress_callback=progress_function)

This is your progress function

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

    size = stream.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
like image 51
Ismael GraHms Avatar answered Sep 24 '22 17:09

Ismael GraHms


You can also do like this without writing your own function.

code:

from pytube import YouTube
from pytube.cli import on_progress #this module contains the built in progress bar. 
link=input('enter url:')
yt=YouTube(link,on_progress_callback=on_progress)
videos=yt.streams.first()
videos.download()
print("(:")
like image 37
Rishi Ratan Pandey Avatar answered Sep 23 '22 17:09

Rishi Ratan Pandey