How to convert CMSampleBufferRef(audio +video) to a video file(.mp4 or .mov) using AVAssetWriter every 10 seconds in iOS?
<p>I am using AVFoundation's <code>AVCaptureSession</code> to capture video for live streaming ( to send to the server using <code>RTMP protocol</code>).</p>
<p>The callback delegate methods of <code>AVCaptureVideoDataOutputSampleBufferDelegate</code> and <code>AVCaptureAudioDataOutputSampleBufferDelegate</code> returns the <code>CMSampleBufferRef</code> in  the following method :</p>
<pre><code>- (void)captureOutput:(AVCaptureOutput *)captureOutputdidOutputSampleBuffer:(CMSampleBufferRef)sampleBufferfromConnection:(AVCaptureConnection *)connection
</code></pre>
<p>As per the contents of this link , I can convert this buffer content to a file using <a href="https://www.objc.io/issues/23-video/capturing-video/#avcapturedataoutput-and-avassetwriter" rel="nofollow noreferrer"><strong>THIS LINK</strong></a>.</p>
<p>like</p>
<pre><code>NSURL *url = …;
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:url fileType:AVFileTypeMPEG4 error:nil];
AVAssetWriterInput *videoInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:nil];
videoInput.expectsMediaDataInRealTime = YES;
AVAssetWriterInput *audioInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio outputSettings:nil];
audioInput.expectsMediaDataInRealTime = YES;
if ([assetWriter canAddInput:videoInput]) {
    [assetWriter addInput:videoInput];
}
if ([assetWriter canAddInput:audioInput]) {
    [assetWriter addInput:audioInput];
}
</code></pre>
<p>But I am not getting the exact logic involved in this file conversion. The <code>didOutputSampleBuffer</code>
is a frame by frame captured (uncompressed )raw data and I want to convert these frames into a single file at certain interval.like say at each 10 seconds or so.And I want to send this file to the server.</p>
<p>Can someone give a brief idea about how <code>AVAssetWriter</code> works?or correct me if I am doing anything wrong.</p>