最近做了一个项目,我把其中的核心功能拿出来和大家分享一下,重点还是自己梳理一下。

这里关于视频转码存储我整理了两个方法,这两个方法都是针对相册内视频进行处理的。 
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视频压缩存储至本地并上传至服务器的更多相关文章

  1. iOS视频压缩存储至本地并上传至服务器-b

    最近做了一个项目,我把其中的核心功能拿出来和大家分享一下,重点还是自己梳理一下. 这里关于视频转码存储我整理了两个方法,这两个方法都是针对相册内视频进行处理的. 1.该方法没有对视频进行压缩,只是将视 ...

  2. 使用Navicat for MySQL把本地数据库上传到服务器

    服务器系统基本都是基于linux的,这个数据库上传的方式适用于linux的各种版本,比如Ubuntu和Centos(尽管这两个版本各种大坑小坑,但至少在数据库传输上保持了一致性) 当然本地数据库上传到 ...

  3. Linux 将本地文件上传Linux服务器, 即ssh 命令上传本地文件

    利用ssh传输文件   在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件 scp username@servername:/path/filename /var/www ...

  4. Linux 将本地文件上传Linux服务器, 即ssh 命令上传本地文件

    http://blog.csdn.net/rodulf/article/details/71169996 利用ssh传输文件 在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下 ...

  5. c#将本地文件上传至服务器(内网)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. ubuntu中将本地文件上传到服务器

    (1)在本地的终端下,而不是在服务器上.在本地的终端上才能将本地的文件拷入服务器. (2) scp -r localfile.txt username@192.168.0.1:/home/userna ...

  7. C# 把本地文件上传到服务器上,和从服务器上下载文件

    方法一.通过Ajax方式上传文件(input file),使用FormData进行Ajax请求 <div  > <input type="file" name=& ...

  8. 借助XShell,使用linux命令sz可以很方便的将服务器上的文件下载到本地,使用rz命令则是把本地文件上传到服务器。

    rz 是将window文件传到linux服务器上,到执行rz命令的目录 sz 可以将linux文件发送到windows上,可以选择目录. https://www.google.com/ncr 登录一下 ...

  9. asp.net将本地Excel上传到服务器并把数据导入到数据库

    前台代码: <td class="formLabel"> 批量修改: </td> <td class="formInput"> ...

随机推荐

  1. python基础-匿名函数、内置函数、正则表达式、模块

    1. 匿名函数 1.1 有名函数 有名函数:定义了一个函数名,函数名指向内存地址:通过函数名进行访问.函数名加括号就可以运行有名函数,例如:func() def func(x, y, z = 1): ...

  2. 搭建redis集群环境

    Redis的集群机制 ============================= 转自http://lib.csdn.net/article/redis/39999 别人写的,写得不错,转了. Red ...

  3. mac 安装PyQt5

    PyQt5官方安装教程指出2种安装方法: Installing from Wheels Building and Installing from Source 网上搜罗的大多是按照第二种方法安装的,本 ...

  4. camera主观测试经验分享.ppt33页

    http://max.book118.com/html/2016/0802/50061502.shtm http://www.docin.com/p-1408441708.html

  5. 【Hadoop】用 Ganglia 监控hadoop集群

    随着数据中心的增长和管理人员的缩减,对计算资源使用有效监视工具的需求变得比以往更加迫切.术语监视 在应用到数据中心时可能会让人混淆,因为它的含义会根据具体的说话者和听众而有所不同.例如: 在集群中运行 ...

  6. [Javascript] Deep merge in Javascript with Ramda.js mergeDeepWith

    Javascript's Object.assign is shadow merge, loadsh's _.merge is deep merge, but has probem for array ...

  7. MPTCP 源码分析(四) 发送和接收数据

    简述:      MPTCP在发送数据方面和TCP的区别是可以从多条路径中选择一条 路径来发送数据.MPTCP在接收数据方面与TCP的区别是子路径对无序包 进行重排后,MPTCP的mpcb需要多所有子 ...

  8. Charles 抓HTTPS包报以下错误:

    1.You may need to configure your browser or application to trust the Charles Root Certificate. See S ...

  9. unity3D打造skybox淡入淡出 - 移动开发

    原地址:http://www.it2down.com/it-mobile/426479.htm 当前位置: IT异常查询网 » unity3D打造skybox淡入淡出 - 移动开发 www.it2do ...

  10. c和c++在windows下获取时间和计算时间差的方法总结

    c/c++在windows下获取时间和计算时间差的几种方法总结 一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double diff ...