最近忙於項目開發, 昨天才完成整個項目的開發, 今天就抽出時間, 分享一下我在開發中所涉及到的技術問題!
由於近期開發涉及到視頻, 所以視頻壓縮, 上傳, 播放等一系列功能都是要涉及到的, 所以在此, 我就跟大家分享一下視頻壓縮!
iOS 視頻壓縮問題, 我在網上也找了資料, 但是不多, 也不夠詳細全面, 我就自己寫了一個壓縮的小demo, 用到的是系統的一個類庫
#import <AVFoundation/AVFoundation.h> 中 AVAssetExportSession 這個類, 官方API 是這樣解釋說明的, AVAssetExportSession 是對AVAsset對象內容進行轉碼, 並輸出到制定的路徑;
- (nullable instancetype)initWithAsset:(AVAsset *)asset presetName:(NSString *)presetName NS_DESIGNATED_INITIALIZER; 初始化方法
asset, 參數為要轉碼的asset 對象, presetName, 該參數為要進行轉碼的方式名稱, 為字符串類型, 系統有給定的類型值,
AVAssetExportPresetLowQuality
AVAssetExportPresetMediumQuality
VAssetExportPresetHighestQuality
AVAssetExportPreset640x480
AVAssetExportPreset1280x720
outputURL , 為輸出內容的URL, (指定一個文件路徑, 然后根據路徑初始化一個URL, 賦給, outputURL)
outputFileType, 為輸出壓縮后視頻內容的格式類型
Demo 下載地址
https://github.com/jessonliu/JFUpLoadVideo.git
http://code.cocoachina.com/view/131886
下邊直接上代碼, 如有錯誤, 或有不好的地方, 歡迎大家指正, 本人虛心學習求教, 與大家共勉!
// Created by iOS-Developer on 16/2/19. // Copyright © 2016年 iOS-Jessonliu. All rights reserved. // #import "JFCompressionVideo.h" #import <AVFoundation/AVFoundation.h> #define CompressionVideoPaht [NSHomeDirectory() stringByAppendingFormat:@"/Documents/CompressionVideoField"] @interface JFCompressionVideo () @end @implementation JFCompressionVideo + (void)compressedVideoOtherMethodWithURL:(NSURL *)url compressionType:(NSString *)compressionType compressionResultPath:(CompressionSuccessBlock)resultPathBlock { NSString *resultPath; NSData *data = [NSData dataWithContentsOfURL:url]; CGFloat totalSize = (float)data.length / 1024 / 1024; AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil]; NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset]; // 所支持的壓縮格式中是否有 所選的壓縮格式 if ([compatiblePresets containsObject:compressionType]) { AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:compressionType]; NSDateFormatter *formater = [[NSDateFormatter alloc] init];// 用時間, 給文件重新命名, 防止視頻存儲覆蓋, [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"]; NSFileManager *manager = [NSFileManager defaultManager]; BOOL isExists = [manager fileExistsAtPath:CompressionVideoPaht]; if (!isExists) { [manager createDirectoryAtPath:CompressionVideoPaht withIntermediateDirectories:YES attributes:nil error:nil]; } resultPath = [CompressionVideoPaht stringByAppendingPathComponent:[NSString stringWithFormat:@"outputJFVideo-%@.mov", [formater stringFromDate:[NSDate date]]]]; JFLog(@"resultPath = %@",resultPath); exportSession.outputURL = [NSURL fileURLWithPath:resultPath]; exportSession.outputFileType = AVFileTypeMPEG4; exportSession.shouldOptimizeForNetworkUse = YES; [exportSession exportAsynchronouslyWithCompletionHandler:^(void) { if (exportSession.status == AVAssetExportSessionStatusCompleted) { NSData *data = [NSData dataWithContentsOfFile:resultPath]; float memorySize = (float)data.length / 1024 / 1024; JFLog(@"視頻壓縮后大小 %f", memorySize); resultPathBlock (resultPath, memorySize); } else { JFLog(@"壓縮失敗"); } }]; } else { JFLog(@"不支持 %@ 格式的壓縮", compressionType); } }
/**
* 清楚沙盒文件中, 壓縮后的視頻所有, 在使用過壓縮文件后, 不進行再次使用時, 可調用該方法, 進行刪除
*/
+ (void)removeCompressedVideoFromDocuments { NSFileManager *manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:CompressionVideoPaht]) { [[NSFileManager defaultManager] removeItemAtPath:CompressionVideoPaht error:nil]; } } @end
