iOS 视频压缩问题, 我在网上也找了资料, 但是不多, 也不够详细全面, 我就自己写了一个压缩的小demo, 用到的是系统的一个类库
#import <AVFoundation/AVFoundation.h> 中 AVAssetExportSession 这个类, 官方API 是这样解释说明的, AVAssetExportSession 是对AVAsset对象内容进行转码, 并输出到制定的路径;
- (nullable instancetype)initWithAsset:(AVAsset *)asset presetName:(NSString *)presetName NS_DESIGNATED_INITIALIZER; 初始化方法
asset, 参数为要转码的asset 对象, presetName, 该参数为要进行转码的方式名称, 为字符串类型, 系统有给定的类型值,
AVAssetExportPresetLowQuality
AVAssetExportPresetMediumQuality
VAssetExportPresetHighestQuality
AVAssetExportPreset640x480
AVAssetExportPreset1280x720
outputURL , 为输出内容的URL, (指定一个文件路径, 然后根据路径初始化一个URL, 赋给, outputURL)
outputFileType, 为输出压缩后视频内容的格式类型
Demo 下载地址
https://github.com/jessonliu/JFUpLoadVideo.git
http://code.cocoachina.com/view/131886
下边直接上代码, 如有错误, 或有不好的地方, 欢迎大家指正, 本人虚心学习求教, 与大家共勉!
// Created by iOS-Developer on 16/2/19. // Copyright © 2016年 iOS-Jessonliu. All rights reserved. // #import "JFCompressionVideo.h" #import <AVFoundation/AVFoundation.h> #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]) { AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName: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]]]]; JFLog(@"resultPath = %@",resultPath); 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; JFLog(@"视频压缩后大小 %f", memorySize); resultPathBlock (resultPath, memorySize); } else { JFLog(@"压缩失败"); } }]; } else { JFLog(@"不支持 %@ 格式的压缩", compressionType); } }
/**
* 清楚沙盒文件中, 压缩后的视频所有, 在使用过压缩文件后, 不进行再次使用时, 可调用该方法, 进行删除
*/
+ (void)removeCompressedVideoFromDocuments { NSFileManager *manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:CompressionVideoPaht]) { [[NSFileManager defaultManager] removeItemAtPath:CompressionVideoPaht error:nil]; } } @end