feat: OCR resolution optimization - native resolution up to 1920px

- 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
This commit is contained in:
Accusys
2026-09-17 22:18:33 +08:00
parent 474a5bee99
commit 9ffbfeb6c5
+18 -2
View File
@@ -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()