How to add contents of audio files in a movie using AVFoundation on iOS4?

I am using AVFoundation to create a movie file (mp4) using images and sound files.

I successfully created a movie file using AVAssetWriterInputPixelBufferAdaptor, which adds CVPixelBufferRef (extracted from UIImage objects) to the movie file.

Now I want to add the audio content from the file in this movie. Getting data from the device’s microphone is not what I’m thinking about here. And I could not find anything similar to AVAssetWriterInputPixelBufferAdaptor, which can help to record audio data into this video file.

Am I missing something?

+5
source share
2

, AVMutableComposition.

1) AVMutableComposition
2) 2 AVURLAsset, , ,
3) 2 AVMutableCompositionTrack, , , ( 2))
4) AVAssetExportSession 1)
5)

+5

@peter. .

-(BOOL)compositeVideo{
    //Record cur video
    NSURL *curAudio = [[NSBundle mainBundle]URLForResource:@"a" withExtension:@".pcm"];
    NSURL *curVideo = [[NSBundle mainBundle]URLForResource:@"v" withExtension:@".mp4"];
    AVAsset *video = [AVAsset assetWithURL:curVideo];
    AVAsset *audio = [AVAsset assetWithURL:curAudio];
    AVAssetTrack *vTrack = [[video tracksWithMediaType:AVMediaTypeVideo] firstObject];
    NSArray *arr = [audio tracksWithMediaType:AVMediaTypeAudio];
    AVAssetTrack *aTrack = [arr firstObject];

    AVMutableComposition *composition = [AVMutableComposition composition];
    AVMutableCompositionTrack *visualTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:1];
    AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    NSError *error;
    [visualTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video.duration) ofTrack:vTrack atTime:kCMTimeZero error:&error];
    if (error) {
        NSLog(@"video composition failed! error:%@", error);
        return NO;
    }
    [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audio.duration) ofTrack:aTrack atTime:kCMTimeZero error:&error];
    if (error) {
        NSLog(@"audio composition failed! error:%@", error);
        return NO;
    }

    AVAssetExportSession *exporter = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    exporter.outputURL = [NSURL fileURLWithPath:[path stringByAppendingPathComponent:@"compositedVideo.mp4"]];
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    exporter.outputFileType = AVFileTypeQuickTimeMovie;
    [exporter exportAsynchronouslyWithCompletionHandler:^{
        if (exporter.error) {
            NSLog(@"exporter synthesization failed! error:%@", error);
            [self.delegate compositeDidFinishAtURL:nil duration:-1];
        }else{
            [self.delegate compositeDidFinishAtURL:exporter.outputURL duration:CMTimeGetSeconds(video.duration)];
        }
    }];
    return YES;
}
0

All Articles