iOS視頻壓縮存儲(chǔ)至本地并上傳至服務(wù)器實(shí)例代碼
最近做了一個(gè)項(xiàng)目,我把其中的核心功能拿出來(lái)和大家分享一下,重點(diǎn)還是自己梳理一下。
這里關(guān)于視頻轉(zhuǎn)碼存儲(chǔ)我整理了兩個(gè)方法,這兩個(gè)方法都是針對(duì)相冊(cè)內(nèi)視頻進(jìn)行處理的。
1、該方法沒(méi)有對(duì)視頻進(jìn)行壓縮,只是將視頻原封不動(dòng)地從相冊(cè)拿出來(lái)放到沙盒路徑下,目的是拿到視頻的NSData以便上傳
這里我傳了一個(gè)URL,這個(gè)URL有點(diǎn)特別,是相冊(cè)文件URL,所以我說(shuō)過(guò)只針對(duì)相冊(cè)視頻進(jìn)行處理
//將原始視頻的URL轉(zhuǎn)化為NSData數(shù)據(jù),寫(xiě)入沙盒
+ (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;
// 初始化一個(gè)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);//沒(méi)到結(jié)尾,沒(méi)出錯(cuò),ok繼續(xù)
}
// 釋放緩沖區(qū),關(guān)閉文件
free(buffer);
buffer = NULL;
fclose(file);
file = NULL;
}
} failureBlock:nil];
}
});
}
2、推薦使用該方法,該方法對(duì)視頻進(jìn)行壓縮處理,壓縮的程度可調(diào)
這里我傳的是模型過(guò)去,將我的URL帶過(guò)去的,然后壓縮完畢用模型把NSData帶出來(lái),數(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;
}
}
}];
}
在這里你可以修改壓縮比例,蘋(píng)果官方都封裝好了,根據(jù)需求調(diào)整
AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
在這里修改輸出類型,正常情況下選MP4不會(huì)有什么問(wèn)題的
exportSession.outputFileType = AVFileTypeMPEG4;
Mark一下圖片壓縮用這個(gè),image是圖片,0.4是比例,大小可調(diào)
model.fileData = UIImageJPEGRepresentation(image, 0.4);
這樣你就很愉快地拿到轉(zhuǎn)碼過(guò)后的NSData了,然后播放一下試試
MPMoviePlayerViewController* playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:sandBoxFilePath]]; [superVC presentViewController:playerView animated:YES completion:nil];
備注一下
可以發(fā)現(xiàn)我這里使用了沙盒存儲(chǔ),在下一節(jié)我整理一下用代碼管理應(yīng)用沙盒。
更新
最近發(fā)現(xiàn)好多人聯(lián)系我,問(wèn)我要Demo,最近我也整理了一下,目前掛在github上,望大神們指正。
https://github.com/Snoopy008/SelectVideoAndConvert
2017-3-23
偶然間幫一位好友看代碼,發(fā)現(xiàn)了一個(gè)更簡(jiǎn)單的獲取本地視頻的NSData的方法,大家自己看,我就不解釋了。代碼放在github上https://github.com/Snoopy008/videoData
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Xcode中Info.plist字段詳解
- IOS 開(kāi)發(fā)之?dāng)?shù)據(jù)存儲(chǔ)writeToFile的應(yīng)用實(shí)例
- iOS應(yīng)用中存儲(chǔ)用戶設(shè)置的plist文件的創(chuàng)建與讀寫(xiě)教程
- ios開(kāi)發(fā)Flutter之?dāng)?shù)據(jù)存儲(chǔ)
- iOS 本地存儲(chǔ)NSUserDefaults封裝代碼
- iOS 讀取URL圖片并存儲(chǔ)到本地的實(shí)例
- iOS開(kāi)發(fā)存儲(chǔ)應(yīng)用程序Info.plist知識(shí)全面詳解
相關(guān)文章
iOS實(shí)現(xiàn)列表與網(wǎng)格兩種視圖的相互切換
相信大家應(yīng)該也都發(fā)現(xiàn)了,在現(xiàn)在很多的電商app中,都會(huì)有列表視圖和網(wǎng)格兩種視圖的相互切換。例如京東和淘寶。這樣更利于提高用戶的體驗(yàn)度,所以這篇文章小編就是大家分享下利用iOS實(shí)現(xiàn)列表與網(wǎng)格兩種視圖相互切換的方法,文中介紹的很詳細(xì),感興趣的下面來(lái)一起看看吧。2016-10-10
iOS應(yīng)用開(kāi)發(fā)中監(jiān)聽(tīng)鍵盤(pán)事件的代碼實(shí)例小結(jié)
這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中監(jiān)聽(tīng)鍵盤(pán)事件的代碼實(shí)例小結(jié),呼出鍵盤(pán)等操作為iOS App中的必備功能,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
iOS UILabel 設(shè)置內(nèi)容的間距及高度的計(jì)算示例
本篇文章主要介紹了iOS UILabel 設(shè)置內(nèi)容的間距及高度的計(jì)算示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
iOS 簡(jiǎn)約日歷控件EBCalendarView的實(shí)現(xiàn)代碼
本篇文章主要介紹了iOS 簡(jiǎn)約日歷控件EBCalendarView的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

