Установка `fileLengthLimit` для `AVAssetExportSession` вызывает: Ошибка экспорта: невозможно открыть

Я пытаюсь ограничить размер AVAssetExportSession до 10 МБ. Без установки fileLengthLimit "Экспорт завершен". После установки fileLengthLimit = 10*1024*1024 появляется сообщение «Ошибка экспорта: невозможно открыть».

 - (void) splitVideo{
     AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:output options:nil];
     CMTime videoDuration = videoAsset.duration;

     CMTime start = CMTimeMakeWithSeconds(0, 1);
     CMTimeRange range = CMTimeRangeMake(start, videoDuration);

     NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output1.mp4"];
     [self cutVideo:output withRange:range withOutput:outputPath];
}

 - (void) cutVideo:(NSURL *)url  withRange:(CMTimeRange)range withOutput:(NSString*)path{

    AVAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
    if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]) {
         AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                           initWithAsset:asset presetName:AVAssetExportPresetPassthrough];

         NSURL *finalUrl = [NSURL fileURLWithPath:path];
         exportSession.outputURL = finalUrl;
         exportSession.outputFileType = AVFileTypeMPEG4;
         exportSession.fileLengthLimit = 10*1024*1024;
         exportSession.timeRange = range;

         [exportSession exportAsynchronouslyWithCompletionHandler:^{
             dispatch_async(dispatch_get_main_queue(), ^{

             });
             if ([exportSession status] == AVAssetExportSessionStatusCompleted){
                 NSLog(@"Export completed");
             }else if ([exportSession status] == AVAssetExportSessionStatusFailed){
                 NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
             }else if ([exportSession status] == AVAssetExportSessionStatusCancelled){
                 NSLog(@"Export canceled");
             }
          }];
     }
 }

Экспортируемые видео имеют размер около 25 МБ.


person Peter    schedule 28.03.2016    source источник


Ответы (2)


я заменил

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough]

с:

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

AVAssetExportPresetPassthrough — «Эта опция экспорта приведет к тому, что медиафайлы всех треков будут переданы на выход в точности в том виде, в котором они хранятся в исходном ресурсе»

person Peter    schedule 28.03.2016

Я видел эту ошибку при использовании AVAssetExportSessionPresetHighestQuality или любой другой предустановки, кроме PassThrough (а при сквозной передаче сеанс не влияет на мое видео, поэтому он бесполезен). Оказывается, проблема заключалась в моем входном видео - я думаю, что разрешение было слишком высоким (оно было почти шириной 4k), переключение на видео 1920x1080 исправило это для меня.

person joel.d    schedule 17.12.2018