__license__ = 'GPL-3.0-only' from pathlib import Path from PyQt6.QtWidgets import (QMainWindow, QMessageBox, QFileDialog, QGridLayout, QSpacerItem, QSizePolicy, QWidget, QLabel, QLineEdit, QPushButton) import yt_dlp from yt_dlp import YoutubeDL class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("yt-dlp") self.resize(600, 160) layout = QGridLayout() self.url = QLineEdit() self.download_path = QLineEdit() self.download_path.setText(str(Path.home()/'Downloads')) browse_btn = QPushButton("...") browse_btn.clicked.connect(self.browse) download_btn = QPushButton("Download") layout.addWidget(QLabel("URL:"), 0, 0) layout.addWidget(self.url, 0, 1, 1, 2) layout.addWidget(QLabel("Download:"), 1, 0) layout.addWidget(self.download_path, 1, 1) layout.addWidget(browse_btn, 1, 2) layout.addItem(QSpacerItem(0, 0, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding), 2, 1) layout.addWidget(download_btn, 3, 1) download_btn.clicked.connect(self.download) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) def on_finish_hook(self, data): if data['status'] == 'finished': print(f"\n\nfinished: {data['filename']}\n") def browse(self): dirpath = QFileDialog.getExistingDirectory(self) if len(dirpath) > 0: self.download_path.setText(dirpath) def download(self): ydl_opts = { 'paths': {'home': self.download_path.text()}, 'progress_hooks': [self.on_finish_hook], } with YoutubeDL(ydl_opts) as ydl: try: ydl.download([self.url.text()]) except yt_dlp.utils.DownloadError as ex: box = QMessageBox(self) box.setWindowTitle(self.windowTitle()) box.setText('Error downloading using yt-dlp') box.setDetailedText(f'{ex}') box.exec() finally: QMessageBox.information(self, self.windowTitle(), 'Finished') self.url.clear()