• ios 视频/图片压缩


    - (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;
    }

    视频压缩格式:

    /*!
     @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);

    压缩视频有三级:

    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);

    经测试:

    iPhone6高品质视频:一分钟 57mb   中品质压缩后:6.7mb  低品质压缩后:1.4mb  建议压缩:中品质。压缩时间大约1分钟

    同品质之间的压缩不会压缩大小,时间也相对短,如对中品质视频进行中品质压缩后,其大小不变,时长大约20s;

    另外:

    视频的传入传出的路径为file:格式 用:[NSURL fileURLWithPath:outpath];

  • 相关阅读:
    CentOS 6.3下Samba服务器的安装与配置(转)
    利用香蕉派自制电视盒子
    利用arduino制作瓦力万年历-1.0
    arduino:int & double 转string 适合12864下使用
    centos 6.X下建立arduino开发环境
    树莓派学习笔记(7):利用bypy实现树莓派NAS同步百度云
    直接插入排序
    直接选择排序
    快速排序算法
    git 分支管理 推送本地分支到远程分支等
  • 原文地址:https://www.cnblogs.com/chaochaobuhuifei55/p/5260927.html
Copyright © 2020-2023  润新知