How can I calculate the progress of an AVAssetWriter process? So if I have something like:
如何计算AVAssetWriter进程的进度?所以,如果我有类似的东西:
[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?
我可以在循环中拉动(或轮询)以确定我完成了多少x?
Thanks.
2 个解决方案
#1
17
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().
然后,您可以使用它来确定您进入输入缓冲区的源的距离。 presTime / duration应该给出一个0.0到1.0的值来表示大概的进度。如果您需要更精确,可以尝试使用CMSampleBufferGetDuration()来计算样本缓冲区中样本的持续时间。
If the presentation time does not work for you look at the other time stamps nearby in the header.
如果演示时间不起作用,请查看标题中附近的其他时间戳。
#2
0
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)
}
#1
17
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().
然后,您可以使用它来确定您进入输入缓冲区的源的距离。 presTime / duration应该给出一个0.0到1.0的值来表示大概的进度。如果您需要更精确,可以尝试使用CMSampleBufferGetDuration()来计算样本缓冲区中样本的持续时间。
If the presentation time does not work for you look at the other time stamps nearby in the header.
如果演示时间不起作用,请查看标题中附近的其他时间戳。
#2
0
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)
}