Search code examples
timeavassetimagegenerator

AVAssetImageGenerator gives image to far away from desired time


I am trying to split video into multiple photos, but when I use AVAssetImageGenerator I can only get photo for 0.0s, 0.5s, 0.1s etc

But how can I get e.g. 10 photos per second?

let imageGenerator = AVAssetImageGenerator(asset: asset)
        imageGenerator.appliesPreferredTrackTransform = true

        var times = [NSValue]()
        for i in 0..<5 {
            let t = Double(i) / 5 * 60
            times.append(NSValue(time: CMTimeMake(Int64(t), 60)))
        }

        imageGenerator.generateCGImagesAsynchronously(forTimes: times) { (time, imageRef, time2, result, error) in

Resulting frame times

time time2
0.0 0.0
0.2 0.0
0.4 0.5005
0.6 0.5005
0.8 1.001

Solution

  • It's is fixed by

    imageGenerator.requestedTimeToleranceAfter = CMTimeMake(1, 60)
    imageGenerator.requestedTimeToleranceBefore = CMTimeMake(1, 60)