简体   繁体   English

我如何跟踪AVAssetWriter的写作进度?

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

How can I calculate the progress of an AVAssetWriter process? 如何计算AVAssetWriter进程的进度? 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? 在循环中可以拉出(或轮询)什么以找出完成的y个x?

Thanks. 谢谢。

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. presTime /持续时间应为您提供0.0到1.0的值,代表大致进度。 If you needed to be more precise you could try to factor in the duration of the samples in the sample buffer using CMSampleBufferGetDuration(). 如果需要更精确,则可以尝试使用CMSampleBufferGetDuration()将样本缓冲区中的样本持续时间考虑在内。

If the presentation time does not work for you look at the other time stamps nearby in the header. 如果演示时间不适合您,请查看标题中附近的其他时间戳。

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM