Search code examples
iosavassetwriter

AVAssetWriter with non-constant frame rates


I'm trying to export a series of JPG images into a H264 movie (ideally it would be a motion JPEG movie, but unfortunately out AVAssetWriter does not support this codec).

The images come with a surveillance camera with VFR (variable frame rate), hence I compute the CMTime based on the time they have been captured, which – due to the way the VFR camera works – results in a non-constant frame rate.

When I do this though, the AVAssetWriter always fails to render the movie:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-16364), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x604000245a90 {Error Domain=NSOSStatusErrorDomain Code=-16364 "(null)"}}

When I change the CMTime instances to be e.g. CMMakeTime( frameIndex * 150, 600 ) it works.

Any idea how I could fix this?


Solution

  • -16364 is the code for invalid time code. It turned out I computed a wrong CMTime for the last image I wrote, which made the time going backwards. AVAssetWriter does not like that -- and rightly so.

    Thanks to bford in the Apple Developer Forum who helped me solve this problem.