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

  • 相关阅读:
    分享几个原生javascript面向对象设计小游戏
    原生javascript模仿win8等待进度条。
    微信公众平台入门开发教程.Net(C#)框架
    AsyncDelegate
    Lock
    BackgroundWorker
    Thread
    深拷贝-浅拷贝
    WindowsService
    事件本质
  • 原文地址:https://www.cnblogs.com/hxwj/p/4743088.html
Copyright © 2011-2022 走看看