From ff7b7205d15472b85d1d638c6f04b1eac25d8e6a Mon Sep 17 00:00:00 2001 From: Cyberes Date: Sat, 6 May 2023 20:05:25 -0600 Subject: [PATCH] fix progress issues --- process/threads.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/process/threads.py b/process/threads.py index 125f7f1..2a14f42 100644 --- a/process/threads.py +++ b/process/threads.py @@ -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: