iOS Video stitching/compositing

Source: Internet
Author: User

The diagram above illustrates the mixed process, with the following code:

- (void) Mergeandexportvideos: (nsarray*) Videospatharray Withoutpath: (nsstring*) outpath{if(Videospatharray.count = =0) {        return; } avmutablecomposition*mixcomposition =[[Avmutablecomposition alloc] init]; Avmutablecompositiontrack*audiotrack =[Mixcomposition Addmutabletrackwithmediatype:avmediatypeaudio Preferredtrackid:kcmpersistenttracki    D_invalid]; Avmutablecompositiontrack*videotrack =[Mixcomposition Addmutabletrackwithmediatype:avmediatypevideo Preferredtrackid:kcmpersistenttracki    D_invalid]; Cmtime totalduration=Kcmtimezero;  for(inti =0; i < Videospatharray.count; i++) {Avurlasset*asset =[Avurlasset Assetwithurl:[nsurl Fileurlwithpath:videospatharray[i]]; Nserror*erroraudio =Nil;
     // get audio or video in Avasset Avassettrack
*assetaudiotrack =[[Asset Trackswithmediatype:avmediatypeaudio] firstobject];
     // add audio or video to the channel BOOL BA
=[Audiotrack inserttimerange:cmtimerangemake (Kcmtimezero, asset.duration) Oftrack:assetaudi Otrack attime:totalduration Error:&Erroraudio]; NSLog (@"erroraudio:%@%d", ERRORAUDIO,BA); Nserror*errorvideo =Nil; Avassettrack*assetvideotrack =[[Asset Trackswithmediatype:avmediatypevideo]firstobject]; BOOL BL=[Videotrack inserttimerange:cmtimerangemake (Kcmtimezero, asset.duration) oftrack:assetvide Otrack attime:totalduration Error:&Errorvideo]; NSLog (@"errorvideo:%@%d", ERRORVIDEO,BL); Totalduration=Cmtimeadd (totalduration, asset.duration); } NSLog (@"%@", Nshomedirectory ()); Nsurl*mergefileurl =[Nsurl Fileurlwithpath:outpath]; Avassetexportsession*exporter =[[Avassetexportsession alloc] Initwithasset:mixcomposition PRESETNAME:AVASSETEXPORTPRESET640X480]; Exporter.outputurl=Mergefileurl; Exporter.outputfiletype=AVFileTypeMPEG4; Exporter.shouldoptimizefornetworkuse=YES; [Exporter Exportasynchronouslywithcompletionhandler:^{NSLog (@"exporter%@", Exporter.error); }];

Here is an explanation of the method:

-(Avmutablecompositiontrack *) Addmutabletrackwithmediatype: (NSString *) mediatype Preferredtrackid: ( Cmpersistenttrackid) Preferredtrackid;

Parameters:

MediaType: Data type Avmediatypevideo video Avmediatypeaudio audio

Preferredtrackid: can be set freely but recommended kcmpersistenttrackid_invalid is 0

// add audio or video to channel -(BOOL) Inserttimerange: (cmtimerange) Timerange oftrack: (Avassettrack *) track Attime: (Cmtime) StartTime Error: (Nserror * __nullable * __nullable) Outerror;

Parameters:

Timerange: Time period for inserting video/audio

Track: Plugged in video/audio

iOS Video stitching/compositing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.