Added a progress bar and fixed some typos.

This commit is contained in:
2024-02-19 21:03:57 -05:00
parent 51201f26c2
commit 5bdc69c548

23
main.py
View File

@@ -1,22 +1,33 @@
from pytube import YouTube
# Asks the User for a youtube video URL.
# Asks the User for a YouTube video URL.
def get_video_url():
try:
video_url = input('YouTube video URL: ')
return video_url
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):
try:
youtube = YouTube(video_url)
youtube = YouTube(video_url, on_progress_callback=progress_bar)
video = youtube.streams.get_highest_resolution()
print("Downloading...")
video.download()
print("\nDownload complete!")
except Exception as e:
print(f"An error has occured: {e}")
print(f"An error has occurred: {e}")
# Program main function.
def main():
try:
video_url = get_video_url()
@@ -24,7 +35,7 @@ def main():
except KeyboardInterrupt:
print("\nSession ended by user.")
except Exception as e:
print(f"{e}")
print(f"An error has occurred: {e}")
if __name__ == "__main__":
main()