Skip to content

Instantly share code, notes, and snippets.

@Anik0808
Last active February 12, 2024 06:19
Show Gist options
  • Save Anik0808/10956ef79474bf40f4c907c45d9ff014 to your computer and use it in GitHub Desktop.
Save Anik0808/10956ef79474bf40f4c907c45d9ff014 to your computer and use it in GitHub Desktop.
This function reads a video and exposes it's pixel buffers.
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