【问题标题】:Merging two m4v Movie Files Using AVMutableComposition - Videos Will Not Merge使用 AVMutableComposition 合并两个 m4v 电影文件 - 视频不会合并
【发布时间】:2012-05-04 07:52:24
【问题描述】:

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

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

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

有没有人知道什么是错的?

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

编辑 - 已尝试将文件扩展名更改为 .mov,但出现相同问题。

【问题讨论】:

  • 任何想法都将不胜感激。
  • 您是否尝试过使用不同的视频文件?另外,当您说它适用于两个相同长度的剪辑时,它们实际上是两个不同的剪辑,还是同一剪辑的两个实例?
  • 肖恩 - 感谢您的帮助 - 同一剪辑的 2 个实例。

标签: iphone objective-c xcode video avmutablecomposition


【解决方案1】:

您尚未将组合设置为 exportSession。

行后:

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

添加这一行

exportSession.videoComposition = composition;

这应该可以解决您的问题。

【讨论】:

    【解决方案2】:

    好的 - 所以我最终通过使用单独的 AVMutableComposition 轨道然后为音频和视频设置一个可变组合来完成这项工作。

    【讨论】:

    • 你能告诉我你是如何将asseturl保存到assetURLArray@Ohnomycoco
    • 我也有同样的问题。但与您的不同,第二个资产仍然是空白的。所以,如果我添加两个 5 秒的视频。成功导出 10 秒的新视频,但最后 5 秒显示空白视频。任何建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多