1.獲取視頻路徑
2.創(chuàng)建壓縮視頻類
.h文件
import <Foundation/Foundation.h>
import <AVFoundation/AVFoundation.h>
@interface DdcompressVideo : NSObject
/**
- 壓縮成功Block
- @param resultPath 返回壓縮成功的視頻路徑
/
typedef void (^CompressionSuccessBlock)(NSString resultPath,float memorySize); // 定義成功的Block 函數(shù)
/ - method Comperssion Video 壓縮視頻的方法, 該方法將壓縮過的視頻保存到沙河文件, 如果壓縮過的視頻不需要再進行保留, 可調(diào)用 removeCompressedVideoFromDocuments 方法, 將其刪除即可
- @param url SourceVideoURL 被壓縮視頻的URL
- @param compressionType 壓縮可選類型
AVAssetExportPresetLowQuality
AVAssetExportPresetMediumQuality
AVAssetExportPresetHighestQuality
AVAssetExportPreset640x480
AVAssetExportPreset960x540
AVAssetExportPreset1280x720
AVAssetExportPreset1920x1080
AVAssetExportPreset3840x2160
返回壓縮后的視頻路徑
*/
- (void)compressedVideoOtherMethodWithURL:(NSURL *)url compressionType:(NSString *)compressionType compressionResultPath:(CompressionSuccessBlock)resultPathBlock;
/**
- 獲取視頻的大小
- @return 返回視頻的大小 float 類型
*/
- (float)countVideoTotalMemorySizeWithURL:(NSURL *)url;
/**
- 清楚沙盒文件中, 壓縮后的視頻所有
*/
- (void)removeCompressedVideoFromDocuments;
@end
3甩鳄。 的诵。m文件
import "DdcompressVideo.h"
define CompressionVideoPaht [NSHomeDirectory() stringByAppendingFormat:@"/Documents/CompressionVideoField"]
@implementation DdcompressVideo
-
(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]]]]; NSLog(@"壓縮文件路徑 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; NSLog(@"視頻壓縮后大小 %f", memorySize); resultPathBlock (resultPath, memorySize); } else { NSLog(@"壓縮失敗"); } }];
} else {
// JFLog(@"不支持 %@ 格式的壓縮", compressionType);
}
}
(float)countVideoTotalMemorySizeWithURL:(NSURL *)url {
NSData *data = [NSData dataWithContentsOfURL:url];
CGFloat totalSize = (float)data.length / 1024 / 1024;
return totalSize;
}(void)removeCompressedVideoFromDocuments {
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:CompressionVideoPaht]) {
[[NSFileManager defaultManager] removeItemAtPath:CompressionVideoPaht error:nil];
}
}
@end
4.調(diào)用