swift - Composing Video and Audio - Video's audio is gone -
my question is, using function below, compose video , audio. want keep video's original sound goes away somehow, not have clue.
i got function this answer
i tried change volumes right after appending avmutablecompositiontrack
s did not work
for instance;
mutablevideocompositiontrack.prefferedvolume = 1.0 mutableaudiocompositiontrack.prefferedvolume = 0.05
but still, can hear audio file.
the function;
private func mergeaudioandvideo(audiourl: url, videourl: url, completion: @escaping (bool)->void){ let mixcomposition = avmutablecomposition() var mutablecompositionvideotrack : [avmutablecompositiontrack] = [] var mutablecompositionaudiotrack : [avmutablecompositiontrack] = [] let totalvideocompositioninstruction = avmutablevideocompositioninstruction() let videoasset = avasset(url: videourl) let audioasset = avasset(url: audiourl) mutablecompositionvideotrack.append(mixcomposition.addmutabletrack(withmediatype: avmediatypevideo, preferredtrackid: kcmpersistenttrackid_invalid)) mutablecompositionaudiotrack.append(mixcomposition.addmutabletrack(withmediatype: avmediatypeaudio, preferredtrackid: kcmpersistenttrackid_invalid)) mutablecompositionaudiotrack[0].preferredvolume = 0.05 mutablecompositionvideotrack[0].preferredvolume = 1.0 let videoassettrack = videoasset.tracks(withmediatype: avmediatypevideo)[0] let audioassettrack = audioasset.tracks(withmediatype: avmediatypeaudio)[0] { try mutablecompositionvideotrack[0].inserttimerange(cmtimerangemake(kcmtimezero, videoassettrack.timerange.duration), of: videoassettrack, at: kcmtimezero) try mutablecompositionaudiotrack[0].inserttimerange(cmtimerangemake(kcmtimezero, videoassettrack.timerange.duration), of: audioassettrack, at: kcmtimezero) }catch{ print("error#1") } totalvideocompositioninstruction.timerange = cmtimerangemake(kcmtimezero, videoassettrack.timerange.duration) let mutablevideocomposition = avmutablevideocomposition() mutablevideocomposition.frameduration = cmtimemake(1, 30) mutablevideocomposition.rendersize = cgsize(width: 1280, height: 720) //exporting savepathurl = try! filemanager.default.url(for: filemanager.searchpathdirectory.documentdirectory, in: filemanager.searchpathdomainmask.userdomainmask, appropriatefor: nil, create: true).appendingpathcomponent("merged").appendingpathextension("mov") let assetexport = avassetexportsession(asset: mixcomposition, presetname: avassetexportpresethighestquality)! assetexport.outputfiletype = avfiletypempeg4 assetexport.outputurl = savepathurl assetexport.shouldoptimizefornetworkuse = true { try filemanager.default.removeitem(at: savepathurl) }catch { print(error) } assetexport.exportasynchronously { switch assetexport.status{ case .completed: print("completed") completion(true) default: print("failed \(assetexport.error!)") completion(false) } } }
i figured out. seems avasset
loads video holds audio , video separately. can reach them writing``
videoasset.tracks(withmediatype: avmediatypeaudio)[0] //audio of video videoasset.tracks(withmediatype: avmediatypevideo)[0] //video of video(without sound)
so added these lines code , worked!
var mutablecompositionbacktrack : [avmutablecompositiontrack] = [] mutablecompositionbacktrack.append(mixcomposition.addmutabletrack(withmediatype: avmediatypeaudio, preferredtrackid: kcmpersistenttrackid_invalid)) try mutablecompositionbacktrack[0].inserttimerange(cmtimerangemake(kcmtimezero, videoassettrack.timerange.duration), of: backassettrack, at: kcmtimezero)
there still missing point not know how do, , setting volumes of these audio assets. update answer figure out how.
Comments
Post a Comment