SPB Git

Repository Public

MediaVerifier.swift 7.5 KB 119 lines swift

1// SP Lecture Studio — LectureRender2// File: Packages/LectureRender/Sources/MediaVerifier.swift3// Description: Verifies an exported movie by actually decoding it (tracks, duration, frames, audio), and helpers to probe frames and samples.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import AVFoundation9import CoreGraphics10import LectureCore1112public struct MediaReport: Sendable, Hashable {13    public var isPlayable = false14    public var duration: MediaTime = .zero15    public var videoSize: CGSize = .zero16    public var nominalFPS: Double = 017    public var videoCodec = ""18    public var hasAudio = false19    public var audioSampleRate: Double = 020    public var audioChannels = 021    /// Number of video frames decoded by a full read (0 when `decodeAll` was false).22    public var decodedFrames = 023    public var decodedAudioFrames: Int64 = 024    public var problems: [String] = []25    public var isValid: Bool { isPlayable && problems.isEmpty && videoSize.width > 0 }26}2728public enum MediaVerifier {29    /// Loads the file like a player would, then (optionally) decodes every video frame and every audio sample.30    public static func verify(_ url: URL, expectedDuration: MediaTime? = nil, decodeAll: Bool = true) async -> MediaReport {31        var report = MediaReport()32        let asset = AVURLAsset(url: url)33        do {34            report.isPlayable = try await asset.load(.isPlayable)35            report.duration = MediaTime(seconds: try await asset.load(.duration).seconds)36            guard let video = try await asset.loadTracks(withMediaType: .video).first else { report.problems.append("no video track"); return report }37            report.videoSize = try await video.load(.naturalSize)38            report.nominalFPS = Double(try await video.load(.nominalFrameRate))39            if let desc = try await video.load(.formatDescriptions).first {40                let code = CMFormatDescriptionGetMediaSubType(desc)41                report.videoCodec = String(bytes: [UInt8(code >> 24 & 0xFF), UInt8(code >> 16 & 0xFF), UInt8(code >> 8 & 0xFF), UInt8(code & 0xFF)], encoding: .ascii) ?? ""42            }43            let audio = try await asset.loadTracks(withMediaType: .audio).first44            if let audio, let desc = try await audio.load(.formatDescriptions).first, let asbd = CMAudioFormatDescriptionGetStreamBasicDescription(desc)?.pointee {45                report.hasAudio = true; report.audioSampleRate = asbd.mSampleRate; report.audioChannels = Int(asbd.mChannelsPerFrame)46            }47            if let expectedDuration, abs(report.duration.seconds - expectedDuration.seconds) > 0.1 {48                report.problems.append(String(format: "duration %.3f s differs from expected %.3f s", report.duration.seconds, expectedDuration.seconds))49            }50            if decodeAll {51                let reader = try AVAssetReader(asset: asset)52                let vOut = AVAssetReaderTrackOutput(track: video, outputSettings: [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA])53                vOut.alwaysCopiesSampleData = false54                reader.add(vOut)55                var aOut: AVAssetReaderTrackOutput?56                if let audio {57                    let o = AVAssetReaderTrackOutput(track: audio, outputSettings: [AVFormatIDKey: kAudioFormatLinearPCM, AVLinearPCMBitDepthKey: 16,58                                                                                    AVLinearPCMIsFloatKey: false, AVLinearPCMIsBigEndianKey: false, AVLinearPCMIsNonInterleaved: false])59                    o.alwaysCopiesSampleData = false; reader.add(o); aOut = o60                }61                guard reader.startReading() else { report.problems.append("cannot decode: \(reader.error?.localizedDescription ?? "?")"); return report }62                while let sb = vOut.copyNextSampleBuffer() { if CMSampleBufferGetImageBuffer(sb) != nil { report.decodedFrames += 1 } }63                if let aOut { while let sb = aOut.copyNextSampleBuffer() { report.decodedAudioFrames += Int64(CMSampleBufferGetNumSamples(sb)) } }64                if reader.status == .failed { report.problems.append("decode error: \(reader.error?.localizedDescription ?? "?")") }65                if report.decodedFrames == 0 { report.problems.append("no decodable video frame") }66            }67        } catch { report.problems.append(error.localizedDescription) }68        return report69    }7071    /// Exact frame of a movie at `time` (zero tolerance).72    public static func frame(of url: URL, at time: MediaTime) async -> CGImage? {73        let gen = AVAssetImageGenerator(asset: AVURLAsset(url: url))74        gen.requestedTimeToleranceBefore = .zero; gen.requestedTimeToleranceAfter = .zero75        gen.appliesPreferredTrackTransform = true76        return try? await gen.image(at: CMTime(value: time.ns, timescale: MediaTime.timescale)).image77    }7879    /// Decoded mono samples of the audio track for a time range, with their sample rate.80    public static func audioSamples(of url: URL, range: TimeRange) async -> (samples: [Float], sampleRate: Double)? {81        let asset = AVURLAsset(url: url)82        guard let track = try? await asset.loadTracks(withMediaType: .audio).first, let reader = try? AVAssetReader(asset: asset) else { return nil }83        let out = AVAssetReaderTrackOutput(track: track, outputSettings: [AVFormatIDKey: kAudioFormatLinearPCM, AVLinearPCMBitDepthKey: 32, AVLinearPCMIsFloatKey: true,84                                                                          AVLinearPCMIsBigEndianKey: false, AVLinearPCMIsNonInterleaved: false, AVNumberOfChannelsKey: 1])85        reader.add(out)86        reader.timeRange = CMTimeRange(start: CMTime(value: range.start.ns, timescale: MediaTime.timescale), duration: CMTime(value: range.duration.ns, timescale: MediaTime.timescale))87        guard reader.startReading() else { return nil }88        var samples: [Float] = []89        var rate = 48_000.090        var firstPTS: Double?91        while let sb = out.copyNextSampleBuffer() {92            if let desc = CMSampleBufferGetFormatDescription(sb), let asbd = CMAudioFormatDescriptionGetStreamBasicDescription(desc)?.pointee { rate = asbd.mSampleRate }93            if firstPTS == nil { firstPTS = CMSampleBufferGetPresentationTimeStamp(sb).seconds }94            guard let block = CMSampleBufferGetDataBuffer(sb) else { continue }95            var length = 0; var pointer: UnsafeMutablePointer<Int8>?96            CMBlockBufferGetDataPointer(block, atOffset: 0, lengthAtOffsetOut: nil, totalLengthOut: &length, dataPointerOut: &pointer)97            if let pointer { pointer.withMemoryRebound(to: Float.self, capacity: length / 4) { samples.append(contentsOf: UnsafeBufferPointer(start: $0, count: length / 4)) } }98        }99        // The reader may start slightly before the requested range (AAC packet boundary): trim so that index 0 is `range.start`.100        if let firstPTS, firstPTS < range.start.seconds {101            let drop = Int(((range.start.seconds - firstPTS) * rate).rounded())102            samples = Array(samples.dropFirst(min(drop, samples.count)))103        }104        return (samples, rate)105    }106107    /// Times (seconds from the start of `samples`) of clicks: isolated samples above `threshold`.108    public static func clickTimes(in samples: [Float], sampleRate: Double, threshold: Float = 0.5, minGap: Double = 0.05) -> [Double] {109        var out: [Double] = []110        var last = -Double.infinity111        for (i, v) in samples.enumerated() where abs(v) >= threshold {112            let t = Double(i) / sampleRate113            if t - last >= minGap { out.append(t) }114            last = t115        }116        return out117    }118}119
Files main
Menu

Theme