合并video(AVFoundation)

在我的应用程序中,我录制了一些小video,并将它们添加到NSMutableArray作为AVAsset以便logging所捕获的内容。 当用户按下一个button合并它们时,最后的结果只是第一个录制的video(例如,如果拍摄三个短video,合并后的最终结果只是第一个video,其他video不会出现)。 我的代码迭代在NSMutableArray和拼接video在这里:

 if (self.capturedVideos.count != 0) { //Create AVMutableComposition Object.This object will hold our multiple AVMutableCompositionTrack. AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init]; for (AVAsset *asset in self.capturedVideos) { //check if the video is the first one captures so that it is placed at time 0. if ([self.capturedVideos indexOfObject:asset] == 0) { AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; [firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; previousAsset = asset; } else{ AVMutableCompositionTrack *track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:previousAsset.duration error:nil]; previousAsset = asset; } } 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(), ^{ [self exportDidFinish:exporter]; }); }]; } 

for循环后面的内容是将video导出到相机胶卷中保存。 那么我的错误在哪里? 持续时间是正确的(所以没有重叠)。 然而,我怀疑的东西。 有一个实例variables,我在@implementation后添加了大括号,它是previousAsset,跟踪之前添加的资源,从而知道下一个放置的位置。 它是AVAsset类,所以我没有初始化它,因为当我尝试它显示我一个错误。

previousAsset = [[AVAsset alloc] init];

这将工作正常

  AVMutableComposition *mainComposition = [[AVMutableComposition alloc] init]; AVMutableCompositionTrack *compositionVideoTrack = [mainComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; AVMutableCompositionTrack *soundtrackTrack = [mainComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; CMTime insertTime = kCMTimeZero; for (AVAsset *videoAsset in assets) { [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:insertTime error:nil]; [soundtrackTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:insertTime error:nil]; // Updating the insertTime for the next insert insertTime = CMTimeAdd(insertTime, videoAsset.duration); } NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Creating a full path and URL to the exported video NSString *outputVideoPath = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"mergeVideo-%d.mov",arc4random() % 1000]]; // NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent: current_name]; NSURL *outptVideoUrl = [NSURL fileURLWithPath:myPathDocs]; AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mainComposition presetName:AVAssetExportPreset640x480]; // Setting attributes of the exporter exporter.outputURL=outptVideoUrl; exporter.outputFileType =AVFileTypeMPEG4; //AVFileTypeQuickTimeMovie; exporter.shouldOptimizeForNetworkUse = YES; [exporter exportAsynchronouslyWithCompletionHandler:^{ dispatch_async(dispatch_get_main_queue(), ^{ //completion(exporter); [self exportDidFinish:exporter]; // [self exportDidFinish:exporter:assets]; }); }]; 

这将工作正常..