From f6832f58a6f59f099e5437475c112bd9e04cd5ed Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Mon, 6 Apr 2026 12:06:32 +0200 Subject: [PATCH] feat: ExportWorker with ffmpeg command builder Co-Authored-By: Claude Sonnet 4.6 --- main.py | 36 ++++++++++++++++++++++++++++++++++++ tests/test_utils.py | 12 +++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 1cd1d55..eaf5ade 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,7 @@ import os +import subprocess import sys +from PyQt6.QtCore import QThread, pyqtSignal from PyQt6.QtWidgets import QApplication, QMainWindow @@ -14,6 +16,40 @@ def format_time(seconds: float) -> str: return f"{m}:{s:04.1f}" +def build_ffmpeg_command(input_path: str, start: float, output_path: str) -> list: + return [ + "ffmpeg", "-y", + "-ss", str(start), + "-i", input_path, + "-t", "8", + "-c:v", "libx264", + "-c:a", "aac", + output_path, + ] + + +class ExportWorker(QThread): + finished = pyqtSignal(str) # output path + error = pyqtSignal(str) # error message + + def __init__(self, input_path: str, start: float, output_path: str): + super().__init__() + self._input = input_path + self._start = start + self._output = output_path + + def run(self): + cmd = build_ffmpeg_command(self._input, self._start, self._output) + try: + result = subprocess.run(cmd, capture_output=True, text=True, timeout=120) + if result.returncode == 0: + self.finished.emit(self._output) + else: + self.error.emit(result.stderr[-500:]) + except Exception as e: + self.error.emit(str(e)) + + def main(): app = QApplication(sys.argv) win = MainWindow() diff --git a/tests/test_utils.py b/tests/test_utils.py index 375e8e9..3a62349 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,4 +1,4 @@ -from main import build_export_path, format_time +from main import build_export_path, format_time, build_ffmpeg_command def test_build_export_path_first(): @@ -21,3 +21,13 @@ def test_format_time_rounding(): def test_format_time_no_sixty_rollover(): assert format_time(59.95) == "0:59.9" + + +def test_ffmpeg_command(): + cmd = build_ffmpeg_command("/in/video.mp4", 12.5, "/out/clip_001.mp4") + assert cmd[0] == "ffmpeg" + assert "-ss" in cmd + assert str(12.5) in cmd + assert "-t" in cmd + assert "8" in cmd + assert cmd[-1] == "/out/clip_001.mp4"