SPB Git

Repository Public License

DocTableTests.swift 5.9 KB 89 lines swift

1// ORIX Convert — Packages/EngineMedia/Tests/EngineMediaTests/DocTableTests.swift2// Description: Opt-in generator (ORIX_MEDIA_DOC=<file>) of the path tables of docs/compatibility/engines/media.md,3//              written from the paths the engines really declare so the document cannot drift from the code.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import Testing7import ConvertCore8@testable import EngineMedia910@Suite(.enabled(if: ProcessInfo.processInfo.environment["ORIX_MEDIA_DOC"] != nil))11struct DocTables {12    static func backend(_ p: ConversionPath) -> String {13        if p.engine == "audio" {14            if p.source == .midi { return "AVAudioEngine offline + Apple DLSSynth, then ffmpeg" }15            let t = MediaTables.audioTarget(p.destination)16            return "ffmpeg · " + (t.map { $0.codec == "pcm" ? "PCM" : AudioEncoding.encoderName($0.codec) } ?? "")17        }18        switch p.operation {19        case VideoEngine.remuxOperation: return "ffmpeg · stream copy"20        case VideoEngine.extractAudioOperation:21            let t = MediaTables.audioTarget(p.destination)22            return "ffmpeg · " + (t.map { $0.codec == "pcm" ? "PCM" : AudioEncoding.encoderName($0.codec) } ?? "") + (MediaTables.extractCopyCodec[p.destination] != nil ? " or stream copy" : "")23        case VideoEngine.extractSubtitlesOperation: return "ffmpeg · subtitle codec"24        default:25            if p.destination == .gif { return "ffmpeg · palettegen + paletteuse" }26            if p.destination == .apng { return "ffmpeg · apng" }27            guard let c = MediaTables.videoContainer(p.destination) else { return "ffmpeg" }28            let v = c.videoCodecs[0]29            let cap = MediaHardware.capability(for: v)30            let enc = cap?.hardwareEncoder.map { "\($0) (hardware)" } ?? cap?.softwareEncoder ?? (v == "flv1" ? "flv" : v)31            return "ffmpeg · \(enc) + \(AudioEncoding.encoderName(c.audioCodecs[0]))"32        }33    }3435    static func testName(_ p: ConversionPath) -> String {36        if p.engine == "audio" {37            if p.source == .midi { return "AudioPaths.midiRendering" }38            return AudioPaths.losslessPairs.contains(p) ? "AudioPaths.conversion, losslessPathsAreSampleExact" : "AudioPaths.conversion"39        }40        switch p.operation {41        case VideoEngine.remuxOperation: return "VideoPaths.remuxIsLossless"42        case VideoEngine.extractAudioOperation: return "VideoPaths.extractAudio"43        case VideoEngine.extractSubtitlesOperation: return "VideoPaths.extractSubtitles"44        default: return p.destination == .gif || p.destination == .apng ? "VideoPaths.animatedImage" : "VideoPaths.conversion"45        }46    }47    typealias AudioPaths = MediaTests.AudioPaths4849    @Test func writeTables() throws {50        let file = try #require(ProcessInfo.processInfo.environment["ORIX_MEDIA_DOC"])51        var out = ""52        func table(_ title: String, _ paths: [ConversionPath]) {53            out += "\n### \(title) (\(paths.count) paths)\n\n"54            out += "| Source | Destination | Operation | Status | Fidelity | Backend (default settings) | Preserves | Test |\n|---|---|---|---|---|---|---|---|\n"55            for p in paths {56                let preserves = p.preserves.map(\.rawValue).sorted().joined(separator: ", ")57                out += "| \(p.source.rawValue) | \(p.destination.rawValue) | \(p.operation ?? "convert") | \(p.status.rawValue) | \(p.fidelity.rawValue) | \(Self.backend(p)) | \(preserves.isEmpty ? "—" : preserves) | `\(Self.testName(p))` |\n"58            }59            // Limitations shown to the user: listed once per destination and per distinct set of statements.60            out += "\nLimitations shown to the user before converting:\n\n"61            var groups: [(destination: String, limitations: [String], sources: [String])] = []62            for p in paths {63                if let i = groups.firstIndex(where: { $0.destination == p.destination.rawValue && $0.limitations == p.limitations }) {64                    groups[i].sources.append(p.source.rawValue)65                } else {66                    groups.append((p.destination.rawValue, p.limitations, [p.source.rawValue]))67                }68            }69            for g in groups {70                let all = Set(paths.filter { $0.destination.rawValue == g.destination }.map(\.source.rawValue)).count == g.sources.count71                out += "- **→ \(g.destination)**\(all ? "" : " (from \(g.sources.joined(separator: ", ")))") — " + g.limitations.joined(separator: " ") + "\n"72            }73        }74        let video = VideoEngine().paths, audio = AudioEngine().paths75        table("Video · conversion (re-encode)", video.filter { $0.operation == nil && $0.destination != .gif && $0.destination != .apng })76        table("Video · `video.remux` (container change without re-encoding)", video.filter { $0.operation == VideoEngine.remuxOperation })77        table("Video · `video.extract-audio`", video.filter { $0.operation == VideoEngine.extractAudioOperation })78        table("Video · `video.extract-subtitles`", video.filter { $0.operation == VideoEngine.extractSubtitlesOperation })79        table("Video · animated GIF / APNG", video.filter { $0.operation == nil && ($0.destination == .gif || $0.destination == .apng) })80        table("Audio · conversion", audio.filter { $0.source != .midi })81        table("Audio · MIDI rendering", audio.filter { $0.source == .midi })82        out += "\nTotal: \(video.count) video paths + \(audio.count) audio paths = \(video.count + audio.count).\n"83        out += "\n### Hardware capability read on the build machine\n\n"84        for line in MediaHardware.summaryLines() { out += "- \(line)\n" }85        for c in MediaHardware.capabilities() { out += "- \(c.displayName): VideoToolbox encoders listed — \(c.systemEncoders.isEmpty ? "none" : c.systemEncoders.joined(separator: "; "))\n" }86        try out.write(toFile: file, atomically: true, encoding: .utf8)87    }88}89
Files main
Menu

Theme