John-Connolly/Merge

Audio Channel

Closed this issue · 2 comments

Overlaying images removes the audio Channel. Is this a known "feature" of the library or am I missing something.

hey gmccormack95
I believe he overlooked it, i fixed it by doing the following

fileprivate final class Composition {

let asset = AVMutableComposition()
let track: AVMutableCompositionTrack

init(duration: CMTime, videoAsset: AVAssetTrack, audioAsset: AVAssetTrack?) throws {
    track = asset.addMutableTrack(withMediaType: AVMediaTypeVideo,
                                  preferredTrackID: Int32(kCMPersistentTrackID_Invalid)
    )
    try track.insertTimeRange(CMTimeRangeMake(kCMTimeZero, duration),
                              of: videoAsset,
                              at: kCMTimeZero)
    if audioAsset != nil {
        let audioCompositionTrack = asset.addMutableTrack(withMediaType: AVMediaTypeAudio,
                                                          preferredTrackID: kCMPersistentTrackID_Invalid)
        try audioCompositionTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, duration), of: audioAsset!, at: kCMTimeZero)
    }
}

}

I modified the Composition class, thus when he calls it in overlayvideo function, i changed to this

    var compositionTry: Composition?
    if let audioTrack = video.tracks(withMediaType: AVMediaTypeAudio).first {
        compositionTry = try? Composition(duration: video.duration, videoAsset: track, audioAsset:audioTrack)

    } else {
        compositionTry = try? Composition(duration: video.duration, videoAsset: track, audioAsset:nil)
    }

This way you can add the audio track to your exported video

I just merged a PR that should fix this issue.