Convert AVAsset to CMSampleBuffer with timestamp

142 Views Asked by At

I followed a solution in this answer to write a video file into a CMSampleBuffer. I want to do trajectory recognition on a given video file. Therefore VNDetectTrajectoriesRequest requires CMSampleBuffer to be timestamped. But I have no clue how to achieve that.

The code I use looks like this:

guard let path = Bundle.main.path(forResource: "IMG_9900", ofType: "MOV") else {
    fatalError("no movie found")
}

let url = URL(fileURLWithPath: path)
let asset :AVAsset = AVAsset(url: url)
let reader = AVAssetReader(asset: asset)
guard let track = asset.tracks(withMediaType: .video).last else {
    return
}
let trackOutput = AVAssetReaderTrackOutput(track: track, outputSettings: nil)
reader.add(trackOutput)
reader.startReading()

// Get first sample buffer
var sample = trackOutput.copyNextSampleBuffer()
while sample != nil {
    print("1",sample)
    sample =  trackOutput.copyNextSampleBuffer()

    let requestHandler = VNImageRequestHandler(cmSampleBuffer: sample!)
    try requestHandler.perform([request])
    print("2",sample)
}


0

There are 0 best solutions below