iOS視頻壓縮有很多方法,今天發現使用系統自帶的AVAssetExportSession對視頻進行壓縮很方便,壓縮的還不錯,有設置壓縮質量和導出視頻格式的API,壓縮結果是個block回調,可以在這裡做一些處理完的事情
/** * 視頻壓縮 * * @param inputURL 傳入的URL * @param outputURL 輸出的URL * @param handler 返回的block */ - (void)lowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL blockHandler:(void (^)(AVAssetExportSession*))handler { AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil]; /* 視頻壓縮質量選項 AVAssetExportPresetLowQuality AVAssetExportPresetMediumQuality AVAssetExportPresetHighestQuality */ AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality]; session.outputURL = outputURL; /* 輸出視頻格式選項 AVFileTypeQuickTimeMovie AVFileTypeMPEG4 AVFileTypeAppleM4V AVFileTypeAppleM4A AVFileType3GPP AVFileType3GPP2 */ session.outputFileType = AVFileTypeQuickTimeMovie; session.shouldOptimizeForNetworkUse = YES; [session exportAsynchronouslyWithCompletionHandler:^(void) { handler(session); }]; }