将NSData视频文件合并为一个视频文件 [英] Merging NSData video files into one video file

查看:950
本文介绍了将NSData视频文件合并为一个视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆视频文件要合并到一个视频文件中,我使用NSMutableData来完成任务

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);

视频保存在我的相机胶卷后我尝试播放但只有第一个NSData视频是在里面。我不知道为什么。

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.

我尝试了AVMutableComposition,即便如此我也有同样的问题

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);
                }
            });
        }];



编辑



我也试过这个,但它给了我一个错误,它说 [__ NSArrayM insertObject:atIndex:]:object不能为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都有值?

I am not sure why as both timeRanges and tracks have values in them

推荐答案

您可以合并多个视频并通过使用 AVFoundation 类附加一个视频来创建单个视频 - AVURLAsset AVMutableComposition AVMutableCompositionTrack 等。

You can merge multiple videos and create a single video by appending one after other using AVFoundation classes- AVURLAsset, AVMutableComposition, AVMutableCompositionTrack etc.

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

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

There is a nice tutorials also 1.Tutorial 1 2.Tutorial 2

希望它对你有帮助。

这篇关于将NSData视频文件合并为一个视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆