2011-10-04 11 views
10

मैं डिवाइस माइक्रोफ़ोन और कैमरा से ऑडियो और वीडियो नमूने कैप्चर करने के लिए एक AVCaptureSession का उपयोग कर रहा हूं।आईओएस में एएसी ऑडियो लिखने के लिए मैं AVAssetWriter का उपयोग कैसे करूं?

मैं तो CMSampleBuffers (AVAssetWriter और AVAssetWriterInputs का उपयोग करना) लिखने का प्रयास कर रहा हूँ AVCaptureSessions प्रतिनिधि विधि के माध्यम से लौटे

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

यह ठीक काम करता है जब मेरे ऑडियो AVAssetWriterInput एप्पल दोषरहित प्रारूप में डेटा लिखने के लिए कॉन्फ़िगर किया गया है (kAudioFormatAppleLossless), लेकिन अगर मैं कोशिश करते हैं और kAudioFormatMPEG4AAC एएसी उपयोग करने के लिए (ऑडियो AVAssetWriterInput कॉन्फ़िगर) इसे सफलतापूर्वक 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} 
के साथ विफल

यहाँ कोड और AVAssetWriterInputs मैं अपने AVAssetWriter बनाने के लिए उपयोग है

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]; 

किसी को भी पता है कि कैसे सही ढंग से लिखने के लिए ऑडियो नमूने एक AVAssetWriterInput एएसी लिखने के लिए कॉन्फ़िगर का उपयोग कर AVCaptureSession से लौटे?

उत्तर

12

मैं अब यह AVEncoderBitRateKey पैरामीटर वांछित आउटपुट सेटिंग्स के रूप में पारित कर दिया बदलने से कोई AVAssetWriterInput लेखन AAC ऑडियो में सक्षम आरंभ के लिए 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

नए आईफोन 4 एस के साथ मुझे AVNumberOfChannelsKey को 2 – RyanSullivan

+0

में भी बदलना पड़ा, मैं इसे समझ नहीं पाया! मुझे पता था कि विकल्पों के साथ इसका कुछ संबंध था, लेकिन वास्तव में नहीं पता था कि क्या। इसके लिए धन्यवाद! –

संबंधित मुद्दे