fix progress issues

This commit is contained in:
Cyberes 2023-05-06 20:05:25 -06:00
parent 5379207af6
commit ff7b7205d1
1 changed files with 16 additions and 6 deletions

View File

@ -10,6 +10,7 @@ from pathlib import Path
from threading import Thread
import numpy as np
# import yt_dlp as ydl_ydl
from tqdm.auto import tqdm
from unidecode import unidecode
@ -64,13 +65,20 @@ def download_video(args) -> dict:
# time.sleep(random.randint(1, 20) / 1000)
def progress_hook(d):
# downloaded_bytes and total_bytes can be None if the download hasn't started yet.
# Variables can be None if the download hasn't started yet.
if d['status'] == 'downloading':
if d.get('downloaded_bytes') and d.get('total_bytes'):
total = None
if d.get('downloaded_bytes'):
# We want total_bytes but it may not exist so total_bytes_estimate is good too
if d.get('total_bytes'):
total = d.get('total_bytes')
elif d.get('total_bytes_estimate'):
total = d.get('total_bytes_estimate')
if total:
downloaded_bytes = int(d['downloaded_bytes'])
total_bytes = int(d['total_bytes'])
if total_bytes > 0:
percent = (downloaded_bytes / total_bytes) * 100
if total > 0:
percent = (downloaded_bytes / total) * 100
bar.update(int(np.round(percent - bar.n))) # If the progress bar doesn't end at 100% then round to 1 decimal place
bar.set_postfix({
'speed': d['_speed_str'],
@ -121,7 +129,7 @@ def download_video(args) -> dict:
# We created a new dict
video['title'] = unidecode(video['title'])
video['uploader'] = unidecode(video['uploader']) # now the additional info is present since we fetched it
video['uploader'] = unidecode(video['uploader']) # now the additional info is present since we fetched it
video_filename = remove_special_chars_linux(ydl.get_output_templ(video_id=video['id'], title=video['title'], uploader=video['uploader'], uploader_id=video['uploader_id'], include_ext=False), special_chars=['/'])
@ -148,6 +156,8 @@ def download_video(args) -> dict:
# base_path = kwargs['output_dir'] / video['id']
ylogger = ytdl_logger(setup_file_logger(video['id'], base_path + '.log'))
kwargs['ydl_opts']['logger'] = ylogger
# with ydl_ydl.YoutubeDL(kwargs['ydl_opts']) as y:
# error_code = y.download(video['url'])
yt_dlp = ydl.YDL(kwargs['ydl_opts']) # recreate the object with the correct logging path
error_code = yt_dlp(video['url']) # Do the download
if not error_code: