///////////////////////////////
//视频转码方法 传相册或者相机得到的url和给视频命名
-(void)shipinZhuanma:(NSURL *)url withName:(NSString *)name{
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
[SVProgressHUD showWithStatus:@"处理中..."];
[self creatSandBoxFilePathIfNoExist];
//保存至沙盒路径
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *videoPath = [NSString stringWithFormat:@"%@/zhuanMaVideo", pathDocuments];
NSString *sandboxPath = [videoPath stringByAppendingPathComponent:name];
//转码配置
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
//AVAssetExportPresetMediumQuality可以更改,是枚举类型,官方有提供,更改该值可以改变视频的压缩比例
AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputURL = [NSURL fileURLWithPath:sandboxPath];
//AVFileTypeMPEG4 文件输出类型,可以更改,是枚举类型,官方有提供,更改该值也可以改变视频的压缩比例
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
int exportStatus = exportSession.status;
NSLog(@"%d",exportStatus);
switch (exportStatus)
{
case AVAssetExportSessionStatusFailed:
{
// log error to text view
NSError *exportError = exportSession.error;
NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
//转码失败
break;
}
case AVAssetExportSessionStatusCompleted:
{
[SVProgressHUD dismiss];
NSData *data = [NSData dataWithContentsOfFile:sandboxPath];
if (data) {
NSString *videoDataString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//把视频二进制流 写入沙盒
NSString*docPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
//字典写文件--> ~/Documents/字典
NSDictionary*dic = @{@"shipinName":name,@"shipinDate":videoDataString};
//拼出文件地址
NSString*dicPath = [docPath stringByAppendingPathComponent:@"飞行圈视频字典"];
//写入文件
[dic writeToFile:dicPath atomically:YES];
}
}
}
}];
}
- (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];
//创建目录
NSString *createPath = [NSString stringWithFormat:@"%@/zhuanMaVideo", pathDocuments];
// 判断文件夹是否存在,如果不存在,则创建
if (![[NSFileManager defaultManager] fileExistsAtPath:createPath]) {
[fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
} else {
NSLog(@"FileImage is exists.");
}
}
//上传视频数据方法
- (void)videoGO_FuwuQI{
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
[SVProgressHUD showWithStatus:@" "];
// __weak typeof(self) weakSelf = self;
NSString*docPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
NSString*dicPath = [docPath stringByAppendingPathComponent:@"飞行圈视频字典"];
//读出文件内容
NSDictionary*readDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
//////这是视频上传服务器的地址
// NSString *svr = [NSString stringWithFormat:@"%@JieKou/Shipinku_Jiekou/shipingupload.aspx",glaFly_upIMG];
NSString *svr = [NSString stringWithFormat:@"%@%@",glaFly_upIMG,self.shangURL];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"image/png",nil];
//////这是视频上传服务器的约好的键值
NSDictionary *parameters =@{@"videoname":readDic[@"shipinName"],@"videodata":readDic[@"shipinDate"]};
// NSURLSessionDataTask *abc =
[manager POST:svr parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
// dispatch_async(dispatch_get_main_queue(), ^{
// [weakSelf.jinDuTiao setProgress:1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount animated:YES];
// weakSelf.jiduLab.text = [NSString stringWithFormat:@"%.2f%",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount * 100];
// });
} success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
// NSLog(@"上传成功");
[SVProgressHUD dismiss];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD dismiss];
NSLog(@"视频上传出错:%@",error);
}];
}
-(void)shipinCaiJian:(NSURL *)url withName:(NSString *)name start:(CGFloat)startTime end:(CGFloat)endTime{
[self cropWithVideoUrlStr:url withName:name start:startTime end:endTime completion:^(NSURL *outputURL, Float64 videoDuration, BOOL isSuccess) {
}];
}
///////////////////////////////视频截取
- (void)cropWithVideoUrlStr:(NSURL *)videoUrl withName:(NSString *)name start:(CGFloat)startTime end:(CGFloat)endTime completion:(void (^)(NSURL *outputURL, Float64 videoDuration, BOOL isSuccess))completionHandle
{
[self creatSandBoxFilePathIfNoExist];
//保存至沙盒路径
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *videoPath = [NSString stringWithFormat:@"%@/zhuanMaVideo", pathDocuments];
NSString *sandboxPath = [videoPath stringByAppendingPathComponent:name];
//转码配置
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoUrl options:nil];
//AVAssetExportPresetMediumQuality可以更改,是枚举类型,官方有提供,更改该值可以改变视频的压缩比例
AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputURL = [NSURL fileURLWithPath:sandboxPath];
//AVFileTypeMPEG4 文件输出类型,可以更改,是枚举类型,官方有提供,更改该值也可以改变视频的压缩比例
exportSession.outputFileType = AVFileTypeMPEG4;
/////////////////////////////////////////////////
///////////////////////////////-------华丽的分割线-------////////////////////////////////////////////////
/////////////////////////////////////////////////
CMTime start = CMTimeMakeWithSeconds(startTime, asset.duration.timescale);
CMTime duration = CMTimeMakeWithSeconds(endTime - startTime,asset.duration.timescale);
CMTimeRange range = CMTimeRangeMake(start, duration);
exportSession.timeRange = range;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
[SVProgressHUD dismiss];
switch ([exportSession status]) {
case AVAssetExportSessionStatusFailed:
{
NSLog(@"合成失败:%@", [[exportSession error] description]);
completionHandle(exportSession.outputURL, endTime, NO);
}
break;
case AVAssetExportSessionStatusCancelled:
{
completionHandle(exportSession.outputURL, endTime, NO);
}
break;
case AVAssetExportSessionStatusCompleted:
{
completionHandle(exportSession.outputURL, endTime, YES);
NSData *data = [NSData dataWithContentsOfFile:sandboxPath];
if (data) {
NSString *videoDataString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//把视频二进制流 写入沙盒
NSString*docPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
//字典写文件--> ~/Documents/字典
NSDictionary*dic = @{@"shipinName":name,@"shipinDate":videoDataString};
//拼出文件地址
NSString*dicPath = [docPath stringByAppendingPathComponent:@"飞行圈视频字典"];
//写入文件
[dic writeToFile:dicPath atomically:YES];
}
}
break;
default:
{
completionHandle(exportSession.outputURL, endTime, NO);
} break;
}
}];
}
@end