iosavfoundationavcapturesessionavcapturemoviefileoutput

Modify AVCaptureSession before saving with AVCaptureMovieFileOutput


Use Case: I want to capture input from the camera, draw on top of the captured frames (and sound) and save the result as a .mov file.

Problem: I can't see how to modify the input before it is saved to file.


Solution

  • The RosyWriter was almost doing what I wanted. Adding the following code to captureOutput:didOutputSampleBuffer:fromConnection: enabled me to draw onto the frame using Quartz.

        CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    
        CVPixelBufferLockBaseAddress(pixelBuffer, 0);
        void *pxdata = CVPixelBufferGetBaseAddress(pixelBuffer);
        NSParameterAssert(pxdata != NULL);
    
        CGSize frameSize = CGSizeMake(self.videoDimensions.width, self.videoDimensions.height);
    
        CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(pxdata, frameSize.width,
                                                     frameSize.height, 8, 4*frameSize.width, rgbColorSpace, 
                                                     kCGImageAlphaNoneSkipFirst);
    
        CGContextMoveToPoint(context, 100, 100);
        CGContextAddLineToPoint(context, 200, 200);
        CGContextDrawPath(context, kCGPathStroke);
    
        CGColorSpaceRelease(rgbColorSpace);
        CGContextRelease(context);