import sys import os import webbrowser from PySide6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLineEdit, QPushButton, QTableWidget, QTableWidgetItem, QProgressBar, QLabel, QFileDialog, QHeaderView, QStyle, QStyleFactory, QComboBox, QTextEdit, QDialog, QPlainTextEdit, QCheckBox, QButtonGroup, QListWidget, QListWidgetItem, QDialogButtonBox, QScrollArea, QGroupBox) from PySide6.QtCore import Qt, Signal, QObject, QThread, QProcess from PySide6.QtGui import QIcon, QPalette, QColor, QPixmap import requests from io import BytesIO from PIL import Image from datetime import datetime import json from pathlib import Path from packaging import version import subprocess import re import yt_dlp from ytsage_ffmpeg import auto_install_ffmpeg, check_ffmpeg_installed from ytsage_utils import check_ffmpeg, get_yt_dlp_path, load_saved_path, save_path # Import utility functions class LogWindow(QDialog): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle('yt-dlp Log') self.setMinimumSize(700, 500) layout = QVBoxLayout(self) self.log_text = QTextEdit() self.log_text.setReadOnly(True) self.log_text.setStyleSheet(""" QTextEdit { background-color: #2b2b2b; color: #ffffff; font-family: Consolas, monospace; font-size: 12px; border: 2px solid #3d3d3d; border-radius: 4px; } """) layout.addWidget(self.log_text) def append_log(self, message): self.log_text.append(message) # Auto-scroll to bottom scrollbar = self.log_text.verticalScrollBar() scrollbar.setValue(scrollbar.maximum()) class CustomCommandDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.parent = parent self.setWindowTitle('Custom yt-dlp Command') self.setMinimumSize(600, 400) layout = QVBoxLayout(self) # Help text help_text = QLabel( "Enter custom yt-dlp commands below. The URL will be automatically appended.\n" "Example: --extract-audio --audio-format mp3 --audio-quality 0\n" "Note: Download path and output template will be preserved." ) help_text.setWordWrap(True) help_text.setStyleSheet("color: #999999; padding: 10px;") layout.addWidget(help_text) # Command input self.command_input = QPlainTextEdit() self.command_input.setPlaceholderText("Enter yt-dlp arguments...") self.command_input.setStyleSheet(""" QPlainTextEdit { background-color: #1d1e22; color: #ffffff; border: 2px solid #1d1e22; border-radius: 4px; padding: 8px; font-family: Consolas, monospace; } """) layout.addWidget(self.command_input) # Add SponsorBlock checkbox self.sponsorblock_checkbox = QCheckBox("Remove Sponsor Segments") self.sponsorblock_checkbox.setStyleSheet(""" QCheckBox { color: #ffffff; padding: 5px; margin-left: 20px; } QCheckBox::indicator { width: 18px; height: 18px; border-radius: 9px; } QCheckBox::indicator:unchecked { border: 2px solid #666666; background: #1d1e22; border-radius: 9px; } QCheckBox::indicator:checked { border: 2px solid #c90000; background: #c90000; border-radius: 9px; } """) layout.insertWidget(layout.indexOf(self.command_input), self.sponsorblock_checkbox) # Buttons button_layout = QHBoxLayout() self.run_btn = QPushButton("Run Command") self.run_btn.clicked.connect(self.run_custom_command) self.close_btn = QPushButton("Close") self.close_btn.clicked.connect(self.close) button_layout.addWidget(self.run_btn) button_layout.addWidget(self.close_btn) layout.addLayout(button_layout) # Log output self.log_output = QTextEdit() self.log_output.setReadOnly(True) self.log_output.setStyleSheet(""" QTextEdit { background-color: #1d1e22; color: #ffffff; border: 2px solid #1d1e22; border-radius: 4px; padding: 8px; font-family: Consolas, monospace; font-size: 12px; } """) layout.addWidget(self.log_output) self.setStyleSheet(""" QDialog { background-color: #15181b; } QPushButton { padding: 8px 15px; background-color: #c90000; border: none; border-radius: 4px; color: white; font-weight: bold; } QPushButton:hover { background-color: #a50000; } """) def run_custom_command(self): url = self.parent.url_input.text().strip() if not url: self.log_output.append("Error: No URL provided") return command = self.command_input.toPlainText().strip() path = self.parent.path_input.text().strip() self.log_output.clear() self.log_output.append(f"Running command with URL: {url}") self.run_btn.setEnabled(False) # Start command in thread import threading threading.Thread(target=self._run_command_thread, args=(command, url, path), daemon=True).start() def _run_command_thread(self, command, url, path): try: class CommandLogger: def debug(self, msg): self.dialog.log_output.append(msg) def warning(self, msg): self.dialog.log_output.append(f"Warning: {msg}") def error(self, msg): self.dialog.log_output.append(f"Error: {msg}") def __init__(self, dialog): self.dialog = dialog # Split command into arguments args = command.split() # Base options ydl_opts = { 'logger': CommandLogger(self), 'paths': {'home': path}, 'debug_printout': True, 'postprocessors': [] } # Add SponsorBlock options if enabled if self.sponsorblock_checkbox.isChecked(): ydl_opts['postprocessors'].extend([{ 'key': 'SponsorBlock', 'categories': ['sponsor', 'selfpromo', 'interaction'], 'api': 'https://sponsor.ajay.app' }, { 'key': 'ModifyChapters', 'remove_sponsor_segments': ['sponsor', 'selfpromo', 'interaction'], 'sponsorblock_chapter_title': '[SponsorBlock]: %(category_names)l', 'force_keyframes': True }]) # Add custom arguments for i in range(0, len(args), 2): if i + 1 < len(args): key = args[i].lstrip('-').replace('-', '_') value = args[i + 1] try: # Try to convert to appropriate type if value.lower() in ('true', 'false'): value = value.lower() == 'true' elif value.isdigit(): value = int(value) ydl_opts[key] = value except: ydl_opts[key] = value with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download([url]) self.log_output.append("Command completed successfully") except Exception as e: self.log_output.append(f"Error: {str(e)}") finally: self.run_btn.setEnabled(True) class FFmpegInstallThread(QThread): finished = Signal(bool) progress = Signal(str) def run(self): # Redirect stdout to capture progress messages import sys from io import StringIO import contextlib output = StringIO() with contextlib.redirect_stdout(output): success = auto_install_ffmpeg() # Process captured output and emit progress signals for line in output.getvalue().splitlines(): self.progress.emit(line) self.finished.emit(success) class FFmpegCheckDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle('Installing FFmpeg') self.setMinimumWidth(450) self.setMinimumHeight(250) # Set the window icon to match the main app self.setWindowIcon(self.style().standardIcon(QStyle.StandardPixmap.SP_ArrowDown)) layout = QVBoxLayout(self) layout.setSpacing(15) # Header with icon header_layout = QHBoxLayout() icon_label = QLabel() icon_label.setPixmap(self.style().standardIcon(QStyle.StandardPixmap.SP_ArrowDown).pixmap(32, 32)) header_layout.addWidget(icon_label) header_text = QLabel("FFmpeg Installation") header_text.setStyleSheet("font-size: 16px; font-weight: bold;") header_layout.addWidget(header_text) header_layout.addStretch() layout.addLayout(header_layout) # Message self.message_label = QLabel( "🎥 YTSage needs FFmpeg to process videos.\n" "Let's set it up for you automatically!" ) self.message_label.setWordWrap(True) self.message_label.setStyleSheet("font-size: 13px;") layout.addWidget(self.message_label) # Progress label with cool emojis self.progress_label = QLabel("") self.progress_label.setWordWrap(True) self.progress_label.setStyleSheet(""" QLabel { background-color: #1e1e1e; border-radius: 5px; padding: 10px; font-family: 'Consolas', monospace; font-size: 12px; } """) self.progress_label.hide() layout.addWidget(self.progress_label) # Buttons container button_layout = QHBoxLayout() # Install button self.install_btn = QPushButton("Install FFmpeg") self.install_btn.setIcon(self.style().standardIcon(QStyle.StandardPixmap.SP_ArrowDown)) self.install_btn.clicked.connect(self.start_installation) button_layout.addWidget(self.install_btn) # Manual install button self.manual_btn = QPushButton("Manual Guide") self.manual_btn.setIcon(self.style().standardIcon(QStyle.StandardPixmap.SP_DialogHelpButton)) self.manual_btn.clicked.connect(lambda: webbrowser.open('https://github.com/oop7/ffmpeg-install-guide')) button_layout.addWidget(self.manual_btn) # Close button self.close_btn = QPushButton("Close") self.close_btn.setIcon(self.style().standardIcon(QStyle.StandardPixmap.SP_DialogCloseButton)) self.close_btn.clicked.connect(self.close) button_layout.addWidget(self.close_btn) layout.addLayout(button_layout) # Style the dialog self.setStyleSheet(""" QDialog { background-color: #2b2b2b; } QLabel { color: #ffffff; } QPushButton { padding: 8px 15px; background-color: #3d3d3d; border: none; border-radius: 4px; color: white; font-weight: bold; margin: 5px; min-width: 100px; } QPushButton:hover { background-color: #4d4d4d; } QPushButton:disabled { background-color: #2d2d2d; color: #666666; } """) # Initialize installation thread self.install_thread = None def start_installation(self): self.install_btn.setEnabled(False) self.manual_btn.setEnabled(False) self.close_btn.setEnabled(False) # Check if FFmpeg is already installed if check_ffmpeg_installed(): self.message_label.setText("🎉 FFmpeg is already installed!") self.progress_label.setText("✅ You can close this dialog and continue using YTSage.") self.install_btn.hide() self.manual_btn.hide() self.close_btn.setEnabled(True) return self.message_label.setText("🚀 Installing FFmpeg... Hold tight!") self.progress_label.show() self.install_thread = FFmpegInstallThread() self.install_thread.finished.connect(self.installation_finished) self.install_thread.progress.connect(self.update_progress) self.install_thread.start() def update_progress(self, message): self.progress_label.setText(message) def installation_finished(self, success): if success: self.message_label.setText("🎉 FFmpeg has been installed successfully!") self.progress_label.setText("✅ You're all set! You can now close this dialog and continue using YTSage.") self.install_btn.hide() self.manual_btn.hide() else: self.message_label.setText("❌ Oops! FFmpeg installation encountered an issue.") self.progress_label.setText("💡 Try using the manual installation guide instead.") self.install_btn.setEnabled(True) self.manual_btn.setEnabled(True) self.close_btn.setEnabled(True) class VersionCheckThread(QThread): finished = Signal(str, str, str) # current_version, latest_version, error_message def run(self): current_version = "" latest_version = "" error_message = "" try: # Get the yt-dlp executable path if getattr(sys, 'frozen', False): if sys.platform == 'win32': yt_dlp_path = os.path.join(os.path.dirname(sys.executable), 'yt-dlp.exe') else: yt_dlp_path = os.path.join(os.path.dirname(sys.executable), 'yt-dlp') else: yt_dlp_path = 'yt-dlp' # Get current version try: result = subprocess.run([yt_dlp_path, '--version'], capture_output=True, text=True, startupinfo=None if sys.platform != 'win32' else subprocess.STARTUPINFO(dwFlags=subprocess.STARTF_USESHOWWINDOW, wShowWindow=subprocess.SW_HIDE), # Hide console window on Windows creationflags=subprocess.CREATE_NO_WINDOW if sys.platform == 'win32' else 0) # Hide console window on Windows if result.returncode == 0: current_version = result.stdout.strip() else: # Try fallback if command failed import yt_dlp current_version = yt_dlp.version.__version__ except Exception: # Fallback to importing yt_dlp package directly if subprocess fails try: import yt_dlp current_version = yt_dlp.version.__version__ except ImportError: error_message = "yt-dlp not found or accessible." self.finished.emit(current_version, latest_version, error_message) return # Get latest version from PyPI response = requests.get("https://pypi.org/pypi/yt-dlp/json", timeout=10) # Add timeout response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx) latest_version = response.json()["info"]["version"] # Clean up version strings current_version = current_version.replace('_', '.') latest_version = latest_version.replace('_', '.') except requests.RequestException as e: error_message = f"Network error checking PyPI: {e}" except Exception as e: error_message = f"Error checking version: {e}" self.finished.emit(current_version, latest_version, error_message) class UpdateThread(QThread): update_status = Signal(str) # For status messages update_finished = Signal(bool, str) # success (bool), message/error (str) def run(self): error_message = "" success = False try: self.update_status.emit("Starting update process...") # Determine paths (similar logic as before) python_path = sys.executable # Default to current interpreter yt_dlp_dir = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else os.getcwd() if getattr(sys, 'frozen', False) and sys.platform == 'win32': alt_python_path = os.path.join(os.path.dirname(sys.executable), 'python.exe') if os.path.exists(alt_python_path): python_path = alt_python_path # Create and configure QProcess process = QProcess() process.setWorkingDirectory(yt_dlp_dir) process.setProcessChannelMode(QProcess.ProcessChannelMode.MergedChannels) # Combine stdout/stderr # Prepare command arguments pip_args = ['install', '--upgrade', '--no-cache-dir', 'yt-dlp'] if sys.platform == 'win32': command = python_path args = ['-m', 'pip'] + pip_args else: # Assume pip is in PATH or use python -m pip for robustness command = python_path args = ['-m', 'pip'] + pip_args # Alternative if pip is guaranteed in PATH: command = 'pip', args = pip_args # Start the process self.update_status.emit(f"Running: {command} {' '.join(args)}") process.start(command, args) # Wait for finish (use QProcess event loop, not blocking waitForFinished) if not process.waitForStarted(5000): # Wait 5s for process to start raise RuntimeError("Update process failed to start.") if not process.waitForFinished(-1): # Wait indefinitely for finish raise RuntimeError("Update process failed to finish.") exit_code = process.exitCode() output = process.readAll().data().decode(errors='ignore') # Read combined output if exit_code == 0: self.update_status.emit("Update completed successfully!") success = True error_message = "Update successful. Please restart the application." else: self.update_status.emit(f"Update failed (Exit Code: {exit_code})") error_message = f"Update failed.\nExit Code: {exit_code}\nOutput:\n{output}" success = False except Exception as e: error_message = f"Update failed with exception: {e}" self.update_status.emit(error_message) success = False self.update_finished.emit(success, error_message) class YTDLPUpdateDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle("Update yt-dlp") self.setMinimumWidth(400) layout = QVBoxLayout(self) # Status label self.status_label = QLabel("Checking for updates...") self.status_label.setAlignment(Qt.AlignmentFlag.AlignCenter) layout.addWidget(self.status_label) # Progress bar self.progress_bar = QProgressBar() self.progress_bar.hide() # Hide initially layout.addWidget(self.progress_bar) # Buttons button_layout = QHBoxLayout() self.update_btn = QPushButton("Update") self.update_btn.clicked.connect(self.perform_update) self.update_btn.setEnabled(False) self.close_btn = QPushButton("Close") self.close_btn.clicked.connect(self.close) button_layout.addWidget(self.update_btn) button_layout.addWidget(self.close_btn) layout.addLayout(button_layout) # Style self.setStyleSheet(""" QDialog { background-color: #15181b; } QLabel { color: #ffffff; font-size: 12px; padding: 10px; } QPushButton { padding: 8px 15px; background-color: #c90000; border: none; border-radius: 4px; color: white; font-weight: bold; min-width: 100px; } QPushButton:disabled { background-color: #666666; } QPushButton:hover { background-color: #a50000; } QProgressBar { border: 2px solid #1d1e22; border-radius: 4px; text-align: center; color: white; background-color: #1d1e22; height: 25px; } QProgressBar::chunk { background-color: #c90000; border-radius: 2px; } """) # Start version check in background self.check_version() def check_version(self): self.status_label.setText("Checking for updates...") self.update_btn.setEnabled(False) self.version_check_thread = VersionCheckThread() self.version_check_thread.finished.connect(self.on_version_check_finished) self.version_check_thread.start() def on_version_check_finished(self, current_version, latest_version, error_message): if error_message: self.status_label.setText(error_message) self.update_btn.setEnabled(False) return if not current_version or not latest_version: self.status_label.setText("Could not determine versions.") self.update_btn.setEnabled(False) return try: # Compare versions current_ver = version.parse(current_version) latest_ver = version.parse(latest_version) if current_ver < latest_ver: self.status_label.setText(f"Update available!\nCurrent version: {current_version}\nLatest version: {latest_version}") self.update_btn.setEnabled(True) else: self.status_label.setText(f"yt-dlp is up to date (version {current_version})") self.update_btn.setEnabled(False) except version.InvalidVersion: # If version parsing fails, do a simple string comparison if current_version != latest_version: self.status_label.setText(f"Update available! (Comparison failed)\nCurrent: {current_version}\nLatest: {latest_version}") self.update_btn.setEnabled(True) else: self.status_label.setText(f"yt-dlp is up to date (version {current_version})") self.update_btn.setEnabled(False) except Exception as e: # Catch any other unexpected errors during comparison self.status_label.setText(f"Error comparing versions: {e}") self.update_btn.setEnabled(False) def perform_update(self): self.update_btn.setEnabled(False) self.close_btn.setEnabled(False) self.status_label.setText("Initializing update...") self.progress_bar.setRange(0, 0) # Indeterminate progress self.progress_bar.show() # Create and start the update thread self.update_thread = UpdateThread() self.update_thread.update_status.connect(self.on_update_status) # Connect status signal self.update_thread.update_finished.connect(self.on_update_finished) # Connect finished signal self.update_thread.start() def on_update_status(self, message): """Slot to receive status messages from UpdateThread.""" self.status_label.setText(message) def on_update_finished(self, success, message): """Slot called when the UpdateThread finishes.""" self.progress_bar.setRange(0, 100) # Set determinate range self.progress_bar.setValue(100) # Mark as complete self.progress_bar.hide() # Optionally hide progress bar again self.status_label.setText(message) self.close_btn.setEnabled(True) if success: # Optionally re-check version automatically after successful update self.check_version() else: # Re-enable update button only if failed? # self.update_btn.setEnabled(True) # Decide if appropriate pass # Keep update button disabled on failure for now def closeEvent(self, event): """Ensure threads are terminated if the dialog is closed prematurely.""" if hasattr(self, 'version_check_thread') and self.version_check_thread.isRunning(): self.version_check_thread.quit() # Ask thread to stop self.version_check_thread.wait() # Wait for it to finish if hasattr(self, 'update_thread') and self.update_thread.isRunning(): self.update_thread.quit() self.update_thread.wait() super().closeEvent(event) class AboutDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.parent = parent # Store parent to access version etc. self.setWindowTitle("About YTSage") self.setMinimumWidth(450) layout = QVBoxLayout(self) layout.setSpacing(15) layout.setContentsMargins(20, 20, 20, 20) # Title and Version title_label = QLabel("