How to set a transparent background for an AVVideoComposition?
Asked Answered
D

1

5

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!

Derouen answered 13/7, 2011 at 12:20 Comment(9)
Anyone? I really need this and I have no idea how to do it.Derouen
Have you tried myVideoCompositionInstruction.backgroundColor = [[UIColor clearColor] CGColor]?Persona
Yup - doesn't do it either :(Derouen
How about [UIColor redColor].CGColor? Does it turn red? This will at least eliminate the possibility of you not using the property correctly.Srini
It does turn red or any other color I apply to it. It just doesn't go transparent whatever I do to it :(Derouen
More code would help. Maybe you can strip it down to something extremely basic and post it?Srini
Were you able to solve this ?Pyromorphite
I don't think I ever managed to fix this. It's been so long ago I don't really remember but I think I just took another approach and never had to deal with it anymore...Derouen
The paradox here is H.264 video doesn't support alpha channel so Apple intentionally disabled alpha channel support for background color. I tried everything but could never get alpha channel working on background color.Pyromorphite
C
2

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.

Center answered 16/7, 2011 at 19:10 Comment(3)
Thanks for the tip. I will try this as soon as I get to the office tomorrow and I'll let you know if this works...Derouen
I did this CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); float transparentWhite[] = { 1, 1, 1, 0 }; CGColorRef backgroundColor = CGColorCreate(colorSpace, transparentWhite); _videoInstruction.backgroundColor = backgroundColor; and it still doesn't work...Derouen
I tried this, the white color gets visible with this method but the alpha is ignored, even if I set opacity for the rendered pixel buffer. Any ideas why this could happen and any ways to workaround ?Pyromorphite

© 2022 - 2024 — McMap. All rights reserved.