2016-04-05 7 views
1

Ich versuche, ein Video in 4 Sekunden Brocken mit AVAssetExportSession zu teilen. Die anfängliche Aufteilung funktioniert und gibt einen 8mb/4 Sekunden Chunk zurück. Aber die Sekunde gibt 12mb zurück, die falsch ist, wenn das ursprüngliche Video nur 18mb ist.Versuchen, ein Video mit `AVAssetExportSession` zu spalten

- (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"); 
      } 
     }]; 
    } 
} 

Dateigröße beträgt: 18,86 MB und Dauer: 9,171667

erste

Start: 0.000000 Ende: 4,000000

DL: 8,194733

4,000000 < 9,171667

an zweiter Stelle

Start: 4,000000 Ende: 8.000000

DL: 12,784523

Antwort

1

Es ist nicht falsch, da Video-Decoder speichert Änderungen vom letzten Frame, nicht nur von einem Set o f "Bilder". Ich nehme an, dass dein Video mehr Farbänderungen im zweiten Teil hat, deshalb bekommst du mehr Platz.

+0

Die gesamte Videogröße beträgt 19 MB. Wenn ich die erste mit einer Spanne von 1-4 Sekunden spalte, was zu 8 mb und dann zu der zweiten 4-8 Sekunden führt, sollte es auf keinen Fall 12 mb sein. – Peter

+0

Außerdem habe ich die ursprüngliche 0-4 Sekunden Split auf 2-4 geändert und dies gibt über 10mb zurück. Also ist es etwas mit den 'CMTime's Ich denke, – Peter

+0

nvm Sie sind richtig, danke. – Peter

Verwandte Themen