将NSDatavideo文件合并到一个video文件
我有一堆video文件,我想合并成一个video文件,我正在使用NSMutableData来实现任务
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);
在video保存在我的相机胶卷后,我尝试播放,但只有第一个NSDatavideo。 我不知道为什么。
编辑
我试过AVMutableComposition,即使那样我也有同样的问题
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); } }); }];
编辑
我也试过这个,但它给了我一个错误,它说[__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];
这是程序崩溃的地方。
[compositionTrack insertTimeRanges:timeRanges ofTracks:tracks atTime:kCMTimeZero error:&error];
我不知道为什么时间和轨道都有价值
您可以合并多个video,并通过使用AVFoundation
AVURLAsset
, AVMutableComposition
, AVMutableCompositionTrack
等)来附加一个video来创build单个video。
你可以检查这个附加/合并多个video文件到一个最终的输出video
还有一个不错的教程也是1. 教程1 2. 教程2
希望它可以帮助你。