Tôi đang sử dụng AVCaptureSession để chụp các mẫu âm thanh và video từ micrô và máy ảnh của thiết bị.Làm cách nào để sử dụng AVAssetWriter để ghi âm thanh AAC ra trong ios?
Tôi sau đó cố gắng để viết CMSampleBuffers (Sử dụng AVAssetWriter và AVAssetWriterInputs) trả về thông qua phương pháp AVCaptureSessions đại biểu
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer: (CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
này hoạt động tốt khi AVAssetWriterInput audio của tôi được cấu hình để viết dữ liệu ra định dạng Apple Lossless (kAudioFormatAppleLossless) nhưng nếu tôi cố gắng và cấu hình AVAssetWriterInput âm thanh để sử dụng AAC (kAudioFormatMPEG4AAC) nó viết thành công video và âm thanh mẫu trong khoảng 500ms sau đó không thành công với các lỗi sau
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}
Đây là mã tôi sử dụng để tạo AVAssetWriter tôi và 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];
Có ai biết làm thế nào để viết một cách chính xác các mẫu âm thanh trở về từ AVCaptureSession sử dụng một AVAssetWriterInput cấu hình để viết AAC?
Với iPhone 4s mới, tôi cũng phải thay đổi AVNumberOfChannelsKey thành 2 – RyanSullivan
Tôi không thể hình dung điều này! Tôi biết nó có liên quan đến các lựa chọn, nhưng không biết chính xác cái gì. Cám ơn vì cái này! –