如何使用AVAssetWriter在ios中写出AAC音频?

时间:2011-10-04 14:24:24

标签: iphone ios audio avassetwriter

我正在使用AVCaptureSession从设备麦克风和相机中捕获音频和视频样本。

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

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

当我的音频AVAssetWriterInput被配置为以Apple Lossless格式(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返回的音频样本?

1 个答案:

答案 0 :(得分:12)

我设法通过将AVEncoderBitRateKey参数作为所需的输出设置从96更改为64000来实现它。

我初始化能够编写AAC音频的AVAssetWriterInput的音频设置现在看起来像

    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]