Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge the videos using AVMutableComposition in iPhone sdk

i have used the following way but videos didn't merge and in the output the last video added to AVMutableCompositionTrack gets stored in 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 please help me out ?

like image 751
Parvez Belim Avatar asked Oct 21 '22 12:10

Parvez Belim


1 Answers

Now You have AVMutableCompositions for both tracks, you need to create -- - AVMutableVideoCompositionLayerInstruction for both of them.

  • Create AVMutableVideoCompositionInstruction for your merged track whose object will be
    having instructions in an Array consisting of AVMutableVideoCompositionLayerInstruction for first and second track.

  • Create AVMutableVideoComposition for Merged track object and provide above
    AVMutableVideoCompositionInstruction.

This approach is from a very good Tutorial from this link.

like image 126
BhushanVU Avatar answered Oct 24 '22 04:10

BhushanVU