Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a transparent background for an AVVideoComposition?

Update - I've added a 50 reputation bounty for answering this question!

I have an application that needs to put together some videos and photos to create a movie out of them. I am using AVMutableComposition for that. To be able to instruct it how the videos get composed I have to use an AVMutableVideoComposition. This thing has a property called backgroundColor and Apple Documentation says:

Only solid BGRA colors are supported; patterns and other supported colors are ignored. If the rendered pixel buffer does not have alpha, the alpha value of the background color is ignored.

What I understand from this is that there is a way to add alpha channel to the backgroundColor but I just don't understand how. What does If the rendered pixel buffer does not have alpha, the alpha value of the background color is ignored. mean? And how I can add such thing?

If I just do myVideoCompositionInstruction.backgroundColor = [[UIColor colorWithWhite:1 alpha:0] CGColor]; it just doesn't work - the background stays white and doesn't go transparent.

Any help would be much appreciated, Thanks!

like image 882
Mihai Fratu Avatar asked Nov 05 '22 18:11

Mihai Fratu


1 Answers

In Apple's documentation for AVVideoCompositionInstruction the property you want to set, backgroundColor, is of type CGColorRef.

My bet is, because you are setting backgroundColor with UIColor and only then getting the value of it through the CGColor property, for some reason the alpha value doesn't get passed into a CGColorRef object. You know the white value gets passed because the default color is black.

UIColor has some limitations. I suggest you read the following article. Take particular notice of the section "The problems with UIColor" of the article.

I'm guessing the best solution for your problem is defining backgroundColor with CGColor directly (use CGColorCreate, for instance), instead of UIColor.

like image 68
MiguelB Avatar answered Nov 09 '22 07:11

MiguelB