簡體   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