Modify AVCaptureSession before saving with AVCaptureMovieFileOutput

╄→尐↘猪︶ㄣ 提交于 2019-12-03 08:49:06

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);

AVCaptureMovieFileOutput does not let you process captured frames. To do that, replace it with AVCaptureVideoDataOutput and encode the input to a .mov file using an AVAssetWriter.

As for the AVVideoComposition, I think you may have to apply it in a separate pass with an AVAssetExportSession.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!