PulseCanvas / app · pulsecanvas.ai / scripts / ReelDecodeSmoke.swift
| 1 | import Foundation |
| 2 | @preconcurrency import AVFoundation |
| 3 | import CoreVideo |
| 4 | |
| 5 | /// Read every encoded video frame and verify dimensions, cadence, coverage and soundtrack. |
| 6 | @main struct ReelDecodeSmoke { |
| 7 | static func main() async throws { |
| 8 | for path in CommandLine.arguments.dropFirst() { |
| 9 | let url = URL(fileURLWithPath: path), asset = AVURLAsset(url: URL(fileURLWithPath: path)) |
| 10 | let metadata = try JSONSerialization.jsonObject(with: Data(contentsOf: url.deletingPathExtension().appendingPathExtension("json"))) as! [String: Any] |
| 11 | let sections = metadata["segments"] as! [[String: Any]] |
| 12 | let expectedSeconds = sections.last!["end"] as! Double |
| 13 | let video = try await asset.loadTracks(withMediaType: .video).first! |
| 14 | let audio = try await asset.loadTracks(withMediaType: .audio) |
| 15 | precondition(!audio.isEmpty, "Missing soundtrack") |
| 16 | let size = try await video.load(.naturalSize), rate = try await video.load(.nominalFrameRate) |
| 17 | let duration = try await asset.load(.duration).seconds |
| 18 | precondition(abs(duration - expectedSeconds) < 0.06, "Incorrect duration") |
| 19 | let reader = try AVAssetReader(asset: asset) |
| 20 | let output = AVAssetReaderTrackOutput(track: video, outputSettings: [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA]) |
| 21 | output.alwaysCopiesSampleData = false; reader.add(output) |
| 22 | precondition(reader.startReading()) |
| 23 | var count = 0, minimumMean = 255.0, maximumCadenceError = 0.0 |
| 24 | while let sample = output.copyNextSampleBuffer() { |
| 25 | let time = CMSampleBufferGetPresentationTimeStamp(sample).seconds |
| 26 | maximumCadenceError = max(maximumCadenceError, abs(time - Double(count) / Double(rate))) |
| 27 | let buffer = CMSampleBufferGetImageBuffer(sample)! |
| 28 | let w = CVPixelBufferGetWidth(buffer), h = CVPixelBufferGetHeight(buffer) |
| 29 | precondition(w == Int(size.width) && h == Int(size.height)) |
| 30 | CVPixelBufferLockBaseAddress(buffer, .readOnly) |
| 31 | let data = CVPixelBufferGetBaseAddress(buffer)!.assumingMemoryBound(to: UInt8.self) |
| 32 | let row = CVPixelBufferGetBytesPerRow(buffer) |
| 33 | var sum = 0, n = 0 |
| 34 | // Sample the scene interior; labels at the bottom cannot mask a black image. |
| 35 | for y in stride(from: h / 20, to: h * 4 / 5, by: max(1, h / 18)) { |
| 36 | for x in stride(from: w / 30, to: w * 29 / 30, by: max(1, w / 24)) { |
| 37 | let i = y * row + x * 4 |
| 38 | sum += Int(data[i]) + Int(data[i+1]) + Int(data[i+2]); n += 3 |
| 39 | } |
| 40 | } |
| 41 | CVPixelBufferUnlockBaseAddress(buffer, .readOnly) |
| 42 | minimumMean = min(minimumMean, Double(sum) / Double(n)); count += 1 |
| 43 | } |
| 44 | precondition(reader.status == .completed && count == Int((expectedSeconds * Double(rate)).rounded()), "Missing or undecodable frames") |
| 45 | precondition(minimumMean > 1 && maximumCadenceError < 0.0001, "Black interior or incorrect cadence") |
| 46 | let receipt: [String: Any] = ["file": url.path, "width": size.width, "height": size.height, |
| 47 | "fps": rate, "duration": duration, "decodedFrames": count, "soundtrackPresent": true, |
| 48 | "minimumInteriorRGB": minimumMean, "maximumCadenceErrorSeconds": maximumCadenceError] |
| 49 | try JSONSerialization.data(withJSONObject: receipt, options: [.prettyPrinted, .sortedKeys]).write(to: url.deletingPathExtension().appendingPathExtension("decoded.json")) |
| 50 | print("PASS every decoded frame: \(url.lastPathComponent), \(count) frames, \(Int(size.width))×\(Int(size.height)), \(rate) fps, soundtrack present, minimum interior RGB \(minimumMean)") |
| 51 | } |
| 52 | } |
| 53 | } |