2016-03-28 228 views
0

我想將AVAssetExportSession的大小限制爲10MB。未設置fileLengthLimit,「導出已完成」。在設置fileLengthLimit = 10*1024*1024後,「導出失敗:無法打開」。設置`AVAssetExportSession`的`fileLengthLimit`會導致:「導出失敗:無法打開」

- (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。

回答

0

我更換

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

有:

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

AVAssetExportPresetPassthrough - 「此導出選項將導致所有曲目的媒體通過傳遞給輸出正是存儲在源資產「