2011-10-04 79 views
10

我正在使用AVCaptureSession从设备麦克风和摄像头捕获音频和视频样本。如何使用AVAssetWriter在ios中写入AAC音频?

我再尝试写CMSampleBuffers(使用AVAssetWriter和AVAssetWriterInputs)通过AVCaptureSessions委托方法返回

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer: (CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection 

这工作得很好,当我的声音AVAssetWriterInput被配置为将数据写出来的苹果无损格式( kAudioFormatAppleLossless),但如果我尝试和配置音频AVAssetWriterInput使用AAC(kAudioFormatMPEG4AAC)它成功地写入视频和音频样本500ms左右,然后失败,出现以下错误

writer has failed with Error Domain=AVFoundationErrorDomain Code=-11821 "Cannot Decode" UserInfo=0x4b2630 {NSLocalizedFailureReason=The media data could not be decoded. It may be damaged., NSUnderlyingError=0x4ad0f0 "The operation couldn’t be completed. (OSStatus error 560226676.)", NSLocalizedDescription=Cannot Decode} 

这里是我用来创建我AVAssetWriter代码和AVAssetWriterInputs

NSError *error = nil; 
m_VideoCaputurePath = [[NSString stringWithFormat:@"%@/%@.mp4",[UserData getSavePath],[UserData getUniqueFilename]] retain]; 

if(USE_AAC_AUDIO) 
{ 
    m_audioAndVideoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:m_VideoCaputurePath] fileType:AVFileTypeMPEG4 error:&error]; 
} 
else 
{ 
    m_audioAndVideoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:m_VideoCaputurePath] fileType:AVFileTypeQuickTimeMovie error:&error]; 
} 

//\Configure Video Writer Input 
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
           AVVideoCodecH264, AVVideoCodecKey, 
           [NSNumber numberWithInt:640], AVVideoWidthKey, 
           [NSNumber numberWithInt:480], AVVideoHeightKey, 
           nil]; 

m_videoWriterInput = [[AVAssetWriterInput 
         assetWriterInputWithMediaType:AVMediaTypeVideo 
         outputSettings:videoSettings] retain]; 

m_videoWriterInput.expectsMediaDataInRealTime = YES; 

//\Configure Audio Writer Input 

AudioChannelLayout acl; 
bzero(&acl, sizeof(acl)); 
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo; 

NSDictionary* audioOutputSettings; 
if(USE_AAC_AUDIO) 
{ 
    audioOutputSettings = [ NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey, 
              [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey, 
              [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey, 
              [ NSData dataWithBytes: &acl length: sizeof(acl) ], AVChannelLayoutKey, 
              [ NSNumber numberWithInt: 96 ], AVEncoderBitRateKey, 
              nil]; 
} 
else 
{ 
    audioOutputSettings = [ NSDictionary dictionaryWithObjectsAndKeys:      
              [ NSNumber numberWithInt: kAudioFormatAppleLossless ], AVFormatIDKey, 
              [ NSNumber numberWithInt: 16 ], AVEncoderBitDepthHintKey, 
              [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey, 
              [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,          
              [ NSData dataWithBytes: &acl length: sizeof(acl) ], AVChannelLayoutKey, nil ]; 
} 

m_audioWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioOutputSettings] retain]; 

m_audioWriterInput.expectsMediaDataInRealTime = YES; 

//\Add inputs to Write 
NSAssert([m_audioAndVideoWriter canAddInput:m_audioWriterInput], @"Cannot write to this type of audio input"); 
NSAssert([m_audioAndVideoWriter canAddInput:m_videoWriterInput], @"Cannot write to this type of video input"); 

[m_audioAndVideoWriter addInput:m_videoWriterInput]; 
[m_audioAndVideoWriter addInput:m_audioWriterInput]; 

有谁知道如何正确地写音频采样使用配置为写AAC的AVAssetWriterInput从AVCaptureSession回来了?

回答

12

我设法得到它通过改变为所需的输出设置通过AVEncoderBitRateKey参数初始化能够写入AAC音频的AVAssetWriterInput工作从96到64000

我的音效设置,现在看起来像

NSDictionary* audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
            [ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey, 
            [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey, 
            [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey, 
            [ NSData dataWithBytes: &acl length: sizeof(AudioChannelLayout) ], AVChannelLayoutKey, 
            [ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey, 
            nil] 
+0

随着新的iPhone 4s我不得不将AVNumberOfChannelsKey更改为2 – RyanSullivan

+0

我只是无法弄清楚这一点!我知道这与选择有关,但不知道究竟是什么。感谢你! –

相关问题