I have a bunch of video files that I want to merge into one video file, I am using NSMutableData to achieve the task
NSMutableData *concatenatedData = [[NSMutableData alloc] init];
for (int i=0; i <[videoArray count]; i ++) {
[concatenatedData appendData: [videoArray objectAtIndex:i]];
}
[concatenatedData writeToFile:[[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"outputConct.mov"] atomically:YES];
UISaveVideoAtPathToSavedPhotosAlbum([[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(),@"outputConct.mov"], nil, nil, nil);
after the video is saved in my camera roll I try to play it but only the first NSData video is in it. I am not sure why.
edit
I tried AVMutableComposition, even then I am having the same issues
AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init];
AVURLAsset* firstAsset;
AVMutableCompositionTrack *firstTrack;
CMTime time = kCMTimeZero;
for (int i=0; i<[videoArray count]; i++) {
firstAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:[[NSString alloc] initWithFormat:@"%@%d%@", NSTemporaryDirectory(),i, @"output.mov"]] options:nil];
firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:time error:nil];
time = firstAsset.duration;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"mergeVideo-%d.mov",arc4random() % 1000]];
NSURL *url = [NSURL fileURLWithPath:myPathDocs];
// 5 - Create exporter
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL=url;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (exporter.status == AVAssetExportSessionStatusCompleted) {
//NSURL *outputURL = exporter.outputURL;
UISaveVideoAtPathToSavedPhotosAlbum(myPathDocs, nil, nil, nil);
}
});
}];
edit
I also tried this, but it gives me an error, it says [__NSArrayM insertObject:atIndex:]: object cannot be nil'
AVMutableComposition *mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError * error = nil;
NSMutableArray * timeRanges = [NSMutableArray arrayWithCapacity:[videoArray count]];
NSMutableArray * tracks = [NSMutableArray arrayWithCapacity:[videoArray count]];
for (int i=0; i<[videoArray count]; i++) {
AVURLAsset *assetClip = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:[[NSString alloc] initWithFormat:@"%@%d%@", NSTemporaryDirectory(),i, @"output.mov"]] options:nil];
AVAssetTrack *clipVideoTrackB = [[assetClip tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[timeRanges addObject:[NSValue valueWithCMTimeRange:CMTimeRangeMake(kCMTimeZero, assetClip.duration)]];
[tracks addObject:clipVideoTrackB];
}
[compositionTrack insertTimeRanges:timeRanges ofTracks:tracks atTime:kCMTimeZero error:&error];
This is the line where the program is crashing.
[compositionTrack insertTimeRanges:timeRanges ofTracks:tracks atTime:kCMTimeZero error:&error];
I am not sure why as both timeRanges and tracks have values in them