ORStudios
ORStudios

Reputation: 3233

Merging Videos Together with AVMutableComposition Causes No Audio

I have an NSArray containing a list of video NSURL's and I want to merge them together to make one long compilation. The problem is, when I use the code below the videos merge but there is no audio.

- (IBAction)buildVideo {

// 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                               preferredTrackID:kCMPersistentTrackID_Invalid];

int i = 0;

for (id object in movieArray) {

    AVAsset *asset = [AVAsset assetWithURL:object];

    if(i == 0){

        [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                       ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];


    }else {

        [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                       ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:[mixComposition duration] error:nil];

    }

    i = i + 1;

}

// 4 - Get path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:
                         [NSString stringWithFormat:@"mergeVideo-%d.mov",arc4random() % 1000]];
NSURL *url = [NSURL fileURLWithPath:myPathDocs];
// 5 - Create exporter
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                  presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL=url;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self exportDidFinish:exporter];
    });
}];

}

Upvotes: 3

Views: 1898

Answers (1)

ChrisH
ChrisH

Reputation: 4558

The reason you're not getting audio is that you're not adding the audio track. You need to create an additional AVMutableCompositionTrack with a type of AVMediaTypeAudio:

AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                           preferredTrackID:kCMPersistentTrackID_Invalid];

AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                           preferredTrackID:kCMPersistentTrackID_Invalid];

And insert the time range for the source audio and video tracks into both composition tracks:

CMTime insertTime = kCMTimeZero;

for (id object in movieArray) {

    AVAsset *asset = [AVAsset assetWithURL:object];

    CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

    [videoTrack insertTimeRange:timeRange
                        ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                         atTime:insertTime
                          error:nil];

    [audioTrack insertTimeRange:timeRange
                         ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                          atTime:insertTime
                           error:nil];

    insertTime = CMTimeAdd(insertTime,asset.duration);
}

Upvotes: 9

Related Questions