2016-04-05 5 views
1

ビデオをAVAssetExportSessionで4秒のチャンクに分割しようとしています。最初の分割は動作し、8MB/4秒のチャンクを返します。しかし、元のビデオがわずか18MBだと、2番目のものは12MBを返します。「AVAssetExportSession」でビデオを分割しようとしています

- (void) splitVideo{ 

    AVURLAsset *vidAsset = [AVURLAsset URLAssetWithURL:output options:nil]; 
    CMTime duration = vidAsset.duration; 

    NSLog(@"File size is : %.2f MB And Duration: %f",(float)[NSData dataWithContentsOfURL:output].length/1024.0f/1024.0f, CMTimeGetSeconds(duration)); 
    splitArray = [[NSMutableArray alloc]init]; 

    CMTime end = CMTimeMake(4, 1); 
    CMTimeRange range = CMTimeRangeMake(kCMTimeZero, end); 

    NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output0.mp4"]; 
    totalSeconds = 4.0f; 
    [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:AVAssetExportPresetHighestQuality]; 

     NSURL *finalUrl = [NSURL fileURLWithPath:path]; 
      [[NSFileManager defaultManager] removeItemAtURL:finalUrl error:NULL]; 

     exportSession.outputURL = finalUrl; 
     exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
     exportSession.shouldOptimizeForNetworkUse = YES; 
     exportSession.timeRange = range; 
      NSLog(@"start: %f end: %f", CMTimeGetSeconds(range.start), CMTimeGetSeconds(range.duration)); 

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

      }); 
      if ([exportSession status] == AVAssetExportSessionStatusCompleted){ 

       NSData *videoData = [[NSData alloc]initWithContentsOfURL:exportSession.outputURL]; 
       NSLog(@"DL: %f", (float)videoData.length/1024.0f/1024.0f); 

       [self makeFile:finalUrl]; 

       AVURLAsset *fullVid = [AVURLAsset URLAssetWithURL:output options:nil]; 

       CMTime start = CMTimeMake(totalSeconds, 1); 
       totalSeconds = totalSeconds + 4.0f; 
       CMTime end; 
       if ((CMTimeGetSeconds(start) + 4) > CMTimeGetSeconds(fullVid.duration)) { 
        end = fullVid.duration; 
       }else{ 
        end = CMTimeMake(CMTimeGetSeconds(start) + 4, 1); 
       } 
       CMTimeRange range2 = CMTimeRangeMake(start, end); 
       NSLog(@"%f < %f\n\n", CMTimeGetSeconds(start), CMTimeGetSeconds(fullVid.duration)); 

       if (CMTimeGetSeconds(start) < CMTimeGetSeconds(fullVid.duration)) { 
        NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"output%lu.mp4", splitArray.count]]; 
        [self cutVideo:output withRange:range2 withOutput:outputPath]; 
       }else{ 
        [self saveVideo:true]; 
       } 
      }else if ([exportSession status] == AVAssetExportSessionStatusFailed){ 
       NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]); 
      }else if ([exportSession status] == AVAssetExportSessionStatusCancelled){ 
       NSLog(@"Export canceled"); 
      } 
     }]; 
    } 
} 

ファイルサイズがある:18.86メガバイトと持続時間:9.171667

最初

開始:0.000000終了:4.000000

DL:8.194733

4.000000 < 9.171667

開始:4.000000終了:8.000000

DL:12.784523

答えて

1

ビデオデコーダので、それは間違っていないのです最後のフレームからの変更を格納します。 f "イメージ"。私は、あなたのビデオが2番目のチャンクでより多くの色を変更していると推測します。

+0

全体のビデオサイズは19MBです。私が最初に分割した範囲が1〜4秒で8 MB、次に2〜4秒の場合、12 MBにならない。 – Peter

+0

私は最初の0-4秒のスプリットを2-4に変更しましたが、これは10MB以上を返します。だから 'CMTime'sと何かが私は – Peter

+0

あなたは正しい感謝していると思う。 – Peter

関連する問題