Added a progress bar and fixed some typos.
This commit is contained in:
23
main.py
23
main.py
@@ -1,22 +1,33 @@
|
|||||||
from pytube import YouTube
|
from pytube import YouTube
|
||||||
|
|
||||||
# Asks the User for a youtube video URL.
|
# Asks the User for a YouTube video URL.
|
||||||
def get_video_url():
|
def get_video_url():
|
||||||
try:
|
try:
|
||||||
video_url = input('YouTube video URL: ')
|
video_url = input('YouTube video URL: ')
|
||||||
return video_url
|
return video_url
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"An error has occured: {e}")
|
print(f"An error has occurred: {e}")
|
||||||
|
|
||||||
# Download the video.
|
# Callback function to show the progress bar.
|
||||||
|
def progress_bar(stream, _chunk, bytes_remaining):
|
||||||
|
current = ((stream.filesize - bytes_remaining)/stream.filesize)
|
||||||
|
percent = ('{0:.1f}').format(current * 100)
|
||||||
|
progress = int(50 * current)
|
||||||
|
status = '█' * progress + '-' * (50 - progress)
|
||||||
|
print(f'\r|{status}| {percent}%', end='')
|
||||||
|
|
||||||
|
# Downloads the video.
|
||||||
def video_download(video_url):
|
def video_download(video_url):
|
||||||
try:
|
try:
|
||||||
youtube = YouTube(video_url)
|
youtube = YouTube(video_url, on_progress_callback=progress_bar)
|
||||||
video = youtube.streams.get_highest_resolution()
|
video = youtube.streams.get_highest_resolution()
|
||||||
|
print("Downloading...")
|
||||||
video.download()
|
video.download()
|
||||||
|
print("\nDownload complete!")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"An error has occured: {e}")
|
print(f"An error has occurred: {e}")
|
||||||
|
|
||||||
|
# Program main function.
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
video_url = get_video_url()
|
video_url = get_video_url()
|
||||||
@@ -24,7 +35,7 @@ def main():
|
|||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\nSession ended by user.")
|
print("\nSession ended by user.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"{e}")
|
print(f"An error has occurred: {e}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user