AVMutableComposition - 2 parallel audio tracks possible?

I successfully combined video and audio and exported them to a new .m4v file.

My problem is that I want to mix the same video file and two audio files, which are two AVAssetTrack and have the same timelines together. How do you do this in an audio editor where you can mix two or more sound files and you get one combined file.

Is it possible? If so, how should I act?

At the moment, I hear only one sound file after continuing, not both.

By the way, my goal is to “simply” include an additional sound file in a video that already has sound, and mix it along with the new sound file. But it looks like AVAssetTrack just allows audio or video, so I made a new AVAssetTrack audio from the original video. Perhaps this is wrong ...

Thank you in advance!

+4
source share
1 answer

Well its hard to help you without seeing your code. Perhaps this code may help:

AVMutableComposition* composition = [AVMutableComposition composition]; AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoURL options:nil]; AVURLAsset* audioAsset1 = [[AVURLAsset alloc]initWithURL:audioURL1 options:nil]; AVURLAsset* audioAsset2 = [[AVURLAsset alloc]initWithURL:audioURL1 options:nil]; AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; NSError* error = NULL; [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0] atTime:kCMTimeZero AVMutableCompositionTrack *compositionAudioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; [compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero,audioAsset1.duration) ofTrack:[[audioAsset1 tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0] atTime:kCMTimeZero error:&error]; AVMutableCompositionTrack *compositionAudioTrack2 = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; [compositionAudioTrack2 insertTimeRange:CMTimeRangeMake(kCMTimeZero,audioAsset2.duration) ofTrack:[[audioAsset2 tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0] atTime:kCMTimeZero error:&error]; 

Now just export this composition with AVExportSession. And don't forget to release the assets.

+8
source

All Articles