我正在尝试使用AVMutableComposition导出AVAssetExportSession。
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mutableComposition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL=url;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.videoComposition = mainCompositionInst;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^
{
switch (exporter.status)
{
case AVAssetExportSessionStatusCompleted:
{
NSLog(@"Video Merge SuccessFullt");
}
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"Failed:%@", exporter.error.description);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Canceled:%@", exporter.error);
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"Exporting!");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"Waiting");
break;
default:
break;
}
}];但是输出1分钟的视频也需要大约30秒的时间,考虑到iPad内置的摄像头应用程序只需要不到2秒的时间,这太过分了。
此外,如果我从出口商删除videoComposition,时间减少到7秒,这仍然是不好的,考虑到视频长度只有1分钟。那么,我想知道如何将出口时间降到最低?
另外,我想知道,AVAssetExportSession通常需要这么多时间,还是仅仅是我的情况?
更新:合并代码:
AVMutableComposition *可变构图= AVMutableComposition组合;
AVMutableCompositionTrack *videoCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio
preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableVideoCompositionLayerInstruction *videoTrackLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoCompositionTrack];
NSMutableArray *instructions = [NSMutableArray new];
CGSize size = CGSizeZero;
CMTime time = kCMTimeZero;
for (AVURLAsset *asset in assets)
{
AVAssetTrack *assetTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *audioAssetTrack = [asset tracksWithMediaType:AVMediaTypeAudio].firstObject;
NSError *error;
[videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, assetTrack.timeRange.duration )
ofTrack:assetTrack
atTime:time
error:&error];
[videoTrackLayerInstruction setTransform:assetTrack.preferredTransform atTime:time];
if (error) {
NSLog(@"asset url :: %@",assetTrack.asset);
NSLog(@"Error1 - %@", error.debugDescription);
}
[audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAssetTrack.timeRange.duration)
ofTrack:audioAssetTrack
atTime:time
error:&error];
if (error) {
NSLog(@"Error2 - %@", error.debugDescription);
}
time = CMTimeAdd(time, assetTrack.timeRange.duration);
if (CGSizeEqualToSize(size, CGSizeZero)) {
size = assetTrack.naturalSize;
}
}
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, time);
mainInstruction.layerInstructions = [NSArray arrayWithObject:videoTrackLayerInstruction];
AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];
mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];
mainCompositionInst.frameDuration = CMTimeMake(1, 30);
mainCompositionInst.renderSize = size;发布于 2020-01-03 21:05:33
我认为这里的问题是AVAssetExportPresetHighestQuality --这将导致转换或向上采样,并且会减慢速度。尝试使用AVAssetExportPresetPassthrough代替。将我从~35+ secs导出的时间缩短到不到1秒
我还禁用了网络使用的优化,因为我们所有的视频都只在应用程序中使用,并且从未通过网络进行流或传递。
发布于 2015-07-30 07:00:41
我建立了一个应用程序,合并了不同的视频片段,我可以放心地说,这是你的情况。我的视频文件有10 mb,所以它们可能要小一点,但是合并起来还需要不到秒的时间,即使有10,20个片段。
至于实际上正在发生的情况,我检查了我的配置与您的配置,区别如下:
export.outputFileType = AVFileTypeMPEG4除此之外,它应该是一样的,我不能真的比较它,因为你需要提供代码来说明你是如何创建这个组合的。不过,有一些事情需要检查:
AVURLAssetPreferPreciseDurationAndTimingKey时使用的是AVURLAsset,并且没有足够的关键帧,实际上需要相当长的时间才能找到密钥,因此会减慢速度如果你能提供更多的信息,我应该能帮你更多的忙,但也许其中的一些东西会有用。试一试,然后汇报。
希望它能帮上忙!
编辑1:我忘了提到,如果你没有选择,你应该尝试使用FFmpeg库,因为它是非常高的性能,虽然由于许可可能不适合你。
发布于 2020-04-03 14:57:17
请记住,您要导出的资产可能不是本地存储的,而是首先下载内容,然后导出您的资产。
如果你不想下载任何内容
let videoRequestOptions: PHVideoRequestOptions = PHVideoRequestOptions()
videoRequestOptions.isNetworkAccessAllowed = true您还将在requestExportSession完成处理程序中接收一条带有几个有用信息值的消息。关键字
否则,如果您想从iCloud下载您的资产并使其尽可能快,您可以使用以下参数
let videoRequestOptions: PHVideoRequestOptions = PHVideoRequestOptions()
// highQualityFormat, highQualityFormat, fastFormat, automatic
videoRequestOptions.deliveryMode = .fastFormat
videoRequestOptions.isNetworkAccessAllowed = true另一个重要的属性是导出预设,有一堆可用的预置
let lowQualityPreset1 = AVAssetExportPresetLowQuality
let lowQualityPreset2 = AVAssetExportPreset640x480
let lowQualityPreset3 = AVAssetExportPreset960x540
let lowQualityPreset4 = AVAssetExportPreset1280x720
let manager = PHImageManager()
manager.requestExportSession(forVideo: asset,
options: videoRequestOptions,
exportPreset: lowQualityPreset1) { (session, info) in
session?.outputURL = outputUrl
session?.outputFileType = .mp4
session?.shouldOptimizeForNetworkUse = true
session?.exportAsynchronously {
}
}https://stackoverflow.com/questions/31610146
复制相似问题