Skip to content

Commit

Permalink
moved the download to a seperate thread so the application will no lo…
Browse files Browse the repository at this point in the history
…nger freeze, added hook for debug info which hardly ever works
  • Loading branch information
EndDragon438 committed Jan 17, 2025
1 parent 024c038 commit de15f80
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ffmpeg.exe
100 changes: 94 additions & 6 deletions Youtube-Downloader-GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from yt_dlp import YoutubeDL

from PySide6.QtCore import QSize, QDir
from PySide6.QtCore import QSize, QDir, QRunnable, QThreadPool
from PySide6.QtGui import QPalette
from PySide6.QtWidgets import (
QApplication,
Expand All @@ -18,12 +18,31 @@
QGroupBox,
QCheckBox,
QLineEdit,
QDialog,
)

# class DownloadLogger(QLabel):
# def debug(self, msg):
# if msg.startswith('[debug] '):
# pass
# else:
# print(msg)
# self.setText(msg)
# def info(self, msg):
# pass
# def warning(self, msg):
# pass
# def error(self, msg):
# print(msg)

class MainWindow(QMainWindow):
def __init__(self):
super().__init__()

# print(help(YoutubeDL))

self.threadpool = QThreadPool()

self.setWindowTitle("yt-dlp GUI")
self.setMinimumSize(QSize(400, 400))

Expand Down Expand Up @@ -196,17 +215,86 @@ def download(self):
'paths': {'home': self.command[5]},
'postprocessors': self.command[1],
'retries': 10,
'updatetime': False
'updatetime': False,
# 'logger': Logger()
'progress_hooks': [self.downloadHook]
}

if self.command[3] != '':
options['final_ext'] = self.command[3]

self.commandLabel.setText("if this is showing for a while, it's cause sometimes i don't get progress from ffmpeg cause that PR still isn't merged\nit's still doin things :3")

# Multithreading!!
downloader = Downloader()
downloader.setOptions(options)
downloader.setTarget(self.command[6])

self.threadpool.start(downloader)

def downloadHook(self, dict):
print(f"\ndownloadHook called with {dict['status']}")
try:
match dict['status']:
case 'downloading':
self.commandLabel.setText(f"{str('%.3f'%dict['elapsed'])} seconds at {str('%.3f'%(dict['speed'].round() / 1000))} kilobytes/second")
case 'finished':
self.commandLabel.setText("Download Complete!")
except:
print("\nHook Failed")


self.commandLabel.setText("Downloading...")
class Downloader(QRunnable):
def __init__(self):
super().__init__()

self.options = {}
self.target = ''


def setOptions(self, options):
self.options = options

with YoutubeDL(options) as ytdl:
ytdl.download(self.command[6])
def setTarget(self, target):
self.target = target

def run(self):
with YoutubeDL(self.options) as ytdl:
ytdl.download(self.target)

# This class is meant to display logs as yt-dlp is downloading the video, and then close itself.
# However, because of the exec() call the thread is taken up by the dialog box listening for events
# So all the log calls don't get through
# I don't feel like dealing with this right now so i'm just going to provide a version of the .exe with console enabled
class Logger(QDialog):
def __init__(self):
super().__init__()

self.commandLabel.setText("Download Complete!")
layout = QVBoxLayout()

self.logLine = QLabel("logging...")
layout.addWidget(self.logLine)

self.setLayout(layout)

self.setWindowTitle("Logs")
self.exec()

def debug(self, msg):
print(f"DEBUG: {msg}")
if msg.find('[download]') > -1:
self.logLine.setText(msg[10:])
elif msg.find('Merger') > -1:
self.close()
def info(self, msg):
print(f"INFO: {msg}")
pass
def warning(self, msg):
print(f"WARN: {msg}")
pass
def error(self, msg):
print(f"ERROR: {msg}")
pass

app = QApplication([])

Expand Down

0 comments on commit de15f80

Please sign in to comment.