VideoFlint / Cabbage

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

Overlay H.265 Video with Transparency #72

Open BoazTelem opened 3 years ago

BoazTelem commented 3 years ago

Hi Guys!

I am trying to add a transparent H.265 video as an overlay and I am getting an opaque background for this track. I tried to set the timeline background to transparent:

timeline.backgroundColor = CIColor(red: 0, green: 0, blue: 0, alpha: 0)

But it did not work. Any idea how to set the background layer of an overlay to be transparent?

I am creating a video track item and add it to the overlay array using:

timeline.overlays = [overlayTrackItem]

P.S: When using the native AVMutableComposition and using the addMutableTrack the transparency is working just fine. I will be more than happy to learn how to eliminate the track background in Cabbage.

vitoziv commented 3 years ago

Fixed on 61c64d3

You may try it out, I'm a lot busy recently, reply maybe not very timely

BoazTelem commented 3 years ago

Hi Vito, Thank you for investing the time looking into it! Unfortunatly it is still opaque, I have emailed you a transparent overlay should you wish to quickly see the results. Hopefully we can resolve this bug soon as transparent video overlays are simply amazing feature for this great framework.

vitoziv commented 3 years ago

Hi

I think it's not possible to use a transparent video, video files only contain YUV data, we can't get the alpha value from YUV data. You may change your transparent video to a GIF or PNG list or something like this. You can subclass a ImageResource and implement your custom imageResource

BoazTelem commented 3 years ago

Hi Vito,

Thank you for your reply, I guess that what you mean is that if we are extracting the frames from the videos each frame will be opaque, and that you couldn't find a way to extract frames that contain transparency with Apple API correct?

The strange thing about this is that if I use:

let overlay = composition.addMutableTrack(withMediaType: .video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))

and then:

let instruction = videoCompositionInstructionFor(track: overlay!, using: overlayAsset) mainInstruction.layerInstructions.insert(instruction, at: 0)

I am able to overlay a transparent video. The question is, can the framework easily support this by bypassing the need to extract frames if I decide not to apply filters or other actions that require frame manipulation?

GIF and APNG files are very big, slow and memory consuming formats that I would like to avoid, if you find it hard to bypass the frame manipulation I think that my workaround will be to save the results from Cabbage, import the results as Avasset and then use the core API to add the overlay and then save again.

Will be more than happy to hear your thoughts.

P.S: The workaround worked nicely