码迷,mamicode.com
首页 > 移动开发 > 详细

iOS视频压缩

时间:2015-08-19 20:16:26      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

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

iOS视频压缩

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!