基于AVAssetExportSession的视频质量压缩
参考 Unclefeng
AVAssetExportSession属于<AVFoundation/AVFoundation.h> 这个类, 官方API 是这样解释说明的, AVAssetExportSession 是对AVAsset对象内容进行转码, 并输出到制定的路径;
- (nullable instancetype)initWithAsset:(AVAsset *)asset presetName:(NSString *)presetName NS_DESIGNATED_INITIALIZER; 初始化方法 asset, 参数为要转码的asset 对象, presetName, 该参数为要进行转码的方式名称, 为字符串类型, 系统有给定的类型值,
视频质量类型
- AVAssetExportPresetLowQuality- AVAssetExportPresetMediumQuality- AVAssetExportPresetHighestQualitAy - AVAssetExportPreset640x480- AVAssetExportPreset1280x720
outputURL , 为输出内容的URL, (指定一个文件路径, 然后根据路径初始化一个URL, 赋给, outputURL) outputFileType, 为输出压缩后视频内容的格式类型
// Created by iOS-Developer on 16/2/19.// Copyright © 2016年 iOS-Jessonliu. All rights reserved.//#import "JFCompressionVideo.h"#import#define CompressionVideoPaht [NSHomeDirectory() stringByAppendingFormat:@"/Documents/CompressionVideoField"]@interface JFCompressionVideo ()@end@implementation JFCompressionVideo+ (void)compressedVideoOtherMethodWithURL:(NSURL *)url compressionType:(NSString *)compressionType compressionResultPath:(CompressionSuccessBlock)resultPathBlock { NSString *resultPath; NSData *data = [NSData dataWithContentsOfURL:url]; CGFloat totalSize = (float)data.length / 1024 / 1024; AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil]; NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset]; // 所支持的压缩格式中是否有 所选的压缩格式 if ([compatiblePresets containsObject:compressionType]) { // 用时间, 给文件重新命名, 防止视频存储覆盖, NSDateFormatter *formater = [[NSDateFormatter alloc] init]; [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"]; NSFileManager *manager = [NSFileManager defaultManager]; BOOL isExists = [manager fileExistsAtPath:CompressionVideoPaht]; if (!isExists) { [manager createDirectoryAtPath:CompressionVideoPaht withIntermediateDirectories:YES attributes:nil error:nil]; } resultPath = [CompressionVideoPaht stringByAppendingPathComponent:[NSString stringWithFormat:@"outputJFVideo-%@.mov", [formater stringFromDate:[NSDate date]]]]; NSLog(@"resultPath = %@",resultPath); AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:compressionType]; exportSession.outputURL = [NSURL fileURLWithPath:resultPath]; exportSession.outputFileType = AVFileTypeMPEG4; exportSession.shouldOptimizeForNetworkUse = YES; [exportSession exportAsynchronouslyWithCompletionHandler:^(void) { if (exportSession.status == AVAssetExportSessionStatusCompleted) { NSData *data = [NSData dataWithContentsOfFile:resultPath]; float memorySize = (float)data.length / 1024 / 1024; NSLog(@"视频压缩后大小 %f", memorySize); resultPathBlock (resultPath, memorySize); } else { NSLog(@"压缩失败"); } }]; } else { NSLog(@"不支持 %@ 格式的压缩", compressionType); }}/*** 清楚沙盒文件中, 压缩后的视频所有, 在使用过压缩文件后, 不进行再次使用时, 可调用该方法, 进行删除 */+ (void)removeCompressedVideoFromDocuments { NSFileManager *manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:CompressionVideoPaht]) { [[NSFileManager defaultManager] removeItemAtPath:CompressionVideoPaht error:nil]; }}@end