七牛
调起相机
// 跳转到相机
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;//sourcetype有三种分别是camera,photoLibrary和photoAlbum
NSArray *availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];//Camera所支持的Media格式都有哪些,共有两个分别是@"public.image",@"public.movie"
ipc.videoQuality = UIImagePickerControllerQualityTypeMedium;
ipc.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];//设置媒体类型为public.movie
[self presentViewController:ipc animated:YES completion:nil];
ipc.videoMaximumDuration = [self.model.videoMaximumDuration floatValue] == 0 ? 30 : [self.model.videoMaximumDuration floatValue];//30秒 //视频最长时间
ipc.delegate = self;//设置委托
获取视频
UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"---------------%@", info);
[picker dismissViewControllerAnimated:YES completion:^{}];
if ([[info objectForKey:@"UIImagePickerControllerMediaType"] isEqualToString:@"public.movie"]) {//选了视频
NSURL *sourceURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"完成视频录制----视频时间===%@",[NSString stringWithFormat:@"%f s", [self getVideoLength:sourceURL]]);
NSLog(@"完成视频录制----视频文件大小===%@", [NSString stringWithFormat:@"%.2f kb", [self getFileSize:[sourceURL path]]]);
NSURL *newVideoUrl ; //一般.mp4
NSDateFormatter *formater = [[NSDateFormatter alloc] init];//用时间给文件全名,以免重复,在测试的时候其实可以判断文件是否存在若存在,则删除,重新生成文件即可
[formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
NSString *file = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4", [formater stringFromDate:[NSDate date]]];
newVideoUrl = [NSURL fileURLWithPath:file] ;//这个是保存在app自己的沙盒路径里,后面可以选择是否在上传后删除掉。建议删除掉,免得占空间。
[picker dismissViewControllerAnimated:NO completion:nil];
//压缩视频文件
[self convertVideoQuailtyWithInputURL:sourceURL outputURL:newVideoUrl completeHandler:nil];
return;
}
}
#pragma mark --//此方法可以获取视频文件的时长。
- (CGFloat) getVideoLength:(NSURL *)URL
{
AVURLAsset *avUrl = [AVURLAsset assetWithURL:URL];
CMTime time = [avUrl duration];
int second = ceil(time.value/time.timescale);
return second;
}
#pragma mark --//此方法可以获取文件的大小,返回的是单位是KB。
- (CGFloat) getFileSize:(NSString *)path
{
NSFileManager *fileManager = [NSFileManager defaultManager];
float filesize = -1.0;
if ([fileManager fileExistsAtPath:path]) {
NSDictionary *fileDic = [fileManager attributesOfItemAtPath:path error:nil];//获取文件的属性
unsigned long long size = [[fileDic objectForKey:NSFileSize] longLongValue];
filesize = 1.0*size/1024;
}else{
NSLog(@"找不到文件");
}
return filesize;
}
处理视频质量 路径
#pragma mark --//完成视频录制,做 压缩 处理
- (void)convertVideoQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL completeHandler:(void (^)(AVAssetExportSession*))handler
{
// 通过文件的 url 获取到这个文件的资源
AVURLAsset *avAsset = [[AVURLAsset alloc] initWithURL:inputURL options:nil];
// 用 AVAssetExportSession 这个类来导出资源中的属性
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
// 压缩视频 //AVAssetExportPresetLowQuality //AVAssetExportPresetMediumQuality //AVAssetExportPresetHighestQuality
if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) { // 导出属性是否包含低分辨率
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse= YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void){
switch (exportSession.status) {
case AVAssetExportSessionStatusCancelled:
NSLog(@"AVAssetExportSessionStatusCancelled");
break;
case AVAssetExportSessionStatusUnknown:
NSLog(@"AVAssetExportSessionStatusUnknown");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"AVAssetExportSessionStatusWaiting");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"AVAssetExportSessionStatusExporting");
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"AVAssetExportSessionStatusCompleted");
NSLog(@"保存视频===时间===%@",[NSString stringWithFormat:@"%f s", [self getVideoLength:outputURL]]);
NSLog(@"保存视频===文件大小===%@", [NSString stringWithFormat:@"%.2f kb", [self getFileSize:[outputURL path]]]);
//UISaveVideoAtPathToSavedPhotosAlbum([outputURL path], self, nil, NULL);//这个是保存到手机相册
// UISaveVideoAtPathToSavedPhotosAlbum([outputURL path], self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
// NSLog(@"图片缩略图111======%@", [self getVideoThumbnailWithFilePath:[outputURL path]]);
// NSLog(@"图片缩略图222======%@", [self getImage:[outputURL path]]);
//上传视频文件 请求接口
[self uploadImageToQNFilePath:[outputURL path]];//传文件的方法 慢
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed");
break;
default:
break;
}
}];
}
}
七牛上传视频(文件的形式)
pod "Qiniu", "~> 7.1"
#import <QiniuSDK.h>
self.qiNiuToken = @"服务器请求接口获取";
//主动取消上传
self.isUploadCancel = YES;
#pragma mark -- 上传 根据路径上传视频 -- 请求接口
- (void)uploadImageToQNFilePath:(NSString *)filePath
{
__weak typeof(self) weakSelf = self;
NSLog(@"上传文件的请求数据-----现在是上传视频--路径========:%@", filePath);
QNUploadManager *upManager = [[QNUploadManager alloc] init];
QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
NSLog(@"上传进度percent == %.2f", percent);
} params:nil checkCrc:NO cancellationSignal:^BOOL() {
//返回 上传视频中途取消时YES 否则NO
return weakSelf.isUploadCancel;
}];
//主动取消上传调用方法
// uploadOption.cancellationSignal();
//qiniu上传文件方法
[upManager putFile:filePath key:nil token:self.qiNiuToken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
weakSelf.progressView.hidden = YES;
NSLog(@"info ===== %@", info);
NSLog(@"resp ===== %@", resp);
dispatch_async(dispatch_get_main_queue(), ^{
});
} option:uploadOption];
}