繁体   English   中英

AVAssetExportSession 导出太慢

[英]AVAssetExportSession exporting too slow

我想导出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 *mutableComposition = [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;

我构建了一个将不同视频片段合并在一起的应用程序,我可以肯定地说这是您的情况。 我的视频文件有大约 10 mb,所以它们可能会小一点,但即使有 10 或 20 个片段,将它们合并在一起也只需不到一秒钟的时间。

现在至于它确实发生了,我已经根据您的配置检查了我的配置,差异如下:

  • 我使用export.outputFileType = AVFileTypeMPEG4
  • 我禁用了网络优化,如果您不打算从您的设备流式传输视频,您也应该禁用它

除此之外,它应该是相同的,我无法真正比​​较它,因为您必须提供有关如何实际创建组合的代码。 但是有一些事情需要检查:

  • 如果你正在使用AVURLAssetPreferPreciseDurationAndTimingKey创建时AVURLAsset ,你没有足够的关键帧,它实际上需要相当长的时间来试图通过找到钥匙,所以它减缓下来
  • 考虑您是否真的需要最高质量的视频
  • 考虑视频的分辨率并可能降低它

如果您提供更多信息,我应该能够为您提供更多帮助,但也许其中一些东西会起作用。 试一试,然后回来报告。

希望它有点帮助!

编辑 1:我忘了提及,如果您没有选择,您应该尝试使用FFmpeg库,因为它具有非常高的性能,但由于许可可能不适合您。

我认为这里的问题是AVAssetExportPresetHighestQuality这会导致转换或上采样,并且会减慢速度。 尝试改用AVAssetExportPresetPassthrough 将我的导出时间从 ~35+ 秒缩短到不到一秒

我还禁用了针对网络使用的优化,因为我们所有的视频都仅在应用程序中使用,并且从未流式传输或通过网络传输

请记住,您尝试导出的资产可能未存储在本地,而是首先下载内容,然后导出您的资产。

如果您不想下载任何内容

let videoRequestOptions: PHVideoRequestOptions = PHVideoRequestOptions()
videoRequestOptions.isNetworkAccessAllowed = true

您还将在requestExportSession完成处理程序中收到一条消息,其中包含一些有用的信息值。 https://developer.apple.com/documentation/photokit/phimagemanager/image_result_info_keys

否则,如果您想从 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 {

  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM