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?

Community
  • 1
  • 1
pramod
  • 341
  • 2
  • 17
  • 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. – Himanshu Mahajan Jul 04 '17 at 06:55

3 Answers3

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.

...

pramod
  • 341
  • 2
  • 17
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

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
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
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256