ios - 如何在iOS中使用AVAsset合并多个视频?

标签 ios objective-c iphone avasset

我想将多个视频合并为一个。

但是我不知道该怎么做,所以请帮我解决这个问题。

以下是我的代码,但不适用于我。

AVMutableComposition* mixComposition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) 
                                ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
                                 atTime:kCMTimeZero error:nil];

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo                                                                                     preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
                           ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] 
                            atTime:kCMTimeZero error:nil];

AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition                                                  presetName:AVAssetExportPresetHighestQuality];   

NSString* videoName = @"export.mov";

NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];

if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) 
{
    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
}
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;

[_assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) {      
        // your completion code here
}];

我已经搜索过了,但没有答案对我有用。

最佳答案

在下面检查我的代码是否正确...

-(void)MergeVideo
{
    AppDelegate *appdel = (AppDelegate*)[[UIApplication sharedApplication] delegate];

    NSLog(@"Array Video Paths :- %@",appdel.arrVideoPath);

    CGFloat totalDuration;
    totalDuration = 0;

    AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

    AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                         preferredTrackID:kCMPersistentTrackID_Invalid];

    AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                    preferredTrackID:kCMPersistentTrackID_Invalid];

    CMTime insertTime = kCMTimeZero;

    for (id object in appdel.arrVideoPath)
    {
        AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:object]];

        CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

        [videoTrack insertTimeRange:timeRange
                        ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                         atTime:insertTime
                          error:nil];

        [audioTrack insertTimeRange:timeRange
                        ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                         atTime:insertTime
                          error:nil];

        insertTime = CMTimeAdd(insertTime,asset.duration);
    }

    NSString* documentsDirectory= [self applicationDocumentsDirectory];
    myDocumentPath= [documentsDirectory stringByAppendingPathComponent:@"merge_video.mp4"];
    urlVideoMain = [[NSURL alloc] initFileURLWithPath: myDocumentPath];

    if([[NSFileManager defaultManager] fileExistsAtPath:myDocumentPath])
    {
        [[NSFileManager defaultManager] removeItemAtPath:myDocumentPath error:nil];
    }

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
    exporter.outputURL = urlVideoMain;
    exporter.outputFileType = @"com.apple.quicktime-movie";
    exporter.shouldOptimizeForNetworkUse = YES;

    [exporter exportAsynchronouslyWithCompletionHandler:^{

        switch ([exporter status])
        {
            case AVAssetExportSessionStatusFailed:
                break;

            case AVAssetExportSessionStatusCancelled:
                break;

            case AVAssetExportSessionStatusCompleted:
                break;

            default:
                break;
        }
    }];
}

- (NSString*) applicationDocumentsDirectory
{
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

关于ios - 如何在iOS中使用AVAsset合并多个视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35859872/

相关文章:

iphone - 是否可以在后台使用 Avplayer 播放视频?

iphone - 为什么我不能访问此UITextView实例的.layer.borderWidth? (附加代码)

iphone - 更改 App Store 中的开发者名称

objective-c - 在第 3 代 iPad 中放大时删除和刷新 CATiledLayer

iphone - 为 NSURLRequest 生成 URL 字符串失败启动

ios - 如何隐藏tableview第一行的默认删除按钮?

c# - System.Drawing.Graphics Drawimage 将IOS拍摄的图片旋转90度

iphone - 检查 Controller 是否在导航 Controller 堆栈的顶部

ios - NSString decimalValue 方法

objective-c - iOS MapKit 用户定位而不初始化 map