尝试使用`AVAssetExportSession`拆分视频

时间:2016-04-05 05:31:15

标签: ios avfoundation avassetexportsession cmtime

我正在尝试使用AVAssetExportSession将视频拆分为4个第二个块。初始拆分工作并返回8mb / 4秒的块。但是当原始视频仅为18mb时,第二次返回12mb是不正确的。

 - (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 MB和持续时间:9.171667

第一

  

开始:0.000000结束:4.000000

     

DL:8.194733

     

4.000000&lt; 9.171667

第二

  

开始:4.000000结束:8.000000

     

DL:12.784523

1 个答案:

答案 0 :(得分:1)

这不是不正确的,因为视频解码器存储来自最后一帧的变化,而不仅仅是一组&#34;图像&#34;。我猜你的视频在第二块中有更多的颜色变化,这就是你获得更多空间的原因。