2011-10-04 12 views
10

AVCaptureSessionを使用して、デバイスのマイクとカメラからオーディオとビデオのサンプルをキャプチャしています。AVAssetWriterを使用してiosでAACオーディオを書き出す方法を教えてください。

私はその後、(AVAssetWriterとAVAssetWriterInputsを使用)CMSampleBuffersを書き込もうとしていますが

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

これは、(私のオーディオAVAssetWriterInputは、Appleロスレス形式のうち、データを書き込むように構成されている場合正常に動作しますAVCaptureSessionsのデリゲートメソッドを介して返さkAudioFormatAppleLossless)しかし、私は試してみて、それが成功した後、約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} 
で失敗)(kAudioFormatMPEG4AACをAACを使用するオーディオAVAssetWriterInputを設定した場合ここ

は私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

私は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] 
のように見える96 64000へ

から所望の出力設定として渡さAVEncoderBitRateKeyパラメータを変更することで、それが仕事を得るために管理

+0

新しいiPhone 4では、AVNumberOfChannelsKeyを2 – RyanSullivan

+0

に変更する必要がありましたが、これを理解できませんでした。私はそれがオプションと何か関係があることを知っていたが、正確に何がわからなかった。ありがとう! –

関連する問題