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 ?
Now You have AVMutableComposition
s 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 aboveAVMutableVideoCompositionInstruction
.
This approach is from a very good Tutorial from this link.
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