how to merge two video with transparency
Asked Answered
C

3

16

I have successfully merge video-1 and video-2, over each other with video-2 being transparent using AVFoundation framework but after merging below video(video-1) is not displayed only video-2 is visible but when I use below code

AVMutableVideoCompositionLayerInstruction *SecondlayerInstruction =[AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:secondTrack];
[SecondlayerInstruction setOpacity:0.6 atTime:kCMTimeZero];

its set opacity on video-2 layer.But here actual problem is, there are some content over video-2 layer which is not transparent and here after applying opacity over video-2 layer it also apply over that content which is not transparent.
I am adding two image here which describe both scenario after set opacity using AVMutableVideoCompositionLayerInstruction

enter image description here enter image description here

  • as in image after merging transparent area is black and when I set opacity over second layer whole the video-2 goes transparent now but the content also become transparent.
  • but my question is that how to play transparent video over another video after merging.I already checked video-2 is transparent as it proper play in android platform.

Edited-1 : I also try to set a background color on myVideoCompositionInstructionwhich also not helped. taking reference from this old question link

Edited-2 : In AVVideoComposition.h, I found

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.

What it means, I didn't get it.can any one help?

Consumer answered 1/12, 2016 at 7:16 Comment(1)
Have you got solution for this problem? I am facing same issue. I want to merge two videos with transparency of second one. But its not working.Grasp
C
2

Good Question :

Try This

var totalTime : CMTime = CMTimeMake(0, 0)

func mergeVideoArray() {

let mixComposition = AVMutableComposition()
for videoAsset in arrayVideos {
    let videoTrack = 
        mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, 
                                       preferredTrackID: Int32(kCMPersistentTrackID_Invalid))          
    do {
        if videoAsset == arrayVideos.first {
            atTimeM = kCMTimeZero
        } else {
            atTimeM = totalTime // <-- Use the total time for all the videos seen so far.
        }
        try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), 
                                       of: videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0], 
                                       at: atTimeM)  
        videoSize = videoTrack.naturalSize
    } catch let error as NSError {
        print("error: \(error)")
    }
    totalTime += videoAsset.duration // <-- Update the total time for all videos.

...

Consumer answered 4/9, 2018 at 12:51 Comment(0)
G
1

Instead of opacity, you can set the alpha of video.

Explanation : Alpha sets the opacity value for an element and all of its children, While opacity sets the opacity value only for a single component.

enter link description here

Giliana answered 22/12, 2016 at 12:53 Comment(2)
or the alpha of the viewKermitkermy
@ShravyaBoggarapu Didn't get you.Giliana
M
1

This worked for me. I put the first video above the second video. I wanted the first video to have an opacity of 0.7

let firstVideoCompositionTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))

let secondVideoCompositionTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))

// ... run MixComposition ...

let firstLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: firstVideoCompositionTrack!)

firstLayerInstruction.setOpacity(0.7, at: .zero) <-----HERE

// rest of code
Mundy answered 3/4, 2022 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.