Composing Video and Audio - Video's audio is gone

前端 未结 3 365
刺人心
刺人心 2021-01-21 09:14

My question is, I am using the function below, to compose a video and audio. I want to keep video\'s original sound but it goes away somehow, I do not have any clue.

I

3条回答
  •  孤城傲影
    2021-01-21 09:50

    I figured it out. It seems an AVAsset which loads a video holds the audio and video separately. So you can reach them writing``

    videoAsset.tracks(withMediaType: AVMediaTypeAudio)[0] //audio of a video
    videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0] //video of a video(without sound)
    

    So I added these lines to the code and it 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 is a still missing point that I do not know how to do, and that is setting volumes of these audio assets. I will update this answer as soon as I figure out how.

提交回复
热议问题