I have 2 videos where one of them is overlay video with transparent background (explosion video that Im trying to add to the video). When I try to combine them using AVMutableComposition I see that overlay's video alpha channel is ignored. Basically I see 2nd video only (with black background instead of transparent).
As test I added opacity of 0.9 to overlay video just to make sure that they merged correctly and as result I see main video underneath overlay video (not what I want of course but proves that composition works). Any idea as to how to make alpha channel work for 2nd video?
NSError* error = nil;
AVMutableComposition *comp = [AVMutableComposition composition];
AVMutableCompositionTrack* videoCompTrack = [comp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:0];
AVMutableCompositionTrack* videoCompTrack2 = [comp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:1];
AVMutableCompositionTrack* audioCompTrack = [comp addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
// main video
AVURLAsset* videoAssetMain = [AVURLAsset URLAssetWithURL:url1 options:nil];
NSArray* tracks = [videoAssetMain tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack* videoTrackMain = [tracks firstObject];
tracks = [videoAssetMain tracksWithMediaType:AVMediaTypeAudio];
AVAssetTrack* audioTrackMain = [tracks firstObject];
CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, videoTrackMain.timeRange.duration);
// overlay video with alpha channel
AVURLAsset* videoAssetOver = [AVURLAsset URLAssetWithURL:url2 options:nil];
tracks = [videoAssetOver tracksWithMediaType:AVMediaTypeVideo];
AVAssetTrack* videoTrackOver = [tracks firstObject];
[videoCompTrack insertTimeRange:timeRange ofTrack:videoTrackMain atTime:kCMTimeZero error:&error];
[videoCompTrack2 insertTimeRange:timeRange ofTrack:videoTrackOver atTime:kCMTimeZero error:&error];
[audioCompTrack insertTimeRange:timeRange ofTrack:audioTrackMain atTime:kCMTimeZero error:&error];
AVMutableVideoCompositionLayerInstruction *inst1 = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoCompTrack];
[inst1 setOpacity:1 atTime:kCMTimeZero];
AVMutableVideoCompositionLayerInstruction *inst2 = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoCompTrack2];
// adding translucency to test that bottom video is there
// [inst2 setOpacity:0.9 atTime:kCMTimeZero];
// stretch overlay video on top of main video
CGAffineTransform scale = CGAffineTransformMakeScale(videoTrackMain.naturalSize.width/videoTrackOver.naturalSize.width, videoTrackMain.naturalSize.height/videoTrackOver.naturalSize.height);
[inst2 setTransform:scale atTime:kCMTimeZero];
AVMutableVideoCompositionInstruction *trans = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
trans.backgroundColor = [UIColor clearColor].CGColor;
trans.timeRange = timeRange;
trans.layerInstructions = [NSArray arrayWithObjects:inst2,inst1, nil];
AVMutableVideoComposition* videoComp = [AVMutableVideoComposition videoComposition];
videoComp.instructions = [NSArray arrayWithObjects:trans,nil];
videoComp.frameDuration = CMTimeMake(1, 30);
videoComp.renderSize = comp.naturalSize;
AVAssetExportSession* expSession = [[AVAssetExportSession alloc] initWithAsset:comp presetName:AVAssetExportPresetHighestQuality];
NSString* newVideoPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output_final.mov"];
if ([[NSFileManager defaultManager] fileExistsAtPath:newVideoPath]) {
[[NSFileManager defaultManager] removeItemAtPath:newVideoPath error:&error];
}
expSession.outputURL = [NSURL fileURLWithPath:newVideoPath];
expSession.outputFileType = AVFileTypeQuickTimeMovie;
expSession.videoComposition = videoComp;
[expSession exportAsynchronouslyWithCompletionHandler:^{
if (delegate) {
[delegate videoProcessor:self didFinish:expSession.outputURL];
}
}];
AVVideoCompositionInstruction
/* Indicates the background color of the composition. Solid BGRA colors only are supported; patterns and other color refs that are not supported will be ignored.
If the background color is not specified the video compositor will use a default backgroundColor of opaque black.
If the rendered pixel buffer does not have alpha, the alpha value of the backgroundColor will be ignored. */
@property (nonatomic, retain) __attribute__((NSObject)) CGColorRef backgroundColor;
have same problem, but did'nt find any way to provide BGRA color for backgroundColor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With