端午節(jié)快到了魄健,提前祝大家節(jié)日快樂。擔(dān)心端午沒時間寫插勤,今天抽空寫一下沽瘦。
最近做了一個項目革骨,我把其中的核心功能拿出來和大家分享一下,重點還是自己梳理一下析恋。
這里關(guān)于視頻轉(zhuǎn)碼存儲我整理了兩個方法良哲,這兩個方法都是針對相冊內(nèi)視頻進行處理的。
1助隧、該方法沒有對視頻進行壓縮筑凫,只是將視頻原封不動地從相冊拿出來放到沙盒路徑下,目的是拿到視頻的NSData以便上傳####
這里我傳了一個URL并村,這個URL有點特別巍实,是相冊文件URL,所以我說過只針對相冊視頻進行處理
//將原始視頻的URL轉(zhuǎn)化為NSData數(shù)據(jù),寫入沙盒
+ (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);//沒到結(jié)尾橘霎,沒出錯蔫浆,ok繼續(xù)
}
// 釋放緩沖區(qū),關(guān)閉文件
free(buffer);
buffer = NULL;
fclose(file);
file = NULL;
}
} failureBlock:nil];
}
});
}
2姐叁、推薦使用該方法瓦盛,該方法對視頻進行壓縮處理,壓縮的程度可調(diào)####
這里我傳的是模型過去外潜,將我的URL帶過去的原环,然后壓縮完畢用模型把NSData帶出來,數(shù)據(jù)大家根據(jù)自己需求自由發(fā)揮
+ (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];
//轉(zhuǎn)碼配置
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(@"視頻轉(zhuǎn)碼成功");
NSData *data = [NSData dataWithContentsOfFile:model.sandBoxFilePath];
model.fileData = data;
}
}
}];
}
在這里你可以修改壓縮比例处窥,蘋果官方都封裝好了嘱吗,根據(jù)需求調(diào)整
AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
在這里修改輸出類型,正常情況下選MP4不會有什么問題的
exportSession.outputFileType = AVFileTypeMPEG4;
Mark一下圖片壓縮用這個滔驾,image是圖片谒麦,0.4是比例,大小可調(diào)
model.fileData = UIImageJPEGRepresentation(image, 0.4);
這樣你就很愉快地拿到轉(zhuǎn)碼過后的NSData了哆致,然后播放一下試試
MPMoviePlayerViewController* playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:sandBoxFilePath]];
[superVC presentViewController:playerView animated:YES completion:nil];
備注一下###
可以發(fā)現(xiàn)我這里使用了沙盒存儲绕德,在下一節(jié)我整理一下用代碼管理應(yīng)用沙盒。
更新#
最近發(fā)現(xiàn)好多人聯(lián)系我摊阀,問我要Demo耻蛇,最近我也整理了一下,目前掛在github上胞此,望大神們指正臣咖。https://github.com/Snoopy008/SelectVideoAndConvert
(如果我的代碼對你有幫助,請你不忘在github上給我個Star漱牵,謝謝)夺蛇。
2017-3-23#
偶然間幫一位好友看代碼,發(fā)現(xiàn)了一個更簡單的獲取本地視頻的NSData的方法酣胀,大家自己看蚊惯,我就不解釋了愿卸。代碼放在github上https://github.com/Snoopy008/videoData