视频压缩

#import 

// MARK:压缩视频 
+ (void)convertVideoQuailtyWithInputURL:(NSURL *)inputURL completeHandler:(void (^)(NSData *videoData))handler {
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
    NSString *fileName = [formater stringFromDate:[NSDate date]];
    NSString *videoFileName = [NSString stringWithFormat:@"Video_%@.mp4", fileName];
    //保存至沙盒路径
    NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *videoPath = [NSString stringWithFormat:@"%@/VideoInfo", pathDocuments];
    NSString *videoFilePath = [videoPath stringByAppendingPathComponent:videoFileName];
    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil];

    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];

    exportSession.outputURL = [NSURL fileURLWithPath:videoFilePath];
    exportSession.outputFileType = AVFileTypeMPEG4;
    exportSession.shouldOptimizeForNetworkUse = YES;

    __block NSData *data;
    [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:

                data = [NSData dataWithContentsOfFile:videoFilePath];
                NSLog(@"------视频大小%zd", data.length);
                handler(data);
                break;
            case AVAssetExportSessionStatusFailed:
                NSLog(@"AVAssetExportSessionStatusFailed");
                break;
        }
    }];
}

你可能感兴趣的:(视频压缩)