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