0

When encoding a video on iOS, most of the solutions include this step :

while(encoding) {
    if(assetWriterInput.readyForMoreMediaData) {
        [adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];
        if(buffer)
            CVBufferRelease(buffer);
        [NSThread sleepForTimeInterval:0.05]; // <=== This line slows down encoding
    }
} 

If I don't sleep the thread, the result video will look jerky, even if readyForMoreMediaData always return YES. If I pause the thread, the result looks perfect.

But I don't understand the purpose of "readyForMoreMediaData" if we need to pause the thread anyway ? It looks like I can reduce the sleep time to 0.03 without the result looking jerky though, but it's still slowing down a lot the encoding process.

Any help would be appreciated, thanks !

Xys
  • 8,486
  • 2
  • 38
  • 56

1 Answers1

0

I have used the assetwriter to write real time video in several apps for several years, including one that runs at 240 fps as a standard. I have had not problems with jerky video. I have never used any sleep commands and no CVBufferRelease. My code essentially looks like this:

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer  fromConnection:(AVCaptureConnection *)connection
{
  if (videoWriterInput.readyForMoreMediaData) [videoWriterInput appendSampleBuffer:sampleBuffer]) 

}

Maybe you should check how you set up your assetwriter? There is a "recommendedVideoSettingsForAssetWriterWithOutputFileType" setting that can help you optimize it. I would try without the adaptor if you don't absolutely need it, in my experience it runs smoother without.

Sten
  • 3,624
  • 1
  • 27
  • 26
  • Sorry, I should have said that I use OpenGL to draw objects, and encode the result into a video. I don't use the camera or an existing video as source. I need an adaptor because I need a pixelBufferPool. I will try the "recommendedVideoSettingsForAssetWriterWithOutputFileType". Thanks – Xys Sep 04 '17 at 08:49
  • I think recommendedVideoSettingsForAssetWriterWithOutputFileType is just for "AVCaptureVideoDataOutput" but I don't use this – Xys Sep 04 '17 at 08:53
  • If you want to see more code, here's a post explaining it : https://stackoverflow.com/a/9704392/5120292 – Xys Sep 04 '17 at 10:20