Record video from a camera with an animated overlay UIView

I currently have a camcorder configured using AVCaptureVideoDataOutput , whose sample buffer delegate is implemented as such:

 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { NSArray *detectedFaces = [self detectFacesFromSampleBuffer:sampleBuffer]; [self animateViewsForFaces:detectedFaces]; } 

The selection buffer is processed, and if any faces are found, their boundaries are displayed as views above AVCaptureVideoPreviewLayer , which display live video output (rectangles above the faces). Representations are animated so that they move smoothly between face recognition. Is it possible to somehow record what is shown in the preview layer and combine it with an animated UIView that overlays it, the end result is a video file?

+5
source share
1 answer

Typically, you can use a low-level approach to create a video stream and then write it to a file. I am not a specialist in video formats, codecs, etc., But the approach:

- Configure CADisplayLink to receive a callback in each frame when the screen is redrawn. Perhaps a good solution is to set the frame interval to 2 to reduce the target video frame rate to ~ 30 frames per second.

- Each time you re-view the screen, take a snapshot of the preview and overlay layer.

- Processing collected images: zip every two images of one frame, then make a video stream from a sequence of combined frames. I assume that iOS has built-in tools for a more or less simple way to do this.

Of course, resolution and quality are limited by layer parameters. If you need an unprocessed stream of video from the camera, you should capture this stream, and then draw your overlay data directly in the captured video frames.

+1
source

All Articles