Question

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.

Was it helpful?

Solution

The sample buffer has several time stamps on them. You could get the presentation time stamp with a call to:

CMTime presTime = CMSampleBufferGetPresentationTimeStamp( sampleBuffer );

You could then use that to determine how far you are into your source for the input buffer. presTime / duration should give you a 0.0 to 1.0 value representing the approximate progress. If you needed to be more precise you could try to factor in the duration of the samples in the sample buffer using CMSampleBufferGetDuration().

If the presentation time does not work for you look at the other time stamps nearby in the header.

OTHER TIPS

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)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top