Reputation: 193
I need to merge two video files along with their corresponding audio. I tried using : http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios
The videos gets merged, but their audio is missing. It's like a muted video. I need the audio also to be merged. I also googled on this, couldn't find anything useful. Can someone help me on this please...
Upvotes: 3
Views: 1599
Reputation: 535945
Live-editing audio is exactly like live-editing video. Go back to each movie and fetch the audio track and stick it into your mutable composition.
In this example, I grab the first five seconds of video and the last five seconds of video from a movie and put them one after the other in a new video:
NSString* type = AVMediaTypeVideo;
NSArray* arr = [oldAsset tracksWithMediaType:type];
AVAssetTrack* track = [arr lastObject];
CMTime duration = track.timeRange.duration;
AVMutableComposition* comp = [AVMutableComposition composition];
AVMutableCompositionTrack* comptrack = [comp addMutableTrackWithMediaType:type preferredTrackID:kCMPersistentTrackID_Invalid];
[comptrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(0,600), CMTimeMakeWithSeconds(5,600)) ofTrack:track atTime:CMTimeMakeWithSeconds(0,600) error:nil];
[comptrack insertTimeRange:CMTimeRangeMake(CMTimeSubtract(duration, CMTimeMakeWithSeconds(5,600)), CMTimeMakeWithSeconds(5,600)) ofTrack:track atTime:CMTimeMakeWithSeconds(5,600) error:nil];
But the resulting video would be silent. So I also go back and fetch the corresponding audio:
type = AVMediaTypeAudio;
arr = [oldAsset tracksWithMediaType:type];
track = [arr lastObject];
comptrack = [comp addMutableTrackWithMediaType:type preferredTrackID:kCMPersistentTrackID_Invalid];
[comptrack insertTimeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(0,600), CMTimeMakeWithSeconds(5,600)) ofTrack:track atTime:CMTimeMakeWithSeconds(0,600) error:nil];
[comptrack insertTimeRange:CMTimeRangeMake(CMTimeSubtract(duration, CMTimeMakeWithSeconds(5,600)), CMTimeMakeWithSeconds(5,600)) ofTrack:track atTime:CMTimeMakeWithSeconds(5,600) error:nil];
Upvotes: 5