我有一堆视频文件要合并到一个视频文件中,我使用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);
视频保存在我的相机胶卷后,我尝试播放,但只有第一个NSData视频。我不知道为什么。
我尝试过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];
我不确定为什么timeRanges和track都包含值
答案 0 :(得分:2)
您可以合并多个视频并使用AVFoundation
类 - AVURLAsset
类,AVMutableComposition
,AVMutableCompositionTrack
等逐个追加,从而制作单个视频。
您可以查看Append/merge multiple video files into one final output video
还有一个很好的教程 1。Tutorial 1 2。Tutorial 2
希望它对你有所帮助。