使用AVMutableComposition合并两个m4v电影文件 - 视频将无法合并 [英] Merging two m4v Movie Files Using AVMutableComposition - Videos Will Not Merge

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

问题描述

我使用以下代码尝试合并存储在文档文件夹中的两个m4v文件:

I am using the below code to try and merge two m4v files stored in the documents folder :

CMTime insertionPoint = kCMTimeZero;
NSError * error = nil;

AVMutableComposition *composition = [AVMutableComposition composition];

AVURLAsset* asset = [AVURLAsset URLAssetWithURL: [assetURLArray objectForKey:kIntroVideo] options:nil];

if (![composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) 
                          ofAsset:asset 
                           atTime:insertionPoint 
                            error:&error]) 
{
    NSLog(@"error: %@",error);
}

insertionPoint = CMTimeAdd(insertionPoint, asset.duration);

AVURLAsset* asset2 = [AVURLAsset URLAssetWithURL: [assetURLArray objectForKey:kMainVideo] options:nil];

if (![composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset2.duration) 
                          ofAsset:asset2 
                           atTime:insertionPoint 
                            error:&error]) 
{
    NSLog(@"error: %@",error);
}

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];

NSString *exportVideoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/FinishedVideo.m4v"];

NSURL *exportURL = [NSURL fileURLWithPath:exportVideoPath];
exportSession.outputURL = exportURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
    switch (exportSession.status) {
        case AVAssetExportSessionStatusFailed:{
            NSLog (@"FAIL");
            break;
        }
        case AVAssetExportSessionStatusCompleted: {
            NSLog (@"SUCCESS");
}
};
 }]; 
}

问题是这两个视频无法正常合并。合并的总电影持续时间是正确的,但是视频从不转换到第二部电影并且在其持续时间内继续显示第一部电影的最后一帧。奇怪的是,我可以听到第二个视频在后台播放的音频。

The problem is that the two videos will not merge properly. The total merged movie duration is correct, however the video never transitions to the second movie and continues to display the last frame of the first movie for its duration. Oddly I can hear the audio for the second video playing in the background.

有没有人有任何想法有什么问题?

Does anyone have any ideas what is wrong ?

编辑 - 奇怪的是,如果我合并两个完全相同长度的剪辑,它就可以工作。

EDIT - The odd thing is is that if I merge two clips of exactly the same length it works.

编辑 - 尝试将文件扩展名更改为.mov同样的问题。

EDIT - Have tried changing file extension to .mov with same problem.

推荐答案

好的 - 所以我最终通过使用单独的AVMutableComposition轨道,然后为音频设置一个mutablecomposition来实现这一点一个用于视频。

Ok - so I eventually got this working by using individual AVMutableComposition tracks and then setting a mutablecomposition for audio and one for video.

这篇关于使用AVMutableComposition合并两个m4v电影文件 - 视频将无法合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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