0

I am trying to limit the size of AVAssetExportSession to 10mb. Without setting fileLengthLimit, the "Export is completed". After setting fileLengthLimit = 10*1024*1024, the "Export failed: Cannot Open".

 - (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");
             }
          }];
     }
 }

The videos being exported are around 25mb.

Peter
  • 1,053
  • 13
  • 29

2 Answers2

0

I replaced

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

with:

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

AVAssetExportPresetPassthrough - "This export option will cause the media of all tracks to be passed through to the output exactly as stored in the source asset"

Peter
  • 1,053
  • 13
  • 29
0

I saw this error when using AVAssetExportSessionPresetHighestQuality or indeed any preset besides PassThrough (and with passthrough the session has no effect on my video so it's useless). Turns out my input video was the issue - I think the resolution was too high (it was almost 4k width), switching to a 1920x1080 video fixed it for me.

joel.d
  • 1,611
  • 16
  • 21