当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS实现视频压缩上传实例代码

iOS实现视频压缩上传实例代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

柳亚子纪念馆,天龙八部sfgogoxl,q宠大乐斗炼丹炉

之前写过图片上传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);
  }];

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网