ios - How to trim the video file and convert to 20 seconds video with Swift? -


i want trim video file. want pick video gallery , convert 15 second video. following this link objective c. works fine me i'm beginner swift language. can 1 me convert code in swift?

below code in objective c:

-(void)cropvideo:(nsurl*)videototrimurl{     avurlasset *asset = [avurlasset urlassetwithurl:videototrimurl options:nil];     avassetexportsession *exportsession = [[avassetexportsession alloc] initwithasset:asset presetname:avassetexportpresethighestquality];      nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);     nsstring *outputurl = paths[0];     nsfilemanager *manager = [nsfilemanager defaultmanager];     [manager createdirectoryatpath:outputurl withintermediatedirectories:yes attributes:nil error:nil];     outputurl = [outputurl stringbyappendingpathcomponent:@"output.mp4"];     // remove existing file     [manager removeitematpath:outputurl error:nil];      //     exportsession.outputurl = [nsurl fileurlwithpath:outputurl];     exportsession.shouldoptimizefornetworkuse = yes;     exportsession.outputfiletype = avfiletypequicktimemovie;     cmtime start = cmtimemakewithseconds(1.0, 600); // modify time range here     cmtime duration = cmtimemakewithseconds(19.0, 600);     cmtimerange range = cmtimerangemake(start, duration);     exportsession.timerange = range;     [exportsession exportasynchronouslywithcompletionhandler:^(void)      {          switch (exportsession.status) {              case avassetexportsessionstatuscompleted:                  [self writevideotophotolibrary:[nsurl fileurlwithpath:outputurl]];                  nslog(@"export complete %d %@", exportsession.status, exportsession.error);                  break;              case avassetexportsessionstatusfailed:                  nslog(@"failed:%@",exportsession.error);                  break;              case avassetexportsessionstatuscancelled:                  nslog(@"canceled:%@",exportsession.error);                  break;              default:                  break;          }           //[exportsession release];      }]; } -(void)writevideotophotolibrary:(nsurl*)aurl {     nsurl *url = aurl;     nsdata *data = [nsdata datawithcontentsofurl:url];      // write cache directory     nsstring *path = [[nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes) objectatindex:0] stringbyappendingpathcomponent:@"file.mov"];     [data writetofile:path atomically:yes];       // after use path save photolibrary      alassetslibrary *library = [[alassetslibrary alloc] init];     [library writevideoatpathtosavedphotosalbum:[nsurl fileurlwithpath:path] completionblock:^(nsurl *asseturl, nserror *error) {          if (error) {             nslog(@"%@", error.description);         }else {             nslog(@"done :)");         }      }];   } 

for swift can use following code trimming video.

func trimvideo(sourceurl: nsurl, destinationurl: nsurl, trimpoints: trimpoints, completion: trimcompletion?) {     assert(sourceurl.fileurl)     assert(destinationurl.fileurl)      let options = [ avurlassetpreferprecisedurationandtimingkey: true ]     let asset = avurlasset(url: sourceurl, options: options)     let preferredpreset = avassetexportpresetpassthrough     if verifypresetforasset(preferredpreset, asset) {         let composition = avmutablecomposition()         let videocomptrack = composition.addmutabletrackwithmediatype(avmediatypevideo, preferredtrackid: cmpersistenttrackid())         let audiocomptrack = composition.addmutabletrackwithmediatype(avmediatypeaudio, preferredtrackid: cmpersistenttrackid())          let assetvideotrack: avassettrack = asset.trackswithmediatype(avmediatypevideo).first as! avassettrack         let assetaudiotrack: avassettrack = asset.trackswithmediatype(avmediatypeaudio).first as! avassettrack          var comperror: nserror?          var accumulatedtime = kcmtimezero         (starttimeforcurrentslice, endtimeforcurrentslice) in trimpoints {             let durationofcurrentslice = cmtimesubtract(endtimeforcurrentslice, starttimeforcurrentslice)             let timerangeforcurrentslice = cmtimerangemake(starttimeforcurrentslice, durationofcurrentslice)              videocomptrack.inserttimerange(timerangeforcurrentslice, oftrack: assetvideotrack, attime: accumulatedtime, error: &comperror)             audiocomptrack.inserttimerange(timerangeforcurrentslice, oftrack: assetaudiotrack, attime: accumulatedtime, error: &comperror)              if comperror != nil {                 nslog("error during composition: \(comperror)")                 if let completion = completion {                     completion(comperror)                 }             }              accumulatedtime = cmtimeadd(accumulatedtime, durationofcurrentslice)         }          let exportsession = avassetexportsession(asset: composition, presetname: preferredpreset)         exportsession.outputurl = destinationurl         exportsession.outputfiletype = avfiletypeapplem4v         exportsession.shouldoptimizefornetworkuse = true          removefileaturlifexists(destinationurl)          exportsession.exportasynchronouslywithcompletionhandler({ () -> void in             if let completion = completion {                 completion(exportsession.error)             }         })     } else {         nslog("could not find suitable export preset input video")         let error = nserror(domain: "org.linuxguy.videolab", code: -1, userinfo: nil)         if let completion = completion {             completion(error)         }     } } 

trimpoints cmtime.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -