Update downloader with working progress bar

This commit is contained in:
moshferatu 2023-10-03 08:48:29 -07:00
parent 51a0fdef26
commit 1db3d40ce5
4 changed files with 15 additions and 10 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__/

View File

@ -3,25 +3,24 @@ import subprocess
import re
def download(video_url, output_file_name, dest_dir='./', audio_only=False, progress_callback=None):
# Ensure the destination directory exists
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
# Prepare the command to predict filename extension
# Prepare the command to predict filename extension.
if audio_only:
predict_command = ['yt-dlp', '--newline', '-x', '--audio-format', 'mp3', '--get-filename', '-o', f'{output_file_name}.%(ext)s', video_url]
else:
predict_command = ['yt-dlp', '--newline', '--get-filename', '-o', f'{output_file_name}.%(ext)s', video_url]
try:
# Using yt-dlp with --get-filename to predict the output filename
# Using yt-dlp with --get-filename to predict the output filename.
result = subprocess.check_output(predict_command)
file_extension = os.path.splitext(result.decode('utf-8').strip())[1]
except Exception as e:
print(f'Unexpected error when getting filename: {e}')
return
# Prepare the command to download the media
# Prepare the command to download the media.
if audio_only:
download_command = [
'yt-dlp',
@ -37,20 +36,20 @@ def download(video_url, output_file_name, dest_dir='./', audio_only=False, progr
]
try:
# Execute the download command and capture stdout in real-time
# Execute the download command and capture stdout in real-time.
process = subprocess.Popen(download_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
while True:
line = process.stdout.readline()
if not line:
break
if '[download]' in line:
# Parse the progress from the output
# Parse the progress from the output.
match = re.search(r'\[download\]\s+(\d+\.\d+|\d+)%', line)
if match:
percentage = int(float(match.group(1)))
if progress_callback:
progress_callback(percentage) # Call the callback with the parsed progress value
process.communicate() # Wait for the process to finish if it hasn't yet
progress_callback(percentage) # Call the callback with the parsed progress value.
process.communicate() # Wait for the process to finish if it hasn't yet.
except subprocess.CalledProcessError:
print('Error occurred while downloading the video/audio.')
except Exception as e:

View File

@ -1,4 +1,6 @@
import threading
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from download import download
@ -15,7 +17,9 @@ def start_download():
messagebox.showerror("Error", "Please fill in all required fields!")
return
download(url, filename, dest_dir=dest_dir, audio_only=audio_only, progress_callback=update_progress_bar)
# Run the download function in a separate thread
thread = threading.Thread(target=download, args=(url, filename, dest_dir, audio_only, update_progress_bar))
thread.start()
def select_directory():
directory = filedialog.askdirectory()

View File

@ -1 +1,2 @@
tkinter
tkinter
yt-dlp