zoukankan      html  css  js  c++  java
  • 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);

  • 相关阅读:
    10 行Python代码实现批量压缩图片500 张,面试必学
    用Python邮件发送,新手必学
    用Python监控男女朋友每天都在看哪些网站?这招绝了
    不踩坑的Python爬虫:如何在一个月内学会爬取大规模数据?新手必学
    用Python做一个520表白神器,值得收藏
    微分不等式
    零点问题
    2019英语一 Text4分析
    A1065. A+B and C(64bit)
    A1046. Shortest Distance
  • 原文地址:https://www.cnblogs.com/hxwj/p/4743088.html
Copyright © 2011-2022 走看看