AFN上传并压缩视频详细版

1.获取视频路径

AFN上传并压缩视频详细版_第1张图片
73768579-E2E2-438C-BB3A-CB4D3528BD56.png

2.创建压缩视频类
.h文件

import

import

@interface DdcompressVideo : NSObject
/**

  • 压缩成功Block
  • @param resultPath 返回压缩成功的视频路径
    /
    typedef void (^CompressionSuccessBlock)(NSString resultPath,float memorySize); // 定义成功的Block 函数
    /
  • method Comperssion Video 压缩视频的方法, 该方法将压缩过的视频保存到沙河文件, 如果压缩过的视频不需要再进行保留, 可调用 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.调用

AFN上传并压缩视频详细版_第2张图片
4C5BD19E-DDD9-44CB-AF75-B7AF267BFBAC.png

你可能感兴趣的:(AFN上传并压缩视频详细版)