I am trying to make a movie file using an array of images and an audio file. To make a movie using an array of images, I used the great zoul post here . Everything is perfect, I have my movie with my picture. However, when I try to add some audio tracks, I have a lot of problems. To understand, I put my code:
When I call this method, the image array and song file are ready:
-(void) writeImagesToMovieAtPath:(NSString *) path withSize:(CGSize) size { NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:documentsDirectoryPath]; for (NSString *tString in dirContents) { if ([tString isEqualToString:@"essai.mp4"]) { [[NSFileManager defaultManager]removeItemAtPath:[NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,tString] error:nil]; } } NSLog(@"Write Started"); NSError *error = nil; AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL: [NSURL fileURLWithPath:path] fileType:AVFileTypeMPEG4 error:&error]; NSParameterAssert(videoWriter); NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys: AVVideoCodecH264, AVVideoCodecKey, [NSNumber numberWithInt:size.width], AVVideoWidthKey, [NSNumber numberWithInt:size.height], AVVideoHeightKey, nil]; AudioChannelLayout channelLayout; memset(&channelLayout, 0, sizeof(AudioChannelLayout)); channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo; NSDictionary *audioSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey, [NSNumber numberWithFloat:44100.0] ,AVSampleRateKey, [NSNumber numberWithInt: 1] ,AVNumberOfChannelsKey, [NSNumber numberWithInt:192000],AVEncoderBitRateKey, [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)],AVChannelLayoutKey, nil]; AVAssetWriterInput* videoWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings] retain]; AVAssetWriterInput* audioWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioSettings] retain]; NSURL* fileURL = [[NSBundle mainBundle] URLForResource:@"Big_Voice_1" withExtension:@"caf"]; NSLog(@"%@",fileURL); AVAsset *asset = [[AVURLAsset URLAssetWithURL:fileURL options:nil] retain]; AVAssetReader *audioReader = [[AVAssetReader assetReaderWithAsset:asset error:&error] retain]; AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput sourcePixelBufferAttributes:nil]; AVAssetTrack* audioTrack = [asset.tracks objectAtIndex:0]; AVAssetReaderOutput *readerOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:audioTrack outputSettings:nil]; [audioReader addOutput:readerOutput]; NSParameterAssert(videoWriterInput); NSParameterAssert(audioWriterInput); NSParameterAssert([videoWriter canAddInput:audioWriterInput]); NSParameterAssert([videoWriter canAddInput:videoWriterInput]); audioWriterInput.expectsMediaDataInRealTime = NO; videoWriterInput.expectsMediaDataInRealTime = YES; [videoWriter addInput:audioWriterInput]; [videoWriter addInput:videoWriterInput];
I am completing the recording of an image in a movie file, and I want to copy the audio to a file, and I do this:
[audioReader startReading]; [videoWriter startSessionAtSourceTime:kCMTimeZero]; dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL); [audioWriterInput requestMediaDataWhenReadyOnQueue:mediaInputQueue usingBlock:^ { NSLog(@"Request"); NSLog(@"Asset Writer ready :%d",audioWriterInput.readyForMoreMediaData); while (audioWriterInput.readyForMoreMediaData) { NSLog(@"Ready"); CMSampleBufferRef nextBuffer = [readerOutput copyNextSampleBuffer]; if (nextBuffer) { NSLog(@"NextBuffer"); [audioWriterInput appendSampleBuffer:nextBuffer]; } } } ]; [audioWriterInput markAsFinished]; [videoWriter finishWriting];
However, the AssetWriterInput status of the audio file is always "NO".
My question is: how to add audio to a video file using AVFoundation?
So please, can someone help me by telling me that I will forget something or something is wrong.
Thank you very much