视频压缩转码上传到服务器
最近比较项目中有一个视频上传的需求,可以支持多选的。找了一些资料都是比较乱。后来就是通过一个好友的帮助。自己总结了一下,实现了这个需求,但是还是有点小问题有待解决。这里就给大家分享一下。
1,首先要获取到本地的视频
2,开始选取视频,可以定一个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上面,我项目中做的是类似于图片的这种格式
4,步骤如下:点击加好按钮进行选择,之后返回选择的视频的数组,下边有一步重要的压缩转码操作接下来会贴出方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{NSLog(@"您点击了添加");
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;
//压缩转码操作
[self convertVideoToData:fileModell];
[fileModelArray addObject:fileModell];
[_ImageArr addObject:model.thumbnail];
NSLog(@"111%@",_ImageArr);
[_collectionview reloadData];}
}];
[self presentViewController:picker animated:YES completion:nil];}
}
5,压缩转码操作
//压缩转码
- (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];
//转码配置
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 = @"不能创建文件";
[_hud1 hide:YES afterDelay:0.5];
break;}
case AVAssetExportSessionStatusCompleted:{
NSLog(@"视频转码成功");
//视频的NSData数据在模型fileData属性里
NSError *error;
NSData * data = [NSData dataWithContentsOfFile:model.sandBoxPath options:
(NSDataReadingMappedIfSafe) error:&error];
model.fileData = data;
[_VoideData addObject:data];
NSLog(@"%@",model.sandBoxPath);
//判断多选的情况下,什么时候视频全部转码成功
if (_VoideData.count==fileModelArray.count) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"全部结束");
[_hud1 hide:YES];});
}
break;}
}
}];
}
6,将视频文件保存至本地 此方法要先调用,因为首先要开辟储存空间,写在ViewDidLoad里面调用
- (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];
//创建目录
_createPath = [NSString stringWithFormat:@"%@/Video",pathDocuments];
// 判断文件夹是否存在,如果不存在,则创建
if (![[NSFileManager defaultManager] fileExistsAtPath:_createPath]) {
[fileManager createDirectoryAtPath:_createPath withIntermediateDirectories:YES attributes:nil error:nil];} else {
NSLog(@"FileImage is exists.");}}
7,上传的话每个人都有每个人的方法,可以用AFN进行上传,注意上传的时候是上传的NsData数据。这里我就不贴代码了。