Extracting frame from video in swift 3

3.4k views Asked by At

Hey I am trying to run a live feed on my device. Now I want to capture a photo every 3 seconds, but every-time it does it. It makes a shutter sound. This is bad UX.

Hence I want to run a live camera stream from the front camera and capture the frame at certain duration(~3 sec).

How can I extract a frame from the live camera feed and store it in a UIImage variable?

Thanks and Cheers!

2

There are 2 answers

11
Anand Nigam On BEST ANSWER

I understand your problem to the full extent. Few days back, I was also facing this problem, that's why I have developed a complete solution from showing live camera preview, arranging it properly on the view to getting camera frames continuously and converting the frames into UIImages efficiently without memory leak to utilise it accordingly. Kindly utilise the solution according to your need. The solution is optimised for swift 4.2 and developed on Xcode 10.0.

THIS IS THE LINK OF GITHUB REPO FOR THIS :- https://github.com/anand2nigam/CameraFrameExtractor

Kindly use your iPhone or iPad to test the application because it will not work on simulator. Please let me know about the app functionality and its working and if any help needed, do contact me. Hope it can solve your problem. Happy Learning.

1
Tim Bull On

When you're writing your own custom camera, you'll use AVAssetReader and AVAssetWriter in conjunction with the AVCaptureVideoDataOutputSampleBufferDelegate to vend and process CMSampleBuffers.

Using this approach, you can easily get a CIImage and process that with filters etc.

You basically want a timer that triggers every three seconds, lets the delegate block know to capture and process a frame, do what you want with it and just keep discarding the rest of them (or writing them to video instead if that's what you want).

This question here Recording videos with real-time filters in Swift contains the sample code you're looking for. Instead of writing the buffer out, instead capture it as an image.

Good luck!