合并NSData的视频文件合并为一个视频文件(Merging NSData video files

2019-09-01 23:56发布

我有一大堆的视频文件,我想合并成一个视频文件,我使用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和轨道在他们价值观

Answer 1:

您可以合并多个视频和其他后使用附加一个创建一个单一的视频AVFoundation类- AVURLAssetAVMutableCompositionAVMutableCompositionTrack等。

您可以检查此追加/多个视频文件合并成一个最终输出视频

有一个很好的教程也是1 教程1 2 教程2

希望它可以帮助你。



文章来源: Merging NSData video files into one video file