2

I want to implement a progress bar in my code, but neither the old nor the new way of implementation is working.

How to add progress bar? this fix dosen't work in the latest version.
Here is the latest documentation https://pypi.org/project/pytube/

from pytube import YouTube
url="https://youtu.be/J5EXnh53A1k"
path=r'D://'
yt = YouTube(url)
yt.register_on_progress_callback(show_progress_bar)#by commenting this line code works fine but no progress bar is displyed
yt.streams.filter(file_extension='mp4').first().download(path)


def show_progress_bar(stream, _chunk, _file_handle, bytes_remaining):
  current = ((stream.filesize - bytes_remaining)/stream.filesize)
  percent = ('{0:.1f}').format(current*100)
  progress = int(50*current)
  status = '█' * progress + '-' * (50 - progress)
  sys.stdout.write(' ↳ |{bar}| {percent}%\r'.format(bar=status, percent=percent))
  sys.stdout.flush()
shubhamr238
  • 1,226
  • 14
  • 22

4 Answers4

4

You first need to define the progress bar function, say progress_function:

def progress_function(chunk, file_handle, bytes_remaining):
    global filesize
    current = ((filesize - bytes_remaining)/filesize)
    percent = ('{0:.1f}').format(current*100)
    progress = int(50*current)
    status = '█' * progress + '-' * (50 - progress)
    sys.stdout.write(' ↳ |{bar}| {percent}%\r'.format(bar=status, percent=percent))
    sys.stdout.flush()

Then register the above defined function progress_function with the on_progress_callback as follows:

yt_obj = YouTube(<<youtube_video_url>>, on_progress_callback = progress_function)

Rest of the code follows:

yt_obj.streams.filter(progressive=True, file_extension='mp4').get_highest_resolution().download(output_path='/home/myusername/Videos', filename='MyVideo')

Output looks like this:

↳ |██████████████████████████████████----------------| 68.4%

Have fun!!

vagdevi k
  • 1,478
  • 9
  • 25
  • NameError: name 'filesize' is not defined, how can I solve this error? – Khan Saad Jul 18 '21 at 09:32
  • filesize is something which we have to set outside the function. This is the reason that was set global in function. – vagdevi k Jul 19 '21 at 12:33
  • It is not necessary to always set it outside. You can get the filesize info from the 'chunk' parameter. So you can just use it like `filesize=chunk.filesize` – Sai Dandem Jun 21 '22 at 04:43
2

I'm using progressbar2

def progress_Check(stream = None, chunk = None, file_handle = None, remaining = None):

        percent = file_size - remaining + 1000000   

        try: 
            # updates the progress bar                                   
            bar.update(round(percent/1000000,2))
        except: 
            # progress bar dont reach 100% so a little trick to make it 100    
            bar.update(round(file_size/1000000,2))

yt = YouTube(url, on_progress_callback=progress_Check)
yt = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download()
1

Here is function used to download youtube video and display progress bar from shell:

from pytube import YouTube
from pytube.cli import on_progress

fuchsia = '\033[38;2;255;00;255m'   #  color as hex #FF00FF
reset_color = '\033[39m'

# url is url of youtube video to download.
def download_youtube(url):

    """ Instantiates YouTube class and downloads selected video.  Uses Built-in
    pytube.cli function on_progress to show a DOS style progress bar. """
    yt = YouTube(url, on_progress_callback=on_progress)

    # following line displays title and number of times video has been viewed. 
    print(f'\n' + fuchsia + 'Downloading: ', yt.title, '~ viewed', yt.views, 
    'times.')

    # creates download and downloads to subdirectory called 'downloads'
    yt.streams.first().download('.\\downloads\\')

    # displays message verifying download is complete, and resets color scheme 
    # back to original color scheme.
    print(f'\nFinished downloading:  {yt.title}' + reset_color)

Display colors were switched because the default progress bar is fairly bright. In event video was previously downloaded the 'Finished downloading:' message will display but the progress bar won't displayed.

Please see this Showing progress in pytube regarding the use of pytube's built-in on_progress function.

chester
  • 11
  • 3
-1
# importing YouTube from pytube

import progressbar as progress
from pytube import YouTube


def progress(streams, chunk: bytes, bytes_remaining: int):
    contentsize = video.filesize
    size = contentsize - bytes_remaining

    print('\r' + '[Download progress]:[%s%s]%.2f%%;' % (
    '█' * int(size*20/contentsize), ' '*(20-int(size*20/contentsize)), float(size/contentsize*100)), end='')


url = 'https://www.youtube.com/watch?v=qOVAbKKSH10'
yt = YouTube(url, on_progress_callback=progress)
video = yt.streams.get_highest_resolution()
video.download()
Juri
  • 24
  • 2