標籤:
最近做了一個項目,我把其中的核心功能拿出來和大家分享一下,重點還是自己梳理一下。
這裡關於視頻轉碼儲存我整理了兩個方法,這兩個方法都是針對相簿內視頻進行處理的。
1、該方法沒有對視頻進行壓縮,只是將視頻原封不動地從相簿拿出來放到沙箱路徑下,目的是拿到視頻的NSData以便上傳
這裡我傳了一個URL,這個URL有點特別,是相簿檔案URL,所以我說過只針對相簿視頻進行處理
//將原始視頻的URL轉化為NSData資料,寫入沙箱 + (void)videoWithUrl:(NSString *)url withFileName:(NSString *)fileName { ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{ if (url) { [assetLibrary assetForURL:[NSURL URLWithString:url] resultBlock:^(ALAsset *asset) { ALAssetRepresentation *rep = [asset defaultRepresentation]; NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *imagePath = [NSString stringWithFormat:@"%@/Image", pathDocuments]; NSString *dbFilePath = [imagePath stringByAppendingPathComponent:fileName]; char const *cvideoPath = [dbFilePath UTF8String]; FILE *file = fopen(cvideoPath, "a+"); if (file) { const int bufferSize = 11024 * 1024; // 初始化一個1M的buffer Byte *buffer = (Byte*)malloc(bufferSize); NSUInteger read = 0, offset = 0, written = 0; NSError* err = nil; if (rep.size != 0) { do { read = [rep getBytes:buffer fromOffset:offset length:bufferSize error:&err]; written = fwrite(buffer, sizeof(char), read, file); offset += read; } while (read != 0 && !err);//沒到結尾,沒出錯,ok繼續 } // 釋放緩衝區,關閉檔案 free(buffer); buffer = NULL; fclose(file); file = NULL; } } failureBlock:nil]; } });}
2、推薦使用該方法,該方法對視頻進行壓縮處理,壓縮的程度可調
這裡我傳的是模型過去,將我的URL帶過去的,然後壓縮完畢用模型把NSData帶出來,資料大家根據自己需求自由發揮
+ (void) convertVideoWithModel:(RZProjectFileModel *) model { model.filename = [NSString stringWithFormat:@"%ld.mp4",RandomNum]; //儲存至沙箱路徑 NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *videoPath = [NSString stringWithFormat:@"%@/Image", pathDocuments]; model.sandBoxFilePath = [videoPath stringByAppendingPathComponent:model.filename]; //轉碼配置 AVURLAsset *asset = [AVURLAsset URLAssetWithURL:model.assetFilePath options:nil]; AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality]; exportSession.shouldOptimizeForNetworkUse = YES; exportSession.outputURL = [NSURL fileURLWithPath:model.sandBoxFilePath]; exportSession.outputFileType = AVFileTypeMPEG4; [exportSession exportAsynchronouslyWithCompletionHandler:^{ int exportStatus = exportSession.status; RZLog(@"%d",exportStatus); switch (exportStatus) { case AVAssetExportSessionStatusFailed: { // log error to text view NSError *exportError = exportSession.error; NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError); break; } case AVAssetExportSessionStatusCompleted: { RZLog(@"視頻轉碼成功"); NSData *data = [NSData dataWithContentsOfFile:model.sandBoxFilePath]; model.fileData = data; } } }]; }
在這裡你可以修改壓縮比例,蘋果官方都封裝好了,根據需求調整
AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
在這裡修改輸出類型,正常情況下選MP4不會有什麼問題的
exportSession.outputFileType = AVFileTypeMPEG4;
Mark一片壓縮用這個,image是圖片,0.4是比例,大小可調
model.fileData = UIImageJPEGRepresentation(image, 0.4);
這樣你就很愉快地拿到轉碼過後的NSData了,然後播放一下試試
MPMoviePlayerViewController* playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:sandBoxFilePath]]; [superVC presentViewController:playerView animated:YES completion:nil];
備忘一下
可以發現我這裡使用了沙箱儲存,在下一節我整理一下用代碼管理應用沙箱。
更新
最近發現好多人聯絡我,問我要Demo,最近我也整理了一下,目前掛在github上,望大神們指正。https://github.com/Snoopy008/SelectVideoAndConvert
文/Snoopy008(簡書作者)
原文連結:http://www.jianshu.com/p/1eeaec2ae0fa
轉自:http://blog.5ibc.net/p/89566.html
iOS視頻壓縮儲存至本地並上傳至伺服器