iOS实现视频压缩上传实例代码
之前写过图片上传PHP服务器,今天把接口稍微改了一下,把视频上传的代码贴出来,目前上传功能已经调通,视频的压缩代码上似乎并不完善,后续会完善压缩部分的代码;
-(void)convertVideoWithURL:(NSURL*)url
{
NSDate*date=[NSDatedate];
NSDateFormatter*dateformatter=[[NSDateFormatteralloc]init];
[dateformattersetDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
NSString*dateName=[dateformatterstringFromDate:date];
NSString*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
NSString*pathName=[pathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.mp4",dateName]];
NSLog(@"沙盒:%@",pathName);
//转码配置
AVURLAsset*asset=[AVURLAssetURLAssetWithURL:urloptions:nil];
AVAssetExportSession*exportSession=[[AVAssetExportSessionalloc]initWithAsset:assetpresetName:AVAssetExportPresetMediumQuality];
exportSession.shouldOptimizeForNetworkUse=YES;
exportSession.outputURL=[NSURLfileURLWithPath:pathName];
exportSession.outputFileType=AVFileTypeMPEG4;
[exportSessionexportAsynchronouslyWithCompletionHandler:^{
intexportStatus=exportSession.status;
switch(exportStatus){
caseAVAssetExportSessionStatusFailed:
{
//logerrortotextview
NSError*exportError=exportSession.error;
NSLog(@"AVAssetExportSessionStatusFailed:%@",exportError);
[SVProgressHUDshowErrorWithStatus:@"视频压缩失败"];
[SVProgressHUDdismissWithDelay:1.0];
break;
}
caseAVAssetExportSessionStatusCompleted:
{
self.videoData=[NSDatadataWithContentsOfFile:pathName];
[[NetToolshareDL]upLoadVideoWithURL:@"http://192.168.1.102/php/image.php"paremeter:nildata:self.videoDatavideoName:[NSStringstringWithFormat:@"%@.mp4",dateName]progress:^(NSProgress*_NonnulluploadProgress){
[SVProgressHUDshowProgress:1.0*uploadProgress.completedUnitCount/uploadProgress.totalUnitCountstatus:@"正在上传"];
NSLog(@"正在上传%f%%",(1.0*uploadProgress.completedUnitCount/uploadProgress.totalUnitCount)*100);
}success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject){
[SVProgressHUDshowSuccessWithStatus:@"上传成功"];
[SVProgressHUDdismissWithDelay:1.0];
}fail:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror){
[SVProgressHUDshowErrorWithStatus:@"上传失败"];
[SVProgressHUDdismissWithDelay:1.0];
}];
}
}
}];
}
[managerPOST:urlparameters:parameterconstructingBodyWithBlock:^(id_NonnullformData){ [formDataappendPartWithFileData:videoDataname:@"upimage"fileName:videoNamemimeType:@"video/mp4"]; }progress:^(NSProgress*_NonnulluploadProgress){ progress(uploadProgress); }success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject){ success(task,responseObject); }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror){ fail(task,error); }];
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。