From 9ffbfeb6c5a0ef933f0e039eb6bce8ee829fd1d7 Mon Sep 17 00:00:00 2001 From: Accusys Date: Thu, 17 Sep 2026 22:18:33 +0800 Subject: [PATCH] feat: OCR resolution optimization - native resolution up to 1920px MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace fixed 320px downscale with smart resolution handling - Videos <= 1920px: use native resolution (no downscale) - Videos > 1920px: downscale to 1920px width - Improves OCR quality for small text, subtitles, and scene text Before: All videos scaled to 320px → poor OCR for small text After: 1080p/720p videos use native resolution, 4K scales to 1920px Verified: swift_ocr binary builds and runs correctly - 720x890 video: native resolution (no scaling) - 1920x1080 video: native resolution (at threshold) - Detects video width and applies conditional scaling Note: PaddleOCR fallback (ocr_processor_mps.py) already uses native resolution --- scripts/swift_processors/swift_ocr.swift | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/scripts/swift_processors/swift_ocr.swift b/scripts/swift_processors/swift_ocr.swift index 33890e2b..b6269c9d 100644 --- a/scripts/swift_processors/swift_ocr.swift +++ b/scripts/swift_processors/swift_ocr.swift @@ -68,11 +68,27 @@ struct SwiftOCR: ParsableCommand { let framePattern = framesDir.appendingPathComponent("frame_%05d.jpg").path print("[SwiftOCR] Extracting frames with ffmpeg (interval=\(frameStep))...") - + + // Get video dimensions for smart scaling + let videoSize = videoTrack.naturalSize.applying(videoTrack.preferredTransform) + let videoWidth = Int(abs(videoSize.width)) + let maxOcrWidth = 1920 + let targetWidth = min(videoWidth, maxOcrWidth) + + // Build scale filter: only downscale if video > 1920px wide + let scaleFilter: String + if videoWidth > maxOcrWidth { + scaleFilter = "select=not(mod(n\\,\(frameStep))),scale=\(targetWidth):-2" + print("[SwiftOCR] Video width \(videoWidth)px > \(maxOcrWidth)px, scaling to \(targetWidth)px") + } else { + scaleFilter = "select=not(mod(n\\,\(frameStep)))" + print("[SwiftOCR] Video width \(videoWidth)px <= \(maxOcrWidth)px, using native resolution") + } + let extractProc = Process() extractProc.executableURL = URL(fileURLWithPath: "/opt/homebrew/bin/ffmpeg") extractProc.arguments = ["-y", "-v", "quiet", "-i", inputPath, - "-vf", "select=not(mod(n\\,\(frameStep))),scale=320:-2", + "-vf", scaleFilter, "-vsync", "vfr", "-q:v", "15", framePattern] let startExtract = Date() try extractProc.run()