5

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!

Micko
  • 389
  • 7
  • 15

1 Answers1

11

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.

Steve
  • 970
  • 3
  • 8
  • 19
  • Hi Steve, I tried with about code. When I try to execute the "[[audioAsset1 tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0]" it is giving error (index 0 beyond bounds for empty array). But when I try to print the audioAsset1 in console, it is printing "". I have checked with the path, that file is there. what is my mistake? please tell me. – jfalexvijay Dec 09 '10 at 13:08
  • I get these error when file which I have load to asset do not have supported data type if that type, try to check the file – Steve Dec 09 '10 at 14:32
  • But in my app, the .m4a file is playing with AVAudioPlayer. SO please tell me what is my mistake in this code. – jfalexvijay Dec 09 '10 at 14:44
  • I have posted it in the following URL. http://stackoverflow.com/questions/4398764/nsmutablearray-objectatindex-index-0-beyond-bounds-for-empty-array – jfalexvijay Dec 09 '10 at 14:54
  • @jfalexvijay I am facing the same issue. I checked the link that steve gave, but no success. As jfalexvijay mentioned, I am getting same error like (index 0 beyond bounds) and i am getting path as jfalexvijay mentioned. Please if u were able to resolve it, please help me out. Thanks – DivineDesert Dec 27 '11 at 05:41
  • @DimplePanchal please check the link "http://stackoverflow.com/questions/4398764/nsmutablearray-objectatindex-index-0-beyond-bounds-for-empty-array-from-avur/4451617#4451617" – jfalexvijay Dec 27 '11 at 18:01
  • @Steve, I am facing the same issue due to file type, how you have resolved that issue? – spaleja Oct 30 '12 at 08:52