How to combine video using AVMutableComposition in iPhone sdk

I used the following method, but the video did not merge, and at the output the last video added to AVMutableCompositionTrack is stored in the Library.

NSArray *arrVideoUrl=[objApp.dictSelectedVideos allKeys];
AVURLAsset *video1=[[AVURLAsset alloc]initWithURL:[arrVideoUrl objectAtIndex:0] options:nil];
AVURLAsset *video2=[[AVURLAsset alloc]initWithURL:[arrVideoUrl objectAtIndex:1] options:nil];
AVURLAsset *audioAsset=[[AVURLAsset alloc]initWithURL:songUrl options:nil];

AVMutableComposition *mixcomposition=[AVMutableComposition composition];

AVMutableCompositionTrack *track1=[mixcomposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[track1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, video1.duration) ofTrack:[[video1 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                 atTime:kCMTimeZero error:nil];


AVMutableCompositionTrack *track2=[mixcomposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

[track2 insertTimeRange:CMTimeRangeMake(kCMTimeZero, video2.duration) ofTrack:[[video2 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                 atTime:kCMTimeZero error:nil];

Can anyone help me out?

+5
source share
1 answer

Now that you have AVMutableCompositionfor both tracks, you need to create - - AVMutableVideoCompositionLayerInstructionfor both of them.

  • Create AVMutableVideoCompositionInstructionfor your combined track whose object it will look like having instructionsin an array consisting of AVMutableVideoCompositionLayerInstruction for the first and second tracks.

  • Create a AVMutableVideoCompositionMerged track for the object and provide above
    AVMutableVideoCompositionInstruction.

This approach is made from a very good lesson from this link.

+2

All Articles