VideoFlint / Cabbage

A video composition framework build on top of AVFoundation. It's simple to use and easy to extend.
MIT License
1.54k stars 224 forks source link

Custom Filter #20

Closed cosmicsalad closed 5 years ago

cosmicsalad commented 5 years ago

Hi Vito, I'm wondering what the process looks like for processing a filter now that filterProcessor was removed in version 0.2. The VideoCat demo used this when applying Lookup table filters but I'm wondering how I should go about doing this now it's gone. Do you have an example I could try? Thank you for your work and time!

vitoziv commented 5 years ago

You can set custom filter in TrackItem.videoConfiguration.configurations

cosmicsalad commented 5 years ago

What would be the correct way of creating the configuration? I'm just unclear about how to start that.

vitoziv commented 5 years ago

You need create a new class and implement VideoConfigurationProtocol protocol, then you can set the class instance to configurations

cosmicsalad commented 5 years ago

I've spent a while trying to wrap my head around it, if you have the time, would you be open to providing a very basic example of what correct implementation would look like?

vitoziv commented 5 years ago

Create a CustomEffect and implement VideoConfigurationProtocol

public class CustomEffect: VideoConfigurationProtocol {

    public func applyEffect(to sourceImage: CIImage, info: VideoConfigurationEffectInfo) -> CIImage {
        // HERE IS YOUR CUSTOM FILTER CODE
    }

}

Then you can put the effect to TrackItem

TrackItem item = ...
let effect = CustomEffect()
item.videoConfiguration.configurations = [effect]
cosmicsalad commented 5 years ago

Ok thank you for the confirmation that I was at least doing the setup right! Mostly confused with the purpose of the required NSCopying when conforming to VideoConfigurationProtocol

vitoziv commented 5 years ago

You are right :+1:, NSCopying is unnecessary...... I will optimize it later

cosmicsalad commented 5 years ago

So I got Custom Filters to work with the new configuration setup, but something I'm noticing is that with high-resolution videos filter application tends to make playback lag and eventually memory usage rises above about 1.3gb and it crashes. Would it be possible/helpful to modify how filter's get applied maybe by utilizing AVVideoComposition.init(asset:applyingCIFiltersWithHandler:)?

vitoziv commented 5 years ago

You need to find out why the memory keeps rising, not to change API.

cosmicsalad commented 5 years ago

Hmm, it seems to be caused when I set the Timeline's renderSize to the resource's width and height. If it's a portrait video, I don't want AVPlayer to have a background. I want the player to be the same size as the video's size so there's never a background.