その後のその後

iOSエンジニア 堤 修一のブログ github.com/shu223

AVAssetWriterでのファイル書き込みの進行状況を取得する

AVAssetExportSession を使う場合は progress なるプロパティがあって簡単にできたようですが、AVAssetWriter とかそれ関連のクラスにはそういったプロパティがなかったので、下記のように実装しました。

[assetWriterInput requestMediaDataWhenReadyOnQueue:queue usingBlock:^{
  while (1){
    if ([assetWriterInput isReadyForMoreMediaData]) {
      CMSampleBufferRef sampleBuffer = [audioMixOutput copyNextSampleBuffer];
      if (sampleBuffer) {
        CMTime presTime = CMSampleBufferGetPresentationTimeStamp( sampleBuffer );
        float currentProgress = ((float)presTime.value / presTime.timescale) / duration;
        if (currentProgress > progress + 0.01) {
          progress = currentProgress;
          [Common sendNotification:NOTIFY_EXPORT_PROGRESS object:[NSNumber numberWithFloat:progress] userInfo:nil];
        }
        [assetWriterInput appendSampleBuffer:sampleBuffer];
        CFRelease(sampleBuffer);
      } else {
        [assetWriterInput markAsFinished];
        break;
      }
    }
  }
}];


ポイント:

  • 現在読み込み中の位置のタイムスタンプを取得
CMTime presTime = CMSampleBufferGetPresentationTimeStamp( sampleBuffer );
  • 秒数に変換
presTime.value / presTime.timescale


参考ページ:
progress of AVAssetWriter