視頻壓縮轉(zhuǎn)碼上傳到服務(wù)器

最近比較項(xiàng)目中有一個(gè)視頻上傳的需求,可以支持多選的。找了一些資料都是比較亂。后來就是通過一個(gè)好友的幫助。自己總結(jié)了一下,實(shí)現(xiàn)了這個(gè)需求,但是還是有點(diǎn)小問題有待解決。這里就給大家分享一下。

1,首先要獲取到本地的視頻

2,開始選取視頻,可以定一個(gè)Model這樣用起來比較方便

偽代碼如下:

#import@interface GCMAssetModel : NSObject

@property (nonatomic,strong) UIImage *thumbnail;//縮略圖

@property (nonatomic,copy) NSURL *imageURL;//原圖url

@property (nonatomic,assign) BOOL isSelected;//是否被選中

@property (nonatomic,assign) BOOL isImage;//是否是圖片

- (void)originalImage:(void (^)(UIImage *image))returnImage;//獲取原圖

@end

3,選取視頻展示到Collectionview上面,我項(xiàng)目中做的是類似于圖片的這種格式


由于模擬機(jī)上沒有視頻,就拿圖片上傳展示一下,效果是一樣的


4,步驟如下:點(diǎn)擊加好按鈕進(jìn)行選擇,之后返回選擇的視頻的數(shù)組,下邊有一步重要的壓縮轉(zhuǎn)碼操作接下來會貼出方法

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{NSLog(@"您點(diǎn)擊了添加");

if (indexPath.row == _ImageArr.count) {

GCMImagePickerController *picker = [[GCMImagePickerController alloc] init];

//返回選中的原圖

[picker setDidFinishSelectImageModels:^(NSMutableArray *models) {

// NSLog(@"原圖%@",models);

for (GCMAssetModel *model in models) {

FileModel *fileModell = [[FileModel alloc] init];

fileModell.filename = [NSString stringWithFormat:@"%ld.mp4",RandomNum];

fileModell.fileAssetURL = model.imageURL;

//壓縮轉(zhuǎn)碼操作

[self convertVideoToData:fileModell];

[fileModelArray addObject:fileModell];

[_ImageArr addObject:model.thumbnail];

NSLog(@"111%@",_ImageArr);

[_collectionview reloadData];}

}];

[self presentViewController:picker animated:YES completion:nil];}

}

5,壓縮轉(zhuǎn)碼操作

//壓縮轉(zhuǎn)碼

- (void)convertVideoToData:(FileModel *)model

{//保存至沙盒路徑

NSString *pathDocuments =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSLog(@"%@",pathDocuments);

NSString *videoPath = [NSString stringWithFormat:@"%@/Video",pathDocuments];

model.sandBoxPath = [videoPath stringByAppendingPathComponent:model.filename];

//轉(zhuǎn)碼配置

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:model.fileAssetURL options:nil];

AVAssetExportSession *exportSession= [[AVAssetExportSession alloc]

initWithAsset:asset presetName:AVAssetExportPresetLowQuality];

exportSession.shouldOptimizeForNetworkUse = YES;

exportSession.outputURL = [NSURL fileURLWithPath:model.sandBoxPath];

exportSession.outputFileType = AVFileTypeMPEG4;

NSArray *compatiblePresets = [AVAssetExportSession

exportPresetsCompatibleWithAsset:asset];

NSLog(@"%@",compatiblePresets);

[exportSession exportAsynchronouslyWithCompletionHandler:^{

int exportStatus = exportSession.status;

NSLog(@"%d",exportStatus);

switch (exportStatus)

{

case AVAssetExportSessionStatusFailed:{

_hud1.labelText = @"不能創(chuàng)建文件";

[_hud1 hide:YES afterDelay:0.5];

break;}

case AVAssetExportSessionStatusCompleted:{

NSLog(@"視頻轉(zhuǎn)碼成功");

//視頻的NSData數(shù)據(jù)在模型fileData屬性里

NSError *error;

NSData * data = [NSData dataWithContentsOfFile:model.sandBoxPath options:

(NSDataReadingMappedIfSafe) error:&error];

model.fileData = data;

[_VoideData addObject:data];

NSLog(@"%@",model.sandBoxPath);

//判斷多選的情況下,什么時(shí)候視頻全部轉(zhuǎn)碼成功

if (_VoideData.count==fileModelArray.count) {

dispatch_async(dispatch_get_main_queue(), ^{

NSLog(@"全部結(jié)束");

[_hud1 hide:YES];});

}

break;}

}

}];

}


6,將視頻文件保存至本地 此方法要先調(diào)用,因?yàn)槭紫纫_辟儲存空間,寫在ViewDidLoad里面調(diào)用

- (void)creatSandBoxFilePathIfNoExist{

//沙盒路徑

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

NSString *documentDirectory = [paths objectAtIndex:0];

NSLog(@"databse--->%@",documentDirectory);

NSFileManager *fileManager = [[NSFileManager alloc] init];

NSString *pathDocuments =

[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

//創(chuàng)建目錄

_createPath = [NSString stringWithFormat:@"%@/Video",pathDocuments];

// 判斷文件夾是否存在,如果不存在,則創(chuàng)建

if (![[NSFileManager defaultManager] fileExistsAtPath:_createPath]) {

[fileManager createDirectoryAtPath:_createPath withIntermediateDirectories:YES attributes:nil error:nil];} else {

NSLog(@"FileImage is exists.");}}

7,上傳的話每個(gè)人都有每個(gè)人的方法,可以用AFN進(jìn)行上傳,注意上傳的時(shí)候是上傳的NsData數(shù)據(jù)。這里我就不貼代碼了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容