How can I track the progress of AVAssetWriter’s writing?

Viewed 4386

How can I calculate the progress of an AVAssetWriter process? So if I have something like:

[assetWriterInput requestMediaDataWhenReadyOnQueue:queue usingBlock:^{
  while (1){
    if ([assetWriterInput isReadyForMoreMediaData]) {
      CMSampleBufferRef sampleBuffer = [audioMixOutput copyNextSampleBuffer];
      if (sampleBuffer) {
        [assetWriterInput appendSampleBuffer:sampleBuffer];
        CFRelease(sampleBuffer);
      } else {
        [assetWriterInput markAsFinished];
        break;
      }
    }
  }
}];

what can I be pulling (or polling) during the loop to figure out how many x of y I've completed?

Thanks.

2 Answers

You can track the progress using the code below. You need a total duration of the video and you will get using the code below.

let asset = AVAsset(url: urlToCompress);
let duration = asset.duration
let durationTime = CMTimeGetSeconds(duration)

Now, you need to calculate the current timestamp of the compressed video.

let timeStamp = CMSampleBufferGetPresentationTimeStamp(sample!)
let timeSecond = CMTimeGetSeconds(timeStamp)
let per = timeSecond / durationTime
print("Duration --- \(per)")
DispatchQueue.main.async {
    self.progress.progress = Float(per)
}
Related