From d7576c880b9e1619b225ec990cf6a343cdaef78e Mon Sep 17 00:00:00 2001 From: Jamie Banks Date: Fri, 5 Jun 2026 20:23:04 +0100 Subject: [PATCH] feat: implement duration validation check after encoding to ensure file integrity --- internal/queue/worker.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/queue/worker.go b/internal/queue/worker.go index e842bcd..901ef3b 100644 --- a/internal/queue/worker.go +++ b/internal/queue/worker.go @@ -231,12 +231,32 @@ func (m *Manager) runEncoder(job db.Job) error { return fmt.Errorf("ffmpeg error: %v, last output: %s", err, lastErrLine) } - // Validate + // Validate format integrity if err := m.encoder.ValidateFile(tempOutPath); err != nil { os.Remove(tempOutPath) return fmt.Errorf("validation failed: %w", err) } + // Validate duration (ensure it wasn't cut short) + if duration > 0 { + outDuration, err := m.encoder.ProbeDuration(tempOutPath) + if err != nil { + os.Remove(tempOutPath) + return fmt.Errorf("failed to probe output duration: %w", err) + } + + // Allow up to 5 seconds of difference (sometimes containers/padding vary slightly) + diff := duration - outDuration + if diff < 0 { + diff = -diff + } + + if diff > 5.0 { + os.Remove(tempOutPath) + return fmt.Errorf("duration mismatch: original is %.2fs, encoded is %.2fs", duration, outDuration) + } + } + // Calculate sizes outInfo, err := os.Stat(tempOutPath) if err != nil {