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