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

发布于 2024-12-08 01:19:31 字数 4140 浏览 0 评论 0原文

我正在使用 AVCaptureSession 从设备麦克风和摄像头捕获音频和视频样本。

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

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

当我的音频 AVAssetWriterInput 配置为以 Apple Lossless 格式(kAudioFormatAppleLossless)写出数据时,效果很好,但如果我尝试配置音频 AVAssetWriterInput使用 AAC (kAudioFormatMPEG4AAC) 它成功地为以下内容写入视频和音频样本大约 500 毫秒然后失败并出现以下错误

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 返回的音频样本?

I am using an AVCaptureSession to capture audio and video samples from the devices microphone and camera.

I am then attempting to write the CMSampleBuffers (Using AVAssetWriter and AVAssetWriterInputs) returned via the AVCaptureSessions delegate method

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

This works fine when my audio AVAssetWriterInput is configured to write the data out in Apple Lossless format (kAudioFormatAppleLossless) but if i try and configure the audio AVAssetWriterInput to use AAC (kAudioFormatMPEG4AAC) it successfully writes video and audio samples for about 500ms then fails with the following error

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}

Here is the code I use to create my AVAssetWriter and 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];

Does anyone know how to correctly write audio samples returned from AVCaptureSession using an AVAssetWriterInput configured to write AAC?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-12-15 01:19:31

我设法通过将作为所需输出设置传递的 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]

I managed to get it to work by changing the AVEncoderBitRateKey parameter passed as the desired output settings from 96 to 64000.

My audio settings for initializing an AVAssetWriterInput capable of writing AAC audio now looks like

    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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文