- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSString*path=[[NSBundle mainBundle]pathForResource:@"dachao" ofType:@"m4v"]; NSString*outpath=[NSString stringWithFormat:@"%@/%@/%@",NSHomeDirectory(),@"Documents",@"dachaotest"]; NSLog(@"%@",NSHomeDirectory()); NSDate *currentDate = [NSDate date];//獲取當前時間,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"]; NSString *dateString = [dateFormatter stringFromDate:currentDate]; NSLog(@"dateString:%@",dateString); [self lowQuailtyWithInputURL:[[NSURL alloc]initFileURLWithPath:path] outputURL:[NSURL fileURLWithPath:outpath] blockHandler:^(AVAssetExportSession *session) { if (session.status==AVAssetExportSessionStatusCompleted) { NSLog(@"complete"); NSDate *currentDate = [NSDate date];//獲取當前時間,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"]; NSString *dateString = [dateFormatter stringFromDate:currentDate]; NSLog(@"dateString:%@",dateString); }else{ NSLog(@"fail"); } }]; NSLog(@"代碼結束"); }
//視頻壓縮代碼 - (void) lowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL blockHandler:(void (^)(AVAssetExportSession*))handler { AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil]; AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality]; session.outputURL = outputURL;
//壓縮格式 session.outputFileType = AVFileTypeQuickTimeMovie; [session exportAsynchronouslyWithCompletionHandler:^(void) { handler(session); }]; }
//圖片等比例壓縮 有損壓縮
-(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = size.width;
CGFloat targetHeight = size.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
if(CGSizeEqualToSize(imageSize, size) == NO){
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if(widthFactor > heightFactor){
scaleFactor = widthFactor;
}
else{
scaleFactor = heightFactor;
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
if(widthFactor > heightFactor){
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}else if(widthFactor < heightFactor){
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(size);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil){
NSLog(@"scale image fail");
}
UIGraphicsEndImageContext();
return newImage;
}
//圖片無損壓縮
-(UIImage*)imageCompressWithImage:(UIImage*)image andQuality:(float)ql{
NSData*data=UIImageJPEGRepresentation(image, ql);
UIImage*newImage=[UIImage imageWithData:data];
data=nil;
return newImage;
}
視頻壓縮格式:session
/*!
@constant AVFileTypeQuickTimeMovie
@abstract A UTI for the QuickTime movie file format.
@discussion
The value of this UTI is @"com.apple.quicktime-movie".
Files are identified with the .mov and .qt extensions.
*/
AVF_EXPORT NSString *const AVFileTypeQuickTimeMovie NS_AVAILABLE(10_7, 4_0);
/*!
@constant AVFileTypeMPEG4
@abstract A UTI for the MPEG-4 file format.
@discussion
The value of this UTI is @"public.mpeg-4".
Files are identified with the .mp4 extension.
*/
AVF_EXPORT NSString *const AVFileTypeMPEG4 NS_AVAILABLE(10_7, 4_0);
/*!
@constant AVFileTypeAppleM4V
@discussion
The value of this UTI is @"com.apple.m4v-video".
Files are identified with the .m4v extension.
*/
AVF_EXPORT NSString *const AVFileTypeAppleM4V NS_AVAILABLE(10_7, 4_0);
/*!
@constant AVFileTypeAppleM4A
@discussion
The value of this UTI is @"com.apple.m4a-audio".
Files are identified with the .m4a extension.
*/
AVF_EXPORT NSString *const AVFileTypeAppleM4A NS_AVAILABLE(10_7, 4_0);
/*!
@constant AVFileType3GPP
@abstract A UTI for the 3GPP file format.
@discussion
The value of this UTI is @"public.3gpp".
Files are identified with the .3gp, .3gpp, and .sdv extensions.
*/
AVF_EXPORT NSString *const AVFileType3GPP NS_AVAILABLE(10_11, 4_0);app
壓縮視頻有三級:ide
AVF_EXPORT NSString *const AVAssetExportPresetLowQuality NS_AVAILABLE(10_11, 4_0);
AVF_EXPORT NSString *const AVAssetExportPresetMediumQuality NS_AVAILABLE(10_11, 4_0);
AVF_EXPORT NSString *const AVAssetExportPresetHighestQuality NS_AVAILABLE(10_11, 4_0);測試
AVF_EXPORT NSString *const AVAssetExportPreset640x480 NS_AVAILABLE(10_7, 4_0);
AVF_EXPORT NSString *const AVAssetExportPreset960x540 NS_AVAILABLE(10_7, 4_0);
AVF_EXPORT NSString *const AVAssetExportPreset1280x720 NS_AVAILABLE(10_7, 4_0);
AVF_EXPORT NSString *const AVAssetExportPreset1920x1080 NS_AVAILABLE(10_7, 5_0);
AVF_EXPORT NSString *const AVAssetExportPreset3840x2160 NS_AVAILABLE(10_10, 9_0);ui
經測試:this
iPhone6高品質視頻:一分鐘 57mb 中品質壓縮後:6.7mb 低品質壓縮後:1.4mb 建議壓縮:中品質。壓縮時間大約1分鐘spa
同品質之間的壓縮不會壓縮大小,時間也相對短,如對中品質視頻進行中品質壓縮後,其大小不變,時長大約20s;code
另外:orm
視頻的傳入傳出的路徑爲file:格式 用:[NSURL fileURLWithPath:outpath];視頻