Написал программу, чтобы скачивать с YouTube сразу много видео подряд в лучшем из доступных качеств, она работает, всё хорошо, кроме долгой загрузки видео. Если качать через приложение для скачивания видео в браузере, то видео скачиваются очень быстро, буквально меньше секунды, а программы эти же видео скачивает 48 секунд! Почему? Как исправить?
Код программы
import pytube
import time
def progress_check(stream=None, chunk=None, file_handle=None, remaining=None):
# Gets the percentage of the file that has been downloaded.
percent = int((100*(file_size-remaining))/file_size)
if percent % 20 == 0 and percent not in past:
print('%s%%' % percent)
past.append(percent)
global file_size
global past
if __name__ == "__main__":
path = 'D:/Kirill/Download'
video_urls = [] # list from video urls
url = input("Input URL of a video: ")
while url: # while url is not empty
video_urls.append(url)
url = input("Input URL of a video or just press Enter: ")
for i in video_urls:
start_time = time.time()
past = []
youtube = pytube.YouTube(i, on_progress_callback=progress_check)
video = youtube.streams.filter(adaptive=True).first()
file_size = video.filesize
title = video.title
resolution = video.resolution
print('Resolution: %s, Size: %s byte, Title: %s' % (resolution, file_size, title))
print('Your video will be saved to: %s' % path)
print('Downloaded: ')
video.download(path) # path, where to video download.
print('The video "%s" has downloaded' % title)
print("--- %s seconds ---" % (time.time() - start_time))
print(' ')
Пример вывода
Input URL of a video: https://www.youtube.com/watch?v=hpxMZde62vE&list=PL0bz9dYjS6jMjhwEq0MP4_0w1tD1epIX5&index=6&t=54s'
Input URL of a video or just press Enter: https://www.youtube.com/watch?v=YBUCJK-STFo&list=PL0bz9dYjS6jMjhwEq0MP4_0w1tD1epIX5&index=4'
Input URL of a video or just press Enter:
Resolution: 480p, Size: 13236825 byte, Title: Арсений Тарковский "Жизнь, жизнь"
Your video will be saved to: D:/Kirill/Download
Downloaded:
0%
20%
40%
60%
80%
100%
The video "Арсений Тарковский "Жизнь, жизнь"" has downloaded
--- 48.40962624549866 seconds ---
Resolution: 480p, Size: 3023536 byte, Title: С утра я тебя дожидался вчера - Арсений Тарковский
Your video will be saved to: D:/Kirill/Download
Downloaded:
0%
20%
40%
60%
80%
100%
The video "С утра я тебя дожидался вчера - Арсений Тарковский" has downloaded
--- 6.241721153259277 seconds ---