尝试使用 `AVAssetExportSession` 分割视频
Trying to split a video with `AVAssetExportSession`
我正在尝试使用 AVAssetExportSession
将视频分成 4 秒的块。初始拆分有效,returns 8mb/4 秒块。但是第二个 returns 12mb 是不正确的,而原始视频 os 只有 18mb.
- (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");
}
}];
}
}
File size is : 18.86 MB And Duration: 9.171667
第一
start: 0.000000 end: 4.000000
DL: 8.194733
4.000000 < 9.171667
秒
start: 4.000000 end: 8.000000
DL: 12.784523
这并没有错,因为视频解码器存储的是上一帧的变化,而不仅仅是一组 "images"。我猜你的视频在第二块中有更多的颜色变化,这就是你得到更多 space.
的原因
我正在尝试使用 AVAssetExportSession
将视频分成 4 秒的块。初始拆分有效,returns 8mb/4 秒块。但是第二个 returns 12mb 是不正确的,而原始视频 os 只有 18mb.
- (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");
}
}];
}
}
File size is : 18.86 MB And Duration: 9.171667
第一
start: 0.000000 end: 4.000000
DL: 8.194733
4.000000 < 9.171667
秒
start: 4.000000 end: 8.000000
DL: 12.784523
这并没有错,因为视频解码器存储的是上一帧的变化,而不仅仅是一组 "images"。我猜你的视频在第二块中有更多的颜色变化,这就是你得到更多 space.
的原因