From 6319aaaad148b0701045043ddd55fc1d559b00d7 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sat, 6 Jul 2024 22:30:12 -0700 Subject: [PATCH] Make the test more thorough and move color stuff to video composition --- .../SJSAssetExportSessionTests.swift | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/SJSAssetExportSessionTests/SJSAssetExportSessionTests.swift b/SJSAssetExportSessionTests/SJSAssetExportSessionTests.swift index 502489a..d7b944f 100644 --- a/SJSAssetExportSessionTests/SJSAssetExportSessionTests.swift +++ b/SJSAssetExportSessionTests/SJSAssetExportSessionTests.swift @@ -17,13 +17,16 @@ final class ExportSessionTests { let filename = "ExportSessionTests_testEncode_\(timestamp).mp4" let destinationURL = URL.temporaryDirectory.appending(component: filename) defer { _ = try? FileManager.default.removeItem(at: destinationURL) } - let size = CGSize(width: 1280, height: 720) let duration = CMTime(seconds: 1, preferredTimescale: 600) let videoComposition = try await AVMutableVideoComposition.videoComposition(withPropertiesOf: sourceAsset) videoComposition.renderSize = size videoComposition.renderScale = 1 videoComposition.frameDuration = CMTime(value: 1, timescale: 30) + videoComposition.colorPrimaries = AVVideoColorPrimaries_ITU_R_709_2 + videoComposition.colorTransferFunction = AVVideoTransferFunction_ITU_R_709_2 + videoComposition.colorYCbCrMatrix = AVVideoYCbCrMatrix_ITU_R_709_2 + try await ExportSession.export( asset: sourceAsset, audioMix: nil, @@ -41,11 +44,6 @@ final class ExportSessionTests { AVVideoAverageBitRateKey: NSNumber(value: 1_000_000), AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel as String, ] as [String: any Sendable], - AVVideoColorPropertiesKey: [ - AVVideoColorPrimariesKey: AVVideoColorPrimaries_ITU_R_709_2, - AVVideoTransferFunctionKey: AVVideoTransferFunction_ITU_R_709_2, - AVVideoYCbCrMatrixKey: AVVideoYCbCrMatrix_ITU_R_709_2, - ], ], timeRange: CMTimeRange(start: .zero, duration: duration), to: destinationURL, @@ -54,9 +52,24 @@ final class ExportSessionTests { let asset = AVURLAsset(url: destinationURL) #expect(try await asset.load(.duration) == duration) - try #require(await asset.loadTracks(withMediaType: .video).count == 1) - let videoTrack = try #require(try await asset.loadTracks(withMediaType: .video).first) - #expect(try await videoTrack.load(.naturalSize) == size) + // Audio try #require(try await asset.loadTracks(withMediaType: .audio).count == 1) + let audioTrack = try #require(await asset.loadTracks(withMediaType: .audio).first) + let audioFormat = try #require(await audioTrack.load(.formatDescriptions).first) + #expect(audioFormat.mediaType == .audio) + #expect(audioFormat.mediaSubType == .mpeg4AAC) + #expect(audioFormat.audioChannelLayout?.numberOfChannels == 2) + #expect(audioFormat.audioStreamBasicDescription?.mSampleRate == 44_100) + // Video + try #require(await asset.loadTracks(withMediaType: .video).count == 1) + let videoTrack = try #require(await asset.loadTracks(withMediaType: .video).first) + #expect(try await videoTrack.load(.naturalSize) == size) + print(try await videoTrack.load(.formatDescriptions)) + let videoFormat = try #require(await videoTrack.load(.formatDescriptions).first) + #expect(videoFormat.mediaType == .video) + #expect(videoFormat.mediaSubType == .h264) + #expect(videoFormat.extensions[.colorPrimaries] == .colorPrimaries(.itu_R_709_2)) + #expect(videoFormat.extensions[.transferFunction] == .transferFunction(.itu_R_709_2)) + #expect(videoFormat.extensions[.yCbCrMatrix] == .yCbCrMatrix(.itu_R_709_2)) } }