iOS视频压缩

//
//  ViewController.m
//  iOS视频测试
//
//  Created by apple on 15/8/19.
//  Copyright (c) 2015年 tqh. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()

@end

@implementation ViewController


//下载视频
- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path = NSHomeDirectory();//主目录
    NSLog(@"NSHomeDirectory:%@",path);
    //获取沙盒中的路径
    
    NSArray *storeFilePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    
    NSString *doucumentsDirectiory = [storeFilePath objectAtIndex:0];
    
    NSString *plistPath =[doucumentsDirectiory stringByAppendingPathComponent:@"mp4Test.mp4"];
    
    NSFileManager *file = [NSFileManager defaultManager];
    
    if ([file fileExistsAtPath:plistPath])
        
    {
        NSLog(@"沙盒中有视频");
    }
    else //若沙盒中没有
        
    {
        NSError *error;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"mp4Test" ofType:@"mp4"];
        [fileManager copyItemAtPath:bundle toPath:plistPath error:&error];
        NSLog(@"写入没有%d",[fileManager copyItemAtPath:bundle toPath:plistPath error:&error]);
        
    }

    NSString *newPlistPath =[doucumentsDirectiory stringByAppendingPathComponent:@"mp4Test5.mp4"];
    [self lowQuailtyWithInputURL:[NSURL fileURLWithPath:plistPath] outputURL:[NSURL fileURLWithPath:newPlistPath] blockHandler:^(AVAssetExportSession *session) {
        if (session.status == AVAssetExportSessionStatusCompleted)
        {
            NSLog(@"压缩完成");
        }else if (session.status == AVAssetExportSessionStatusFailed) {
            NSLog(@"压缩失败");
        }
    }];
}

#pragma mark - 视频压缩

- (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:AVAssetExportPresetLowQuality];
    session.outputURL = outputURL;
    session.outputFileType = AVFileTypeQuickTimeMovie;
    [session exportAsynchronouslyWithCompletionHandler:^(void)
     {
         handler(session);
     }];
}

@end

AVAssetExportPresetLowQuality:低质量压缩

AVF_EXPORT NSString *const AVAssetExportPresetLowQuality        NS_AVAILABLE_IOS(4_0);
AVF_EXPORT NSString *const AVAssetExportPresetMediumQuality     NS_AVAILABLE_IOS(4_0);
AVF_EXPORT NSString *const AVAssetExportPresetHighestQuality    NS_AVAILABLE_IOS(4_0);

原文地址:https://www.cnblogs.com/hxwj/p/4743088.html