0

我试图将大小限制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。

4

2 回答 2

0

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

于 2018-12-17T23:27:09.937 回答
0

我换了

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

和:

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

AVAssetExportPresetPassthrough-“此导出选项将导致所有轨道的媒体完全按照源资产中存储的方式传递到输出”

于 2016-03-28T02:15:55.903 回答