If you want to filter existing videos, you should process them one by one.

MLBoy
2 min readDec 20, 2021

Filter existing video files

This is a method of filtering existing video files instead of filtering in real time while shooting with the camera .

I want to filter not only images but also videos. What’s more, I want to apply the video after shooting, not the video shot with the filter set.

Once you’ve filtered your existing video, you can shoot the original video and then mosaic or add cute effects to the areas you want to hide.
I’ve seen some samples that set effects before shooting and process them in real time, but
I couldn’t find any code to filter the original video.

Batch video processing didn’t work

There are two ways to filter video files in the official Apple docs, but
it didn’t work for me.
The initializer that creates AVVideoComposition that applies CIFilter to AVAsset did not work, and the
CustomAVVideoComposition class did not work with CIFilter (which was able to create watermarked images).

Disassemble each frame and hang one by one

Break down the video frame by frame and filter one by one.
Then, it is reconstructed as a moving image.
By doing this, you can be sure to process each sheet individually.
I thought it would take a lot of time to process one by one, but it was about the actual length of the video.
If you free the memory properly, it will not crash.

Method

When the video is read frame by frame with AVAssetReader
and decomposed frame by frame, the CIFilter process is inserted.
Then write to AVAssetWriter.

The point is not to write the sample buffer directly to AVAssetWriterInput, but to
create a pixel buffer acceptor called AVAssetWriterInputPixelBufferAdaptor and write the filtered pixel buffer to it.

Now you can apply various CI Filters to your videos.

🐣

I’m a freelance engineer.
Work consultation
Please feel free to contact us with a brief development description.
rockyshikoku@gmail.com

I am making an app that uses Core ML and ARKit.
We send machine learning / AR related information.

Twitter
Medium

--

--