45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
from pytube import YouTube
|
|
|
|
# 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 occurred: {e}")
|
|
|
|
# Callback function to show the progress bar.
|
|
def progress_bar(stream, _chunk, bytes_remaining):
|
|
try:
|
|
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='')
|
|
except Exception as e:
|
|
print(f"An error has occurred: {e}")
|
|
|
|
# Downloads the video.
|
|
def video_download(video_url):
|
|
try:
|
|
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 occurred: {e}")
|
|
|
|
# Program main function.
|
|
def main():
|
|
try:
|
|
video_url = get_video_url()
|
|
video_download(video_url)
|
|
except KeyboardInterrupt:
|
|
print("\nSession ended by user.")
|
|
except Exception as e:
|
|
print(f"An error has occurred: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|