Audio Mixing in iOS using AVFoundation doesnt work -
i trying stitch bunch of videos , add music on video in ios. audio added using avmutableaudiomix
. when video exported audio mix missing. here how code looks :
- (void)mergevideos:(nsarray *)videos{ avmutablecomposition *mixcomposition = [[avmutablecomposition alloc] init]; avmutablecompositiontrack *videotrack = [mixcomposition addmutabletrackwithmediatype:avmediatypevideo preferredtrackid:kcmpersistenttrackid_invalid]; avmutablecompositiontrack *audiotrack = [mixcomposition addmutabletrackwithmediatype:avmediatypeaudio preferredtrackid:kcmpersistenttrackid_invalid]; cmtime currenttime = kcmtimezero; (avasset *asset in videos) { // 2 - video track [videotrack inserttimerange:cmtimerangemake(kcmtimezero, asset.duration) oftrack:[[asset trackswithmediatype:avmediatypevideo] objectatindex:0] attime:currenttime error:nil]; [audiotrack inserttimerange:cmtimerangemake(kcmtimezero, asset.duration) oftrack:[[asset trackswithmediatype:avmediatypeaudio] objectatindex:0] attime:currenttime error:nil]; currenttime = cmtimeadd(currenttime, asset.duration); } // 4 - path nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *mypathdocs = [documentsdirectory stringbyappendingpathcomponent: [nsstring stringwithformat:@"mergevideo-%d.mp4",arc4random() % 1000]]; nsurl *url = [nsurl fileurlwithpath:mypathdocs]; // 5 - create exporter nsarray *compatiblepresets = [avassetexportsession exportpresetscompatiblewithasset:mixcomposition]; nsstring *quality = avassetexportpresethighestquality; //load audio avmutableaudiomix *audiomix = nil; nsurl *audiourl = [self loadaudiofile];// gives url .caf file bundle if (audiourl) { avurlasset *audioasset = [avurlasset assetwithurl:audiourl]; avassettrack *atrack = (avassettrack *)[[audioasset trackswithmediatype:avmediatypeaudio] firstobject]; avmutableaudiomixinputparameters *trackmix = [avmutableaudiomixinputparameters audiomixinputparameterswithtrack:atrack]; [trackmix setvolumerampfromstartvolume:0 toendvolume:1 timerange: cmtimerangemake(cmtimemakewithseconds(0, 1), cmtimemakewithseconds(3, 1))]; [trackmix setvolume:1.0 attime:kcmtimezero]; avmutableaudiomix *audiomix = [avmutableaudiomix audiomix]; audiomix.inputparameters = [nsarray arraywithobject:trackmix]; } avassetexportsession *exporter = [[avassetexportsession alloc] initwithasset:mixcomposition presetname:quality]; if (audiomix) { exporter.audiomix = audiomix; } exporter.outputurl=url; exporter.outputfiletype = avfiletypempeg4; exporter.shouldoptimizefornetworkuse = yes; [exporter exportasynchronouslywithcompletionhandler:^{ dispatch_async(dispatch_get_main_queue(), ^{ [self exportdidfinish:exporter]; }); }]; }
there no error while executing. music contained in .caf
file doesnt mixed exported file. idea going on?
you haven't inserted audio .caf file avmutablecompositiontrack
, audio mix associated atrack
not going adjust volume .caf file. if you'd audio .caf file included in video associated audio mix, create avmutablecompositiontrack
hold audio .caf file:
avmutablecompositiontrack *audiotrackcaf = [mixcomposition addmutabletrackwithmediatype:avmediatypeaudio preferredtrackid:kcmpersistenttrackid_invalid];
(with time ranges set liking):
[audiotrackcaf inserttimerange:cmtimerangemake(kcmtimezero, audioasset.duration) oftrack:atrack attime:kcmtimezero error:nil]
additionally, helpful pass nserror *
(instead of nil
) inserttimerange:oftrack:attime:error
make sure had valid time ranges , media inserted.
Comments
Post a Comment