【发布时间】: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