设置`AVAssetExportSession`的`fileLengthLimit`会导致:“导出失败:无法打开”

时间:2016-03-28 00:39:51

标签: ios avassetexportsession avasset avurlasset

我试图将AVAssetExportSession的大小限制为10mb。如果不设置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");
             }
          }];
     }
 }

导出的视频大约是25mb。

2 个答案:

答案 0 :(得分:0)

我替换了

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

使用:

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

AVAssetExportPresetPassthrough - “此导出选项会将所有曲目的媒体传递到源资源中存储的输出完全

答案 1 :(得分:0)

在使用AVAssetExportSessionPresetHighestQuality或除PassThrough以外的任何其他预设时,我都看到了此错误(并且通过会话对我的视频没有影响,因此无用)。原来我的输入视频是个问题-我认为分辨率太高(几乎是4k宽度),因此切换到1920x1080视频为我解决了。