iOS实现视频压缩上传实例代码
程序员文章站
2023-12-20 08:36:34
之前写过图片上传php服务器,今天把接口稍微改了一下,把视频上传的代码贴出来,目前上传功能已经调通,视频的压缩代码上似乎并不完善,后续会完善压缩部分的代码;
-...
之前写过图片上传php服务器,今天把接口稍微改了一下,把视频上传的代码贴出来,目前上传功能已经调通,视频的压缩代码上似乎并不完善,后续会完善压缩部分的代码;
- (void)convertvideowithurl:(nsurl *)url { nsdate *date = [nsdate date]; nsdateformatter *dateformatter = [[nsdateformatter alloc]init]; [dateformatter setdateformat:@"yyyy-mm-dd-hh-mm-ss"]; nsstring *datename = [dateformatter stringfromdate:date]; nsstring *path = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes).firstobject; nsstring *pathname = [path stringbyappendingpathcomponent:[nsstring stringwithformat:@"%@.mp4",datename]]; nslog(@"沙盒:%@",pathname); //转码配置 avurlasset *asset = [avurlasset urlassetwithurl:url options:nil]; avassetexportsession *exportsession= [[avassetexportsession alloc] initwithasset:asset presetname:avassetexportpresetmediumquality]; exportsession.shouldoptimizefornetworkuse = yes; exportsession.outputurl = [nsurl fileurlwithpath:pathname]; exportsession.outputfiletype = avfiletypempeg4; [exportsession exportasynchronouslywithcompletionhandler:^{ int exportstatus = exportsession.status; switch (exportstatus) { case avassetexportsessionstatusfailed: { // log error to text view nserror *exporterror = exportsession.error; nslog (@"avassetexportsessionstatusfailed: %@", exporterror); [svprogresshud showerrorwithstatus:@"视频压缩失败"]; [svprogresshud dismisswithdelay:1.0]; break; } case avassetexportsessionstatuscompleted: { self.videodata = [nsdata datawithcontentsoffile:pathname]; [[nettool sharedl]uploadvideowithurl:@"http://192.168.1.102/php/image.php" paremeter:nil data:self.videodata videoname:[nsstring stringwithformat:@"%@.mp4",datename] progress:^(nsprogress * _nonnull uploadprogress) { [svprogresshud showprogress:1.0*uploadprogress.completedunitcount/uploadprogress.totalunitcount status:@"正在上传"]; nslog(@"正在上传%f%%",(1.0*uploadprogress.completedunitcount/uploadprogress.totalunitcount)*100); } success:^(nsurlsessiondatatask * _nonnull task, id _nullable responseobject) { [svprogresshud showsuccesswithstatus:@"上传成功"]; [svprogresshud dismisswithdelay:1.0]; } fail:^(nsurlsessiondatatask * _nullable task, nserror * _nonnull error) { [svprogresshud showerrorwithstatus:@"上传失败"]; [svprogresshud dismisswithdelay:1.0]; }]; } } }]; }
[manager post:url parameters:parameter constructingbodywithblock:^(id<afmultipartformdata> _nonnull formdata) { [formdata appendpartwithfiledata:videodata name:@"upimage" filename:videoname mimetype:@"video/mp4"]; } progress:^(nsprogress * _nonnull uploadprogress) { progress(uploadprogress); } success:^(nsurlsessiondatatask * _nonnull task, id _nullable responseobject) { success(task,responseobject); } failure:^(nsurlsessiondatatask * _nullable task, nserror * _nonnull error) { fail(task,error); }];
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。