Repository Public License
1// ORIX Convert — Packages/EngineMedia/Tests/EngineMediaTests/VideoTests.swift2// Description: Video engine tests: every declared path is converted and validated; container/codec matrix, remux3// losslessness, trim, subtitles, HDR, hardware truthfulness, cancellation, malformed and hostile input.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import Testing7import VideoToolbox8import CoreMedia9import ConvertCore10@testable import EngineMedia1112extension ConversionPath: @retroactive CustomTestStringConvertible {13 public var testDescription: String { id }14}1516struct CodecCase: Sendable, CustomTestStringConvertible {17 let container: FormatID18 let video: String?19 let audio: String?20 var encoder: String = "auto"21 var testDescription: String { "\(container.rawValue) v=\(video ?? "-") a=\(audio ?? "-") enc=\(encoder)" }22}2324@Suite(.serialized) struct MediaTests {2526@Suite(.serialized) struct VideoPaths {27 static let engine = VideoEngine()28 static let conversions = engine.paths.filter { $0.operation == nil && $0.destination != .gif && $0.destination != .apng }29 static let animations = engine.paths.filter { $0.operation == nil && ($0.destination == .gif || $0.destination == .apng) }30 static let remuxes = engine.paths.filter { $0.operation == VideoEngine.remuxOperation }31 static let audioExtractions = engine.paths.filter { $0.operation == VideoEngine.extractAudioOperation }32 static let subtitleExtractions = engine.paths.filter { $0.operation == VideoEngine.extractSubtitlesOperation }3334 @Test func everyDeclaredPathBelongsToATestedFamily() {35 let all = Self.engine.paths36 #expect(all.count == Self.conversions.count + Self.animations.count + Self.remuxes.count + Self.audioExtractions.count + Self.subtitleExtractions.count)37 #expect(Self.conversions.count == 121)38 #expect(Set(all.map(\.id)).count == all.count, "path ids must be unique")39 #expect(all.allSatisfy { $0.engine == "video" && !$0.limitations.isEmpty })40 #expect(Self.remuxes.allSatisfy { $0.fidelity == .lossless })41 }4243 @Test(arguments: conversions) func conversion(_ path: ConversionPath) async throws {44 let source = try await Fixtures.shared.video(path.source)45 let r = try await T.convert(source, path)46 let container = try #require(MediaTables.videoContainer(path.destination))47 #expect(r.facts["video.codec"] == container.videoCodecs[0])48 #expect(r.facts["audio.tracks"] == "1")49 #expect(r.progress.contains { ($0 ?? 0) > 0 }, "a measured progress fraction was reported")50 #expect(r.progress.last == .some(1))51 }5253 static let codecCases: [CodecCase] = {54 var out: [CodecCase] = []55 for c in MediaTables.videoContainers {56 for v in c.videoCodecs { out.append(CodecCase(container: c.format, video: v, audio: nil)) }57 for a in c.audioCodecs { out.append(CodecCase(container: c.format, video: nil, audio: a)) }58 }59 // Encoder choice, where both a hardware and a software encoder really exist.60 for (container, codec) in [(FormatID.mp4, "h264"), (.mp4, "hevc"), (.mov, "prores")] where MediaHardware.capability(for: codec)?.offersEncoderChoice == true {61 out.append(CodecCase(container: container, video: codec, audio: nil, encoder: "hardware"))62 out.append(CodecCase(container: container, video: codec, audio: nil, encoder: "software"))63 }64 return out65 }()6667 @Test(arguments: codecCases) func containerCodecPair(_ c: CodecCase) async throws {68 let source = try await Fixtures.shared.masterVideo()69 var options: [String: OptionValue] = ["video.encoder": .string(c.encoder)]70 if let v = c.video { options["video.codec"] = .string(v) }71 if let a = c.audio { options["audio.codec"] = .string(a) }72 let r = try await T.convert(source, try T.videoPath(.mp4, c.container), options: options)73 let info = try await T.probe(r.url)74 let container = try #require(MediaTables.videoContainer(c.container))75 #expect(info.video.first?.codec == (c.video ?? container.videoCodecs[0]))76 #expect(info.audio.first?.codec == (c.audio ?? container.audioCodecs[0]))77 if c.encoder == "hardware" { #expect(r.facts["video.backend"] == "hardware"); #expect(r.facts["video.encoder"]?.contains("hardware") == true) }78 if c.encoder == "software" { #expect(r.facts["video.backend"] == "software"); #expect(r.facts["video.encoder"]?.contains("hardware") == false) }79 }8081 @Test(arguments: remuxes) func remuxIsLossless(_ path: ConversionPath) async throws {82 let s = try #require(MediaTables.videoContainer(path.source)), d = try #require(MediaTables.videoContainer(path.destination))83 let codecs = try #require(VideoEngine.remuxCodecs(from: s, to: d))84 let source = try await Fixtures.shared.video(path.source, videoCodec: codecs.video, audioCodec: codecs.audio)85 let r = try await T.convert(source, path)86 #expect(r.facts["video.backend"] == "stream copy")87 let a = try await T.probe(source, countPackets: true), b = try await T.probe(r.url, countPackets: true)88 #expect(a.video.first?.codec == b.video.first?.codec)89 #expect(a.audio.first?.codec == b.audio.first?.codec)90 #expect(a.video.first?.packetCount == b.video.first?.packetCount, "same number of coded video packets")91 // Decoded pictures must be identical, frame for frame.92 let md5a = try await T.decodedMD5(source, stream: "v:0", extra: ["-fps_mode", "passthrough"])93 let md5b = try await T.decodedMD5(r.url, stream: "v:0", extra: ["-fps_mode", "passthrough"])94 #expect(md5a == md5b, "decoded video differs after remux \(path.id)")95 // The coded audio packets must be the same bytes. Where a container re-frames packets, fall back to comparing96 // the decoded samples (identical apart from encoder-delay signalling, which differs between containers).97 // The MP4 muxer omits a final AAC frame that is encoder padding only (all of its samples are marked to be98 // discarded), so one packet less is accepted there; the decoded sound is compared below in that case.99 let na = a.audio.first?.packetCount ?? -1, nb = b.audio.first?.packetCount ?? -2100 #expect(na == nb || (na - nb == 1 && a.audio.first?.codec == "aac"), "coded audio packets: \(na) in the source, \(nb) after remux")101 let ha = try await T.packetMD5(source, stream: "a:0", codec: a.audio.first?.codec), hb = try await T.packetMD5(r.url, stream: "a:0", codec: b.audio.first?.codec)102 if ha != hb {103 let pa = try await T.pcm16(source), pb = try await T.pcm16(r.url)104 #expect(Self.alignedEqual(pa, pb, maxShift: 8192), "coded and decoded audio both differ after remux \(path.id)")105 }106 }107108 /// `a` and `b` hold the same samples once one is shifted by at most `maxShift` samples (priming/padding).109 static func alignedEqual(_ a: [Int16], _ b: [Int16], maxShift: Int) -> Bool {110 guard a.count > maxShift * 2, b.count > maxShift * 2 else { return a == b }111 let window = 4096112 func matches(_ x: [Int16], _ y: [Int16], shift: Int) -> Bool {113 // x[i + shift] == y[i] over the overlap114 let n = min(x.count - shift, y.count)115 guard n > window else { return false }116 let start = max(0, n / 2 - window)117 for i in start..<(start + window) where x[i + shift] != y[i] { return false }118 let tail = max(0, n - maxShift * 2)119 for i in 0..<tail where x[i + shift] != y[i] { return false }120 return true121 }122 for shift in stride(from: 0, through: maxShift, by: 1) where matches(a, b, shift: shift) || matches(b, a, shift: shift) { return true }123 return false124 }125126 @Test(arguments: audioExtractions) func extractAudio(_ path: ConversionPath) async throws {127 let source = try await Fixtures.shared.video(path.source)128 let r = try await T.convert(source, path)129 let info = try await T.probe(r.url)130 #expect(info.video.isEmpty && info.audio.count == 1)131 }132133 @Test func extractAudioCopiesTheStreamWhenCodecsMatch() async throws {134 let source = try await Fixtures.shared.masterVideo()135 let r = try await T.convert(source, try T.videoPath(.mp4, .m4a, VideoEngine.extractAudioOperation))136 #expect(r.facts["audio.mode"] == "copy")137 let src = try await T.probe(source, countPackets: true), out = try await T.probe(r.url, countPackets: true)138 #expect(src.audio.first?.packetCount == out.audio.first?.packetCount)139 #expect(try await T.decodedMD5(source, stream: "a:0") == T.decodedMD5(r.url, stream: "a:0"))140 // A setting that changes the sound forces a re-encode, and says so.141 let re = try await T.convert(source, try T.videoPath(.mp4, .m4a, VideoEngine.extractAudioOperation), options: ["audio.channels": .string("mono")])142 #expect(re.facts["audio.mode"] == "re-encode")143 #expect(try await T.probe(re.url).audio.first?.channels == 1)144 // Second audio track of a multi-track file.145 let rich = try await Fixtures.shared.richMKV()146 let second = try await T.convert(rich, try T.videoPath(.mkv, .wav, VideoEngine.extractAudioOperation), options: ["audio.track": .int(2)])147 #expect(try await T.probe(second.url).audio.first?.channels == 6)148 await #expect(throws: ConversionError.self) {149 _ = try await T.convert(rich, try T.videoPath(.mkv, .wav, VideoEngine.extractAudioOperation), options: ["audio.track": .int(5)])150 }151 }152153 @Test(arguments: subtitleExtractions) func extractSubtitles(_ path: ConversionPath) async throws {154 let source = try await Fixtures.shared.subtitled(path.source)155 let r = try await T.convert(source, path)156 let text = try String(contentsOf: r.url, encoding: .utf8)157 #expect(text.contains("Hello ORIX") && text.contains("Second line"))158 }159160 @Test(arguments: animations) func animatedImage(_ path: ConversionPath) async throws {161 let source = try await Fixtures.shared.video(path.source)162 let r = try await T.convert(source, path, options: ["trim.end": .double(1.5), "gif.width": .int(160)])163 let info = try await T.probe(r.url, countPackets: true)164 #expect(info.video.first?.width == 160 && info.video.first?.height == 120)165 #expect((info.video.first?.packetCount ?? 0) >= 16)166 #expect(r.hasWarning("audio-removed"))167 }168169 @Test func gifUsesAnOptimisedPalette() async throws {170 let source = try await Fixtures.shared.masterVideo()171 let r = try await T.convert(source, try T.videoPath(.mp4, .gif), options: ["gif.fps": .int(10), "gif.width": .int(0)])172 #expect(r.facts["video.encoder"]?.contains("palettegen") == true)173 #expect(r.facts["video.size"] == "320×240")174 let frames = try await T.probe(r.url, countPackets: true).video.first?.packetCount ?? 0175 #expect(abs(frames - 30) <= 2)176 // No palette file is left behind in the work directory.177 let leftovers = try FileManager.default.contentsOfDirectory(atPath: r.url.deletingLastPathComponent().path)178 #expect(leftovers.count == 1)179 }180}181182@Suite(.serialized) struct VideoFeatures {183 @Test func resolutionPresetsAndCustomSizes() async throws {184 let source = try await Fixtures.shared.masterVideo() // 320×240185 let path = try T.videoPath(.mp4, .mp4)186 var r = try await T.convert(source, path, options: ["resize.preset": .string("custom"), "resize.width": .int(161)])187 var v = try #require(try await T.probe(r.url).video.first)188 #expect(v.width == 162 && v.height == 120, "161 px wide keeps 4:3 and is rounded to even dimensions (got \(v.width)×\(v.height))")189 r = try await T.convert(source, path, options: ["resize.preset": .string("custom"), "resize.width": .int(200), "resize.height": .int(200), "resize.keepaspect": .bool(false)])190 v = try #require(try await T.probe(r.url).video.first)191 #expect(v.width == 200 && v.height == 200)192 r = try await T.convert(source, path, options: ["resize.preset": .string("720")])193 v = try #require(try await T.probe(r.url).video.first)194 #expect(v.width == 320 && v.height == 240 && r.hasWarning("not-upscaled"))195 // A real downscale by preset: 640×480 source → 360p.196 let big = T.newDirectory("big").appendingPathComponent("big.mp4")197 try await T.ffmpeg(["-filter_complex", "testsrc2=size=640x480:rate=25:duration=1[v]", "-map", "[v]", "-c:v", "h264_videotoolbox", "-q:v", "60", "-f", "mp4", "file:" + big.path])198 r = try await T.convert(big, path, options: ["resize.preset": .string("360")])199 v = try #require(try await T.probe(r.url).video.first)200 #expect(v.width == 480 && v.height == 360)201 await #expect(throws: ConversionError.self) { _ = try await T.convert(source, path, options: ["resize.preset": .string("custom")]) }202 }203204 @Test func oddDimensionsAreMadeEven() async throws {205 let odd = T.newDirectory("odd").appendingPathComponent("odd.mkv")206 try await T.ffmpeg(["-filter_complex", "testsrc2=size=321x241:rate=25:duration=1,format=yuvj444p[v]", "-map", "[v]", "-c:v", "mjpeg", "-q:v", "3", "-f", "matroska", "file:" + odd.path])207 let src = try #require(try await T.probe(odd).video.first)208 #expect(src.width == 321 && src.height == 241, "the fixture really has odd dimensions")209 let r = try await T.convert(odd, try T.videoPath(.mkv, .mp4))210 let v = try #require(try await T.probe(r.url).video.first)211 #expect(v.width == 322 && v.height == 242 && r.hasWarning("odd-dimension-adjusted"))212 }213214 @Test func frameRateConversionAndRotation() async throws {215 let source = try await Fixtures.shared.masterVideo()216 var r = try await T.convert(source, try T.videoPath(.mp4, .mkv), options: ["fps": .string("24000/1001")])217 var v = try #require(try await T.probe(r.url, countPackets: true).video.first)218 #expect(abs((v.frameRate ?? 0) - 23.976) < 0.01)219 #expect(abs((v.packetCount ?? 0) - 72) <= 1, "3 s at 23.976 fps is 72 frames, got \(v.packetCount ?? 0)")220 r = try await T.convert(source, try T.videoPath(.mp4, .mp4), options: ["video.rotate": .string("90")])221 v = try #require(try await T.probe(r.url).video.first)222 #expect(v.width == 240 && v.height == 320 && v.rotation == 0)223 // MPEG-2 only signals standard rates: 30 fps stays, 15 fps is moved to the nearest one with a warning.224 let slow = T.newDirectory("slow").appendingPathComponent("slow.mp4")225 try await T.ffmpeg(["-filter_complex", "testsrc2=size=320x240:rate=15:duration=2[v]", "-map", "[v]", "-c:v", "h264_videotoolbox", "-q:v", "60", "-f", "mp4", "file:" + slow.path])226 r = try await T.convert(slow, try T.videoPath(.mp4, .mpeg))227 #expect(r.hasWarning("frame-rate-changed"))228 #expect(abs((try await T.probe(r.url).video.first?.frameRate ?? 0) - 23.976) < 0.01)229 }230231 @Test func rotationFlagIsAppliedWhenReencoding() async throws {232 let source = try await Fixtures.shared.masterVideo()233 let flagged = T.newDirectory("rot").appendingPathComponent("flagged.mp4")234 try await T.ffmpeg(["-display_rotation:v:0", "90", "-i", "file:" + source.path, "-c", "copy", "-f", "mp4", "file:" + flagged.path])235 let info = try await VideoEngine().inspect(flagged, format: .mp4)236 #expect(info.fields.contains { $0.group == "Video" && $0.key == "Rotation" })237 let r = try await T.convert(flagged, try T.videoPath(.mp4, .mkv))238 let v = try #require(try await T.probe(r.url).video.first)239 #expect(v.width == 240 && v.height == 320 && v.rotation == 0, "the picture is stored upright")240 }241242 @Test func bitrateControlAndTwoPass() async throws {243 let source = try await Fixtures.shared.masterVideo()244 let small = try await T.convert(source, try T.videoPath(.mp4, .webm), options: ["video.rate.mode": .string("bitrate"), "video.bitrate": .int(150), "video.twopass": .bool(true), "audio.tracks": .string("none")])245 let large = try await T.convert(source, try T.videoPath(.mp4, .webm), options: ["video.rate.mode": .string("bitrate"), "video.bitrate": .int(1500), "video.twopass": .bool(true), "audio.tracks": .string("none")])246 let a = try #require(try small.url.resourceValues(forKeys: [.fileSizeKey]).fileSize), b = try #require(try large.url.resourceValues(forKeys: [.fileSizeKey]).fileSize)247 #expect(Double(b) > Double(a) * 2, "1500 kb/s must be clearly larger than 150 kb/s (\(a) vs \(b) bytes)")248 #expect(small.log.contains { $0.contains("-pass 1") } && small.log.contains { $0.contains("-pass 2") })249 #expect(try FileManager.default.contentsOfDirectory(atPath: small.url.deletingLastPathComponent().path).count == 1, "pass log removed")250 #expect(try await T.probe(small.url).audio.isEmpty)251 // Hardware encoders have no two-pass mode: say so instead of pretending.252 let hw = try await T.convert(source, try T.videoPath(.mp4, .mp4), options: ["video.rate.mode": .string("bitrate"), "video.bitrate": .int(400), "video.twopass": .bool(true)])253 #expect(hw.hasWarning("two-pass-unavailable"))254 // Quality presets order file sizes.255 let archive = try await T.convert(source, try T.videoPath(.mp4, .mkv), options: ["quality.preset": .string("archive"), "audio.tracks": .string("none")])256 let tiny = try await T.convert(source, try T.videoPath(.mp4, .mkv), options: ["quality.preset": .string("small"), "audio.tracks": .string("none")])257 let sa = try #require(try archive.url.resourceValues(forKeys: [.fileSizeKey]).fileSize), st = try #require(try tiny.url.resourceValues(forKeys: [.fileSizeKey]).fileSize)258 #expect(sa > st, "Archive (\(sa) bytes) must be larger than Small (\(st) bytes)")259 }260261 @Test func trimIsFrameAccurateWhenReencoding() async throws {262 let source = try await Fixtures.shared.masterVideo()263 let r = try await T.convert(source, try T.videoPath(.mp4, .mkv), options: ["trim.start": .double(0.5), "trim.end": .double(2.0)])264 let info = try await T.probe(r.url, countPackets: true)265 #expect(info.video.first?.packetCount == 45, "1.5 s at 30 fps is exactly 45 frames, got \(info.video.first?.packetCount ?? -1)")266 #expect(abs((info.duration ?? 0) - 1.5) < 0.08)267 #expect(r.facts["trim.accuracy"] == "frame")268 // The first frame of the trimmed file is the frame at 0.5 s of the source. The re-encode is lossy, so frame identity is checked numerically rather than by hash.269 let fa = try await MediaQualityLab.videoFrame(of: source, atSeconds: 0.5, maxDimension: 64)270 let fb = try await MediaQualityLab.videoFrame(of: r.url, atSeconds: 0, maxDimension: 64)271 let fc = try await MediaQualityLab.videoFrame(of: source, atSeconds: 1.5, maxDimension: 64)272 #expect(Self.meanAbsDiff(fa, fb) < 12, "first trimmed frame matches the source frame at 0.5 s")273 #expect(Self.meanAbsDiff(fa, fb) < Self.meanAbsDiff(fc, fb), "and is closer to it than to a frame one second later")274 await #expect(throws: ConversionError.self) { _ = try await T.convert(source, try T.videoPath(.mp4, .mkv), options: ["trim.start": .double(2), "trim.end": .double(1)]) }275 await #expect(throws: ConversionError.self) { _ = try await T.convert(source, try T.videoPath(.mp4, .mkv), options: ["trim.start": .double(99)]) }276 }277278 static func meanAbsDiff(_ a: CGImage, _ b: CGImage) -> Double {279 func bytes(_ image: CGImage) -> [UInt8] {280 let w = 32, h = 24281 var data = [UInt8](repeating: 0, count: w * h)282 data.withUnsafeMutableBytes { raw in283 if let ctx = CGContext(data: raw.baseAddress, width: w, height: h, bitsPerComponent: 8, bytesPerRow: w, space: CGColorSpaceCreateDeviceGray(), bitmapInfo: CGImageAlphaInfo.none.rawValue) {284 ctx.draw(image, in: CGRect(x: 0, y: 0, width: w, height: h))285 }286 }287 return data288 }289 let x = bytes(a), y = bytes(b)290 return zip(x, y).reduce(0.0) { $0 + abs(Double($1.0) - Double($1.1)) } / Double(x.count)291 }292293 @Test func trimIsKeyframeLimitedWhenStreamCopying() async throws {294 let source = try await Fixtures.shared.masterVideo() // keyframe every 15 frames (0.5 s)295 let r = try await T.convert(source, try T.videoPath(.mp4, .mkv, VideoEngine.remuxOperation), options: ["trim.start": .double(1.2), "trim.end": .double(2.5)])296 #expect(r.hasWarning("trim-keyframe-limited"))297 #expect(r.facts["trim.accuracy"] == "keyframe")298 let d = try #require(try await T.probe(r.url).duration)299 #expect(d >= 1.2 && d <= 1.9, "requested 1.3 s; a keyframe-limited cut may start up to one GOP (0.5 s) earlier, got \(d)")300 }301302 @Test func remuxRefusesIncompatibleCodecs() async throws {303 let webm = try await Fixtures.shared.video(.webm) // VP9 + Opus304 do {305 _ = try await T.convert(webm, try T.videoPath(.mkv, .ts, VideoEngine.remuxOperation))306 Issue.record("VP9 cannot be stream-copied into an MPEG transport stream")307 } catch let error as ConversionError {308 guard case .invalidOption(let message) = error else { Issue.record("unexpected error \(error)"); return }309 #expect(message.contains("without re-encoding"))310 }311 // AVI is never a remux destination (see MediaTables): the path is simply not declared.312 #expect(!VideoEngine().paths.contains { $0.operation == VideoEngine.remuxOperation && $0.destination == .avi })313 let inspection = try await VideoEngine().inspect(webm, format: .mkv)314 let warnings = VideoEngine().preflight(ConversionRequest(source: webm, sourceFormat: .mkv, destinationFormat: .ts, operation: VideoEngine.remuxOperation), inspection: inspection)315 #expect(warnings.contains { $0.code == "remux-incompatible" })316 }317318 // Regression (2026-11-21): `video:mpeg>mp4#video.remux` could not pass in the cross-engine sweep, whose only319 // MPEG-PS fixture carried MP2 sound. The path is honest (MPEG-PS with AC-3 or MP3 sound does go into MP4 as it320 // is) but content-dependent: preflight must say so for this file, before converting, with the codec named.321 @Test func remuxIntoMP4DependsOnTheSoundOfTheProgramStream() async throws {322 let path = try T.videoPath(.mpeg, .mp4, VideoEngine.remuxOperation)323 // 1. What the engine writes by default: MPEG-2 video + MP2 sound. MP4 does not take MP2.324 let mp2 = try await Fixtures.shared.video(.mpeg)325 #expect(try await T.probe(mp2).audio.first?.codec == "mp2")326 let request = ConversionRequest(source: mp2, sourceFormat: .mpeg, destinationFormat: .mp4, operation: VideoEngine.remuxOperation)327 let warnings = VideoEngine().preflight(request, inspection: try await VideoEngine().inspect(mp2, format: .mpeg)).filter { $0.code == "remux-incompatible" }328 #expect(warnings.count == 1)329 let said = try #require(warnings.first?.message)330 #expect(said.contains("MPEG Audio Layer II (MP2) audio cannot be stored in MP4") && said.contains("AAC") && said.contains("normal conversion") && said.contains("will stop"), "\(said)")331 do {332 _ = try await T.convert(mp2, path)333 Issue.record("MP2 sound cannot be stream-copied into MP4")334 } catch let ConversionError.invalidOption(message) {335 // The error is the sentence preflight announced.336 #expect(said.hasPrefix(message), "error: \(message)")337 }338 // Without the sound, the same file does change container.339 #expect(VideoEngine().preflight(ConversionRequest(source: mp2, sourceFormat: .mpeg, destinationFormat: .mp4, operation: VideoEngine.remuxOperation,340 options: ConversionOptions(["audio.tracks": .string("none")])),341 inspection: try await VideoEngine().inspect(mp2, format: .mpeg)).allSatisfy { $0.code != "remux-incompatible" })342 let silent = try await T.convert(mp2, path, options: ["audio.tracks": .string("none")])343 #expect(try await T.probe(silent.url).video.first?.codec == "mpeg2video")344345 // 2. Program streams whose sound MP4 accepts, written by the bundled ffmpeg (not by this engine): they pass,346 // bit for bit, and preflight has nothing to say.347 let master = try await Fixtures.shared.masterVideo()348 for (audioCodec, encoder) in [("mp3", ["-c:a", "libmp3lame", "-b:a", "128k"]), ("ac3", ["-c:a", "ac3", "-b:a", "192k"])] {349 let ps = T.newDirectory("ps-\(audioCodec)").appendingPathComponent("program-\(audioCodec).mpg")350 try await T.ffmpeg(["-i", "file:" + master.path, "-map", "0:v:0", "-map", "0:a:0", "-c:v", "mpeg2video", "-q:v", "4", "-r", "25", "-pix_fmt", "yuv420p"] + encoder + ["-ar", "48000", "-ac", "2", "-f", "vob", "file:" + ps.path])351 let a = try await T.probe(ps, countPackets: true)352 #expect(a.video.first?.codec == "mpeg2video" && a.audio.first?.codec == audioCodec, "fixture: \(a.video.first?.codec ?? "-") + \(a.audio.first?.codec ?? "-")")353 let pre = VideoEngine().preflight(ConversionRequest(source: ps, sourceFormat: .mpeg, destinationFormat: .mp4, operation: VideoEngine.remuxOperation),354 inspection: try await VideoEngine().inspect(ps, format: .mpeg))355 #expect(!pre.contains { $0.code == "remux-incompatible" }, "\(pre.map(\.message))")356 let r = try await T.convert(ps, path)357 #expect(r.facts["video.backend"] == "stream copy")358 let b = try await T.probe(r.url, countPackets: true)359 #expect(b.video.first?.codec == "mpeg2video" && b.audio.first?.codec == audioCodec)360 #expect(a.video.first?.packetCount == b.video.first?.packetCount, "\(audioCodec): same number of coded video packets")361 let md5a = try await T.decodedMD5(ps, stream: "v:0", extra: ["-fps_mode", "passthrough"]), md5b = try await T.decodedMD5(r.url, stream: "v:0", extra: ["-fps_mode", "passthrough"])362 #expect(md5a == md5b, "\(audioCodec): decoded video differs after the container change")363 }364 }365366 // Every declared container change has a real file it works on: one written by this engine with codecs the367 // destination takes as they are (`remuxIsLossless` converts exactly these). A pair for which no such file can be368 // written is not declared.369 @Test func everyDeclaredRemuxPairHasACopyableSource() throws {370 for path in VideoPaths.remuxes {371 let s = try #require(MediaTables.videoContainer(path.source)), d = try #require(MediaTables.videoContainer(path.destination))372 let codecs = try #require(VideoEngine.remuxCodecs(from: s, to: d), "\(path.id)")373 #expect(s.videoCodecs.contains(codecs.video) && s.audioCodecs.contains(codecs.audio), "\(path.id): the engine writes \(codecs) into \(s.format.rawValue)")374 #expect(d.copyVideo?.contains(codecs.video) ?? true, "\(path.id)")375 #expect(d.copyAudio?.contains(codecs.audio) ?? true, "\(path.id)")376 #expect(path.limitations.contains { $0.contains("Works only when the codecs inside the file are allowed") }, "\(path.id) tells the user it depends on the file")377 }378 for s in MediaTables.videoContainers { for d in MediaTables.videoContainers where VideoEngine.remuxCodecs(from: s, to: d) == nil {379 #expect(!VideoPaths.remuxes.contains { $0.source == s.format && $0.destination == d.format }, "\(s.format.rawValue) → \(d.format.rawValue) has no copyable content and must not be declared")380 } }381 }382383 @Test func subtitlesChaptersTracksAndMetadata() async throws {384 let rich = try await Fixtures.shared.richMKV()385 let inspection = try await VideoEngine().inspect(rich, format: .mkv)386 #expect(inspection.videoCodec == "h264" && inspection.pixelWidth == 320 && inspection.pixelHeight == 240)387 #expect(inspection.audioCodec == "aac" && inspection.channels == 2 && inspection.sampleRate == 48000)388 #expect(abs((inspection.durationSeconds ?? 0) - 3) < 0.1 && abs((inspection.frameRate ?? 0) - 30) < 0.01)389 for group in ["General", "Video", "Audio", "Subtitles", "Chapters", "Tags"] {390 #expect(inspection.fields.contains { $0.group == group }, "inspection has a \(group) group")391 }392 #expect(inspection.fields.filter { $0.group == "Subtitles" }.count == 2)393 #expect(inspection.fields.contains { $0.group == "Tags" && $0.key.lowercased() == "artist" && $0.value == "ORIX Tests" })394 #expect(inspection.fields.contains { $0.group == "Audio" && $0.value.contains("6 channels") })395396 // MP4: both text tracks kept as mov_text (ASS styling lost, said so), both audio tracks, chapters, tags.397 var r = try await T.convert(rich, try T.videoPath(.mkv, .mp4))398 var info = try await T.probe(r.url)399 #expect(info.subtitles.map(\.codec) == ["mov_text", "mov_text"] && r.hasWarning("subtitle-styles-lost"))400 #expect(info.audio.count == 2 && info.audio[1].channels == 6)401 #expect(info.chapters.count == 2 && info.chapters.first?.title == "Opening")402 #expect(info.tag("artist") == "ORIX Tests")403 // Matroska keeps the ASS track as ASS.404 r = try await T.convert(rich, try T.videoPath(.mkv, .mkv), options: ["audio.tracks": .string("first")])405 info = try await T.probe(r.url)406 #expect(info.subtitles.map(\.codec) == ["subrip", "ass"] && info.audio.count == 1 && !r.hasWarning("subtitle-styles-lost"))407 // WebM: WebVTT.408 r = try await T.convert(rich, try T.videoPath(.mkv, .webm))409 #expect(try await T.probe(r.url).subtitles.map(\.codec) == ["webvtt", "webvtt"])410 // AVI cannot carry them: dropped, with data-loss warnings; MP3 is stereo at most → 5.1 downmix warning.411 r = try await T.convert(rich, try T.videoPath(.mkv, .avi))412 info = try await T.probe(r.url)413 #expect(info.subtitles.isEmpty && r.hasWarning("subtitles-dropped") && r.hasWarning("chapters-dropped") && r.hasWarning("surround-downmix"))414 #expect(r.warnings.first { $0.code == "subtitles-dropped" }?.severity == .dataLoss)415 let pre = VideoEngine().preflight(ConversionRequest(source: rich, sourceFormat: .mkv, destinationFormat: .avi), inspection: inspection)416 #expect(pre.contains { $0.code == "subtitles-dropped" } && pre.contains { $0.code == "chapters-dropped" })417 // Options: drop subtitles and chapters, strip metadata, set a title and a creation date.418 r = try await T.convert(rich, try T.videoPath(.mkv, .mp4), options: ["subtitles": .string("drop"), "chapters.keep": .bool(false), "metadata.strip": .bool(true),419 "metadata.title": .string("New = title; with 'quotes'"), "metadata.creation": .string("2026-05-17T14:30:00Z")])420 info = try await T.probe(r.url)421 #expect(info.subtitles.isEmpty && info.chapters.isEmpty && info.tag("artist") == nil)422 #expect(info.tag("title") == "New = title; with 'quotes'")423 #expect(info.tag("creation_time")?.hasPrefix("2026-05-17T14:30:00") == true)424 await #expect(throws: ConversionError.self) { _ = try await T.convert(rich, try T.videoPath(.mkv, .mp4), options: ["metadata.creation": .string("yesterday")]) }425 // Remux keeps everything the container can hold.426 r = try await T.convert(rich, try T.videoPath(.mkv, .mp4, VideoEngine.remuxOperation))427 info = try await T.probe(r.url)428 #expect(info.audio.map(\.codec) == ["aac", "ac3"] && info.subtitles.count == 2 && info.chapters.count == 2)429 // Subtitle extraction of the styled track to ASS keeps the style; to SRT it warns.430 let ass = try await T.convert(rich, try T.videoPath(.mkv, .ass, VideoEngine.extractSubtitlesOperation), options: ["subtitle.track": .int(2)])431 #expect(try String(contentsOf: ass.url, encoding: .utf8).contains("Helvetica"))432 let srt = try await T.convert(rich, try T.videoPath(.mkv, .srt, VideoEngine.extractSubtitlesOperation), options: ["subtitle.track": .int(2)])433 let srtText = try String(contentsOf: srt.url, encoding: .utf8)434 #expect(srt.hasWarning("subtitle-styles-lost") && srtText.contains("Styled two"))435 // A file without subtitles is refused clearly.436 let plain = try await Fixtures.shared.masterVideo()437 await #expect(throws: ConversionError.self) { _ = try await T.convert(plain, try T.videoPath(.mp4, .srt, VideoEngine.extractSubtitlesOperation)) }438 }439440 @Test func hdrIsKeptOrToneMappedHonestly() async throws {441 let hdr = try await Fixtures.shared.hdrVideo()442 let inspection = try await VideoEngine().inspect(hdr, format: .mp4)443 #expect(inspection.bitDepth == 10)444 #expect(inspection.fields.contains { $0.key == "HDR" && $0.value.contains("HDR10") })445 #expect(inspection.colorSpace == "bt2020/smpte2084/bt2020nc")446 // HEVC, AV1, VP9 keep 10-bit PQ / BT.2020.447 for (dest, codec) in [(FormatID.mp4, "hevc"), (.mkv, "av1"), (.webm, "vp9"), (.mov, "prores")] {448 let r = try await T.convert(hdr, try T.videoPath(.mp4, dest), options: ["video.codec": .string(codec)])449 let v = try #require(try await T.probe(r.url).video.first)450 #expect(v.colorTransfer == "smpte2084" && v.colorPrimaries == "bt2020" && v.colorSpace == "bt2020nc" && (v.bitDepth ?? 8) >= 10, "\(codec) keeps HDR signalling")451 #expect(!r.hasWarning("hdr-tonemapped"))452 }453 // H.264 cannot: tone-mapped to BT.709 8-bit, with a data-loss warning before and during.454 let pre = VideoEngine().preflight(ConversionRequest(source: hdr, sourceFormat: .mp4, destinationFormat: .mp4), inspection: inspection)455 #expect(pre.contains { $0.code == "hdr-tonemapped" && $0.severity == .dataLoss })456 var r = try await T.convert(hdr, try T.videoPath(.mp4, .mp4))457 var v = try #require(try await T.probe(r.url).video.first)458 #expect(v.codec == "h264" && v.colorTransfer == "bt709" && v.colorPrimaries == "bt709" && v.bitDepth == 8 && r.hasWarning("hdr-tonemapped"))459 #expect(r.facts["video.hdr"]?.contains("tone-mapped") == true)460 // Explicit tone mapping with an HDR-capable codec.461 r = try await T.convert(hdr, try T.videoPath(.mp4, .mp4), options: ["video.codec": .string("hevc"), "video.hdr": .string("tonemap")])462 v = try #require(try await T.probe(r.url).video.first)463 #expect(v.codec == "hevc" && v.colorTransfer == "bt709" && v.bitDepth == 8)464 // Stream copy keeps HDR untouched.465 r = try await T.convert(hdr, try T.videoPath(.mp4, .mkv, VideoEngine.remuxOperation), options: [:])466 v = try #require(try await T.probe(r.url).video.first)467 #expect(v.colorTransfer == "smpte2084" && v.bitDepth == 10)468 }469470 @Test func hardwareCapabilitiesAgreeWithVideoToolbox() async throws {471 let caps = MediaHardware.capabilities()472 #expect(Set(caps.map(\.codec)) == ["h264", "hevc", "prores", "av1", "vp9"])473 var list: CFArray?474 #expect(VTCopyVideoEncoderList(nil, &list) == noErr)475 let encoders = (list as? [[CFString: Any]]) ?? []476 func systemHardware(_ types: [CMVideoCodecType]) -> Bool {477 encoders.contains { e in478 guard let t = (e[kVTVideoEncoderList_CodecType] as? NSNumber)?.uint32Value else { return false }479 return types.contains(t) && (e[kVTVideoEncoderList_IsHardwareAccelerated] as? Bool) == true480 }481 }482 let types: [String: [CMVideoCodecType]] = [483 "h264": [kCMVideoCodecType_H264], "hevc": [kCMVideoCodecType_HEVC], "av1": [kCMVideoCodecType_AV1], "vp9": [kCMVideoCodecType_VP9],484 "prores": [kCMVideoCodecType_AppleProRes422, kCMVideoCodecType_AppleProRes422HQ, kCMVideoCodecType_AppleProRes422LT, kCMVideoCodecType_AppleProRes422Proxy,485 kCMVideoCodecType_AppleProRes4444, kCMVideoCodecType_AppleProRes4444XQ],486 ]487 for cap in caps {488 let codecTypes = try #require(types[cap.codec])489 #expect(cap.systemHardwareEncode == systemHardware(codecTypes), "\(cap.codec) hardware encode matches VTCopyVideoEncoderList")490 #expect(cap.systemHardwareDecode == VTIsHardwareDecodeSupported(codecTypes[0]), "\(cap.codec) hardware decode matches VTIsHardwareDecodeSupported")491 // A hardware encoder is never claimed without VideoToolbox listing one.492 if cap.hardwareEncoder != nil { #expect(cap.systemHardwareEncode) }493 if cap.encodeBackend == .hardware { #expect(cap.encodeSummary.contains("hardware")) } else { #expect(!cap.encodeSummary.hasPrefix("\(cap.displayName) encode: hardware")) }494 }495 // AV1 and VP9 are software encodes in this build whatever the chip offers, and are labelled so.496 let av1 = try #require(caps.first { $0.codec == "av1" })497 #expect(av1.hardwareEncoder == nil && av1.softwareEncoder == "libsvtav1" && av1.encodeBackend == .software && !av1.offersEncoderChoice)498 #expect(av1.encodeSummary.contains("software (SVT-AV1)"))499 let source = try await Fixtures.shared.masterVideo()500 let r = try await T.convert(source, try T.videoPath(.mp4, .mkv), options: ["video.codec": .string("av1")])501 #expect(r.facts["video.backend"] == "software" && r.facts["video.encoder"]?.contains("libsvtav1") == true)502 await #expect(throws: ConversionError.self) {503 _ = try await T.convert(source, try T.videoPath(.mp4, .mkv), options: ["video.codec": .string("av1"), "video.encoder": .string("hardware")])504 }505 // What ran is what is reported: with a hardware H.264 encoder present, the default MP4 encode is hardware.506 let h264 = try #require(caps.first { $0.codec == "h264" })507 let d = try await T.convert(source, try T.videoPath(.mp4, .mp4))508 #expect(d.facts["video.backend"] == (h264.hardwareEncoder != nil ? "hardware" : "software"))509 // The encoder choice is offered only where both exist.510 let offered = VideoEngine().options(for: try T.videoPath(.mp4, .webm)).contains { $0.id == "video.encoder" }511 #expect(!offered, "WebM (VP9/AV1/VP8) has no hardware encoder: no encoder choice")512 let offeredMP4 = VideoEngine().options(for: try T.videoPath(.mp4, .mp4)).contains { $0.id == "video.encoder" }513 #expect(offeredMP4 == (caps.contains { ["h264", "hevc"].contains($0.codec) && $0.offersEncoderChoice }))514 }515516 @Test func cancellationStopsFfmpegAndRemovesPartialOutput() async throws {517 // 20 s of 1280×720 to AV1 at the slowest preset used: long enough to cancel mid-way.518 let long = T.newDirectory("long").appendingPathComponent("long.mp4")519 try await T.ffmpeg(["-filter_complex", "testsrc2=size=1280x720:rate=30:duration=20[v];sine=frequency=300:duration=20[a]", "-map", "[v]", "-map", "[a]",520 "-c:v", "h264_videotoolbox", "-b:v", "4M", "-c:a", "aac_at", "-f", "mp4", "file:" + long.path])521 let work = T.newDirectory("cancel")522 let started = Collector<Double>()523 let request = ConversionRequest(source: long, sourceFormat: .mp4, destinationFormat: .mkv,524 options: ConversionOptions(["video.codec": .string("av1"), "quality.preset": .string("archive")]))525 let context = ConversionContext(workDirectory: work, progress: { if let f = $0.fraction, f > 0 { started.add(f) } })526 let task = Task { try await VideoEngine().convert(request, context: context) }527 let begin = Date()528 while started.all.isEmpty, Date().timeIntervalSince(begin) < 60 { try await Task.sleep(nanoseconds: 50_000_000) }529 #expect(!started.all.isEmpty, "the encode reported progress before being cancelled")530 task.cancel()531 let cancelledAt = Date()532 do {533 _ = try await task.value534 Issue.record("the conversion finished instead of being cancelled")535 } catch is CancellationError {536 } catch {537 Issue.record("expected CancellationError, got \(error)")538 }539 #expect(Date().timeIntervalSince(cancelledAt) < 10, "ffmpeg stopped promptly")540 #expect((started.all.max() ?? 1) < 0.9, "cancelled mid-transcode")541 #expect(try FileManager.default.contentsOfDirectory(atPath: work.path).isEmpty, "no partial output left")542 }543544 @Test func malformedInputIsRefusedClearly() async throws {545 let dir = T.newDirectory("bad")546 let master = try await Fixtures.shared.masterVideo()547 let data = try Data(contentsOf: master)548 var cases: [(String, Data)] = [549 ("random.mp4", Data((0..<50_000).map { _ in UInt8.random(in: 0...255) })),550 ("empty.mkv", Data()),551 ("text.avi", Data("this is not a video\n".utf8)),552 ("truncated.mp4", data.prefix(900)),553 ("html.webm", Data("<html><body>404</body></html>".utf8)),554 ]555 var noMoov = data; noMoov.replaceSubrange(0..<min(64, noMoov.count), with: Data(repeating: 0, count: min(64, noMoov.count)))556 cases.append(("zeroed-header.mp4", noMoov))557 for (name, bytes) in cases {558 let url = dir.appendingPathComponent(name)559 try bytes.write(to: url)560 let format = FormatID(url.pathExtension)561 do {562 _ = try await VideoEngine().inspect(url, format: format)563 Issue.record("\(name) was inspected without error")564 } catch let error as ConversionError {565 guard case .malformedInput = error else { Issue.record("\(name): expected malformedInput, got \(error)"); continue }566 #expect(error.errorDescription?.isEmpty == false)567 }568 let work = T.newDirectory("badout")569 await #expect(throws: ConversionError.self) {570 _ = try await VideoEngine().convert(ConversionRequest(source: url, sourceFormat: format, destinationFormat: .mp4), context: ConversionContext(workDirectory: work))571 }572 #expect(try FileManager.default.contentsOfDirectory(atPath: work.path).isEmpty, "\(name): nothing written")573 }574 // Missing file.575 await #expect(throws: ConversionError.self) { _ = try await VideoEngine().inspect(dir.appendingPathComponent("absent.mp4"), format: .mp4) }576 // A video damaged in the middle converts or fails, but never hangs and never leaves a file on failure.577 var damaged = data578 damaged.replaceSubrange((damaged.count / 2)..<(damaged.count / 2 + 4000), with: Data((0..<4000).map { _ in UInt8.random(in: 0...255) }))579 let durl = dir.appendingPathComponent("damaged.mp4")580 try damaged.write(to: durl)581 let work = T.newDirectory("damagedout")582 do {583 _ = try await VideoEngine().convert(ConversionRequest(source: durl, sourceFormat: .mp4, destinationFormat: .mkv), context: ConversionContext(workDirectory: work))584 } catch {585 #expect(try FileManager.default.contentsOfDirectory(atPath: work.path).isEmpty)586 }587 // An audio-only file is not a video.588 let audio = try await Fixtures.shared.masterAudio()589 await #expect(throws: ConversionError.self) {590 _ = try await VideoEngine().convert(ConversionRequest(source: audio, sourceFormat: .mp4, destinationFormat: .mkv), context: ConversionContext(workDirectory: T.newDirectory("ao")))591 }592 }593594 static let hostileNames = ["-i evil", "-y", "100% %d %03d %s", "a:b;[c]'d\"e,f=g", "file:pipe:0", " leading and trailing ", "émoji 🎬 nouvelle\tvague", "concat:a|b", "$(touch pwned)`id`", "back\\slash"]595596 @Test(arguments: hostileNames) func hostileFileNames(_ name: String) async throws {597 let master = try await Fixtures.shared.masterVideo()598 let dir = T.newDirectory("hostile dir %d -x")599 let source = dir.appendingPathComponent(name).appendingPathExtension("mp4")600 try FileManager.default.copyItem(at: master, to: source)601 // Same hostile text as output name, title and through every operation family.602 let remux = try await T.convert(source, try T.videoPath(.mp4, .mkv, VideoEngine.remuxOperation), baseName: name)603 #expect(remux.url.deletingPathExtension().lastPathComponent == name)604 let transcode = try await T.convert(source, try T.videoPath(.mp4, .mp4), options: ["metadata.title": .string(name), "resize.preset": .string("custom"), "resize.width": .int(160)], baseName: name)605 #expect(try await T.probe(transcode.url).tag("title") == name.trimmingCharacters(in: .whitespacesAndNewlines))606 _ = try await T.convert(source, try T.videoPath(.mp4, .mp3, VideoEngine.extractAudioOperation), baseName: name)607 _ = try await T.convert(source, try T.videoPath(.mp4, .gif), options: ["trim.end": .double(0.5), "gif.width": .int(80)], baseName: name)608 _ = try await MediaQualityLab.videoFrame(of: source, atSeconds: 1)609 // Nothing was created next to the source or in the current directory by a misread argument.610 #expect(try FileManager.default.contentsOfDirectory(atPath: dir.path).count == 1)611 #expect(!FileManager.default.fileExists(atPath: FileManager.default.currentDirectoryPath + "/pwned"))612 }613614 @Test func qualityLabFrameAndWaveformAreRealData() async throws {615 let source = try await Fixtures.shared.masterVideo()616 let frame = try await MediaQualityLab.videoFrame(of: source, atSeconds: 1.0)617 #expect(frame.width == 320 && frame.height == 240)618 let small = try await MediaQualityLab.videoFrame(of: source, atSeconds: 2.9, maxDimension: 100)619 #expect(small.width == 100 && small.height == 75)620 // testsrc2 moves: two different times give different pictures; the same time gives the same picture.621 let again = try await MediaQualityLab.videoFrame(of: source, atSeconds: 1.0)622 let later = try await MediaQualityLab.videoFrame(of: source, atSeconds: 2.0)623 #expect(Self.meanAbsDiff(frame, again) == 0 && Self.meanAbsDiff(frame, later) > 1)624 await #expect(throws: ConversionError.self) { _ = try await MediaQualityLab.videoFrame(of: try await Fixtures.shared.masterAudio(), atSeconds: 0) }625626 // Waveform: 1 s of silence then 1 s of a 0.5-amplitude sine.627 let wav = T.newDirectory("wave").appendingPathComponent("wave.wav")628 try await T.ffmpeg(["-filter_complex", "anullsrc=r=44100:cl=mono:d=1[s];sine=frequency=440:sample_rate=44100:duration=1,volume=4[t];[s][t]concat=n=2:v=0:a=1[o]",629 "-map", "[o]", "-c:a", "pcm_s16le", "-f", "wav", "file:" + wav.path])630 let wave = try await MediaQualityLab.waveform(of: wav, buckets: 200)631 #expect(abs(wave.minimums.count - 200) <= 2 && wave.minimums.count == wave.maximums.count)632 #expect(wave.sampleRate == 44100 && wave.channels == 1 && abs(wave.durationSeconds - 2) < 0.01)633 let half = wave.maximums.count / 2634 #expect(wave.maximums[..<(half - 2)].allSatisfy { abs($0) < 0.001 } && wave.minimums[..<(half - 2)].allSatisfy { abs($0) < 0.001 }, "first second is silent")635 let peak = wave.maximums[(half + 2)...].max() ?? 0, trough = wave.minimums[(half + 2)...].min() ?? 0636 #expect(abs(peak - 0.5) < 0.02 && abs(trough + 0.5) < 0.02, "sine peaks at ±0.5 (got \(trough)…\(peak))")637 }638639 @Test func optionDescriptorsAreConsistent() throws {640 for path in VideoEngine().paths + AudioEngine().paths {641 let engine = T.engine(for: path)642 let options = engine.options(for: path)643 #expect(Set(options.map(\.id)).count == options.count, "\(path.id): duplicate option ids")644 for o in options {645 #expect(o.id == o.id.lowercased() && !o.label.isEmpty)646 if case .choice(let d, let choices) = o.kind { #expect(choices.contains { $0.value == d }, "\(path.id) \(o.id): default \(d) is not a choice") }647 }648 }649 #expect(makeEngines().map(\.id) == ["video", "audio"])650 #expect(VideoEngine().availability() == .available && AudioEngine().availability() == .available)651 }652}653654}655