Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVMutableComposition - are 2 parallel audio tracks possible?

I mixed successfully a video and an audio track together and exported it to a new .m4v file.

My problem is now, that I want to mix the same video file and 2 audio files, which are two AVAssetTrack and have the same time lines, together. Like you do it in an audio editor where you can make a mixdown of two or more sound files, and you get one merged file.

Is this possible? If yes, how I have to proceed?

At the moment I just hear one sound file after proceeding, not both.

By the way: my target is to "simply" include an additional sound file to a video which already have sound and mix it with the new sound file together. But it seems, that an AVAssetTrack just allows audio or video, therefore I made a new audio-AVAssetTrack out of the original video. Perhaps this is wrong...

Thank you in advance!

like image 681
Micko Avatar asked Nov 01 '10 20:11

Micko


1 Answers

Well its hard to help you without seeing your code. Maybe this code could 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.

like image 116
Steve Avatar answered Oct 03 '22 16:10

Steve