美文网首页音频视频压缩处理和播放
多媒体-图片、音频、视频压缩

多媒体-图片、音频、视频压缩

作者: 進无尽 | 来源:发表于2016-12-07 19:22 被阅读78次
    Paste_Image.png

    前言

    很多时候我们需要把一些图片、音频、视频上传到服务器,于是就有了文件压缩这个问题了,这篇文章就小结一下具体的文件压缩实现吧。


    图片压缩

    我们都知道苹果手机拍照后的照片格式的是JPG(全称是 JPEG),手机截图的格式是PNG的
    **压:是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降。**
    在Iphone上有两种读取图片数据的简单方法
    **UIImageJPEGRepresentation和UIImagePNGRepresentation.**
    UIImageJPEGRepresentation
    函数需要两个参数:图片的引用和压缩系数而UIImagePNGRepresentation
    只需要图片引用作为参数.
    UIImagePNGRepresentation(UIImage \*image)
    要比UIImageJPEGRepresentation(UIImage* image, 1.0)
    返回的图片数据量大很多.
    同样的一张照片, 使用UIImagePNGRepresentation(image)
    返回的数据量大小为199K
    而 UIImageJPEGRepresentation(image, 1.0)返回的数据量大小只为140K,比前者少了59K
    
       如果对图片的清晰度要求不是极高,建议使用UIImageJPEGRepresentation
    ,可以大幅度降低图片数据量.比如,刚才拍摄的图片,通过调用    
    UIImageJPEGRepresentation(image, 1.0)读取数据时,返回的数据大小为140K
    ,但更改压缩系数为0.5再读取数据时,返回的数据大小只有11K
    #两者差了14倍
    ,大大压缩了图片的数据量,而且清晰度并没有相差多少,图片的质量并没有明显的降低。因此,
    在读取图片数据内容时,建议优先使用UIImageJPEGRepresentation,
    并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小。
    
       CGFloat dividend = 1024.0 * 1024.0;
        //得到图片的data
        NSData *data = [self imageConversationDataWith:upImage];
        //判断图片所占内存的大小
        CGFloat memory = data.length / dividend;
        // 循环压缩图片, 直到满足要求
        while (memory > 2.0) {
            upImage = [self scaleToSize:upImage scale:0.9];
            data = [self imageConversationDataWith:upImage];
            memory = data.length / dividend;
        }
    
    # 内存压
    - (NSData *)imageConversationDataWith:(UIImage *)image {
         NSData *data;
        if (UIImagePNGRepresentation(image)) {
          // png图像。
         data = UIImagePNGRepresentation(image);
       }else {
          // JPEG图像。
          data = UIImageJPEGRepresentation(image, 1);
       }
        return data;
      }
    
    #缩  是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体积同样会减小。
    
    - (UIImage *)scaleToSize:(UIImage *)image scale:(CGFloat)scale{
     // 创建一个bitmap的context
      CGSize size = CGSizeMake(image.size.width * scale, image.size.height * scale);
      // 并把它设置成为当前正在使用的context
      UIGraphicsBeginImageContext(size);
      // 绘制改变大小的图片
      [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
      // 从当前context中创建一个改变大小后的图片
      UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
      // 使当前的context出堆栈
      UIGraphicsEndImageContext();
      // 返回新的改变大小后的图片
      return scaledImage;
    }
    

    音频压缩:

    以10s为例: wav格式 167k

    Snip20161127_1.png

    可以看的出 相同时长 wav格式的内存是最大的,mp3编码格式的文件占用的内存是最小的,10倍的差别
    使用libmp3lame 进行MP3编码格式的转码

    Snip20161207_2.png
    #include "lame.h"
    
    //经过转化,音频文件的大小得到了很大的压缩
    //这里的转化,隐藏了两个参数 :原始路径cafPathStr    输出路径 mp3PathStr
    #pragma mark - caf转mp3 
    - (void)audio_PCMtoMP3
     {
    @try {
        int read, write;
        
        FILE *pcm = fopen([cafPathStr cStringUsingEncoding:1], "rb");  //source 被转换的音频文件位置
        fseek(pcm, 4*1024, SEEK_CUR);                                   //skip file header
        FILE *mp3 = fopen([mp3PathStr cStringUsingEncoding:1], "wb");  //output 输出生成的Mp3文件位置
        
        const int PCM_SIZE = 8192;
        const int MP3_SIZE = 8192;
        short int pcm_buffer[PCM_SIZE*2];
        unsigned char mp3_buffer[MP3_SIZE];
        
        lame_t lame = lame_init();
        lame_set_in_samplerate(lame, 11025.0);
        lame_set_VBR(lame, vbr_default);
        lame_init_params(lame);
        
        do {
            read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
            if (read == 0)
                write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
            else
                write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
            
            fwrite(mp3_buffer, write, 1, mp3);
            
        } while (read != 0);
        
        lame_close(lame);
        fclose(mp3);
        fclose(pcm);
    }
        @catch (NSException *exception) {
            NSLog(@"%@",[exception description]);
        }
          @finally {
            NSLog(@"MP3生成成功: %@",mp3PathStr);
          }
    }
    *******************************************************
     //计算文件大小
    long long fileSize = [self fileSizeAtPath:mp3PathStr]/1024.0;
    
    //单个文件的大小
    - (long long) fileSizeAtPath:(NSString*)filePath{
         NSFileManager* manager = [NSFileManager defaultManager];
          if ([manager fileExistsAtPath:filePath]){
          return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
           }else{
            NSLog(@"计算文件大小:文件不存在");
        }
        return 0;
    } 
    

    视频压缩

    以2s为例

    未经压缩的视频大小有 8968k 而经过压缩的视频长度有 240k,俩者差了 40多倍

    # iOS录制的视频是mov格式的,安卓和PC不支持,因此要转换成MP4,并且要压缩。
    # inputURL原始文件路径、 outputURL 压缩后文件的路径
    - (void) convertVideoQuailtyWithInputURL:(NSURL*)inputURL
                               outputURL:(NSURL*)outputURL
                         completeHandler:(void (^)(AVAssetExportSession*))handler
    {
    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
    //  NSLog(resultPath);
    exportSession.outputURL = outputURL;
    exportSession.outputFileType = AVFileTypeMPEG4;
    exportSession.shouldOptimizeForNetworkUse= YES;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void)
     {
         switch (exportSession.status) {
             case AVAssetExportSessionStatusCancelled:
                 NSLog(@"AVAssetExportSessionStatusCancelled");
                 break;
             case AVAssetExportSessionStatusUnknown:
                 NSLog(@"AVAssetExportSessionStatusUnknown");
                 break;
             case AVAssetExportSessionStatusWaiting:
                 NSLog(@"AVAssetExportSessionStatusWaiting");
                 break;
             case AVAssetExportSessionStatusExporting:
                 NSLog(@"AVAssetExportSessionStatusExporting");
                 break;
             case AVAssetExportSessionStatusCompleted:
                 NSLog(@"AVAssetExportSessionStatusCompleted");
                 NSLog(@"%@",[NSString stringWithFormat:@"33 %f s", [self getVideoLength:outputURL]]);
                 NSLog(@"%@", [NSString stringWithFormat:@"44 %.2f kb", [self getFileSize:[outputURL path]]]);
                 
                 //UISaveVideoAtPathToSavedPhotosAlbum([outputURL path], self, nil, NULL);//这个是保存到手机相册
                 vidoePathUrL = outputURL;
                // [self alertUploadVideo:outputURL];
                 
                 break;
             case AVAssetExportSessionStatusFailed:
                 NSLog(@"AVAssetExportSessionStatusFailed");
                 break;
         } 
     }];
    }
    
    小结

    通过上文的分析,图片压缩后比原文件小了 14倍,音频文件比之前小了10多倍,而视频文件比之前小了40多倍,OMG,由此可以看出,文件上传中的压缩工作是多么的必要啊!

    相关文章

      网友评论

        本文标题:多媒体-图片、音频、视频压缩

        本文链接:https://www.haomeiwen.com/subject/ywxputtx.html