2011-10-04 20 views
10

Utilizzo una AVCaptureSession per acquisire campioni audio e video dal microfono e dalla fotocamera del dispositivo.Come si usa AVAssetWriter per scrivere l'audio AAC in ios?

Sto quindi cercando di scrivere i CMSampleBuffers (Uso AVAssetWriter e AVAssetWriterInputs) ha restituito tramite il metodo AVCaptureSessions delegato

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

Questo funziona bene quando il mio AVAssetWriterInput audio è configurato per scrivere i dati in formato Apple Lossless (kAudioFormatAppleLossless), ma se provo e configurare l'AVAssetWriterInput audio da utilizzare AAC (kAudioFormatMPEG4AAC) scrive con successo i campioni audio e video per circa 500 ms poi non riesce con il seguente errore

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} 

Ecco il codice che uso per creare la mia AVAssetWriter e 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]; 

Qualcuno sa come scrivere correttamente campioni audio tornato da AVCaptureSession utilizzando un AVAssetWriterInput configurato per scrivere AAC?

risposta

12

sono riuscito a farlo funzionare modificando il parametro AVEncoderBitRateKey passato come le impostazioni di output desiderato da 96 a 64000.

mie impostazioni audio per inizializzare un AVAssetWriterInput in grado di scrivere audio AAC ora sembra

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

Con il nuovo iPhone 4s ho dovuto anche modificare AVNumberOfChannelsKey in 2 – RyanSullivan

+0

Non riuscivo a capirlo! Sapevo che aveva qualcosa a che fare con le opzioni, ma non sapevo esattamente cosa. Grazie per questo! –