Last active
February 12, 2024 06:19
-
-
Save Anik0808/10956ef79474bf40f4c907c45d9ff014 to your computer and use it in GitHub Desktop.
This function reads a video and exposes it's pixel buffers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func readImageBuffer(fromVideo videoURL: URL) { | |
let asset = AVAsset(url: videoURL) | |
let assetTrack = asset.tracks.first! | |
let assetReader = try! AVAssetReader(asset: asset) | |
let outputSettings: [String: Any] = [ | |
String(kCVPixelBufferPixelFormatTypeKey): kCVPixelFormatType_32BGRA, | |
String(kCVPixelBufferWidthKey): 513, | |
String(kCVPixelBufferHeightKey): 513, | |
] | |
let assetReaderTrack = AVAssetReaderTrackOutput(track: assetTrack, outputSettings: outputSettings) | |
assetReader.add(assetReaderTrack) | |
assetReader.startReading() | |
while let sampleBuffer = assetReaderTrack.copyNextSampleBuffer() { | |
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { | |
print("failed ❌") | |
continue | |
} | |
//handle pixel buffer | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment