Видео с QuickTime не воспроизводит звук, а VLC воспроизводит звук
Сначала я использую AVCaptureSession для захвата видео и аудио с камеры и микрофона. Затем делегируйте код метода ниже:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
// if is Vedio data ,append to vedioInputWriter
if (mediaType == kCMMediaType_Vedio){
// ...
// ...
// ...
// write the video data
if (_assetWriterVideoInput.readyForMoreMediaData){
[_assetWriterInputPixelBufferAdaptor appendPixelBuffer:renderedOutputPixelBuffer withPresentationTime:timestamp];
}
return;
}
size_t bufferListSizeNeededOut;
CMBlockBufferRef blockBufferOut = nil;
// get AudioBufferList
err = CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer,
&bufferListSizeNeededOut,
currentInputAudioBufferList,
CAAudioBufferList::CalculateByteSize(currentInputASBD.mChannelsPerFrame),
kCFAllocatorSystemDefault,
kCFAllocatorSystemDefault,
kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment,
&blockBufferOut);
// mix recorded audio with background music
[self mixAudioBufferListwithBackgroundMusic:currentInputAudioBufferList];
if (!self.audioInputAssetWriter.readyForMoreMediaData) {
if (self.assetWriter.status == AVAssetWriterStatusFailed) {
NSLog(@"assetWrite error=%@",self.viewController.assetWriter.error);
}
return;
}
// tranfer `AudioBufferList` to `CMSampleBuffer`
CMSampleBuffer newSampleBuffer = [self processAudioData:outputBufferList->ABL() framesNumber:numberOfFrames format:&mClientFormat];
// append samplebuffer to assetInputWriter
if ([self.audioInputAssetWriter appendSampleBuffer:newSampleBuffer]) {
NSLog(@" append sample buffer success");
}else{
NSLog(@"append sample buffer failed");
}
}
Tranfer AudioBufferList
в CMSampleBuffer
код ниже:
- (CMSampleBufferRef)processAudioData:(AudioBufferList *)audioData framesNumber:(UInt32 )num format:(AudioStreamBasicDescription *)asbd
{
CMSampleBufferRef sbuf;
OSStatus status;
// 1.audio foramt description
CMAudioFormatDescriptionRef format;
status = CMAudioFormatDescriptionCreate(kCFAllocatorDefault,&(bufferToSampleFormat), 0, NULL, 0, NULL, NULL, &format);
// 2.sample timing info
CMSampleTimingInfo timing;
timing.duration = CMTimeMake(1,44100);
timing.presentationTimeStamp = kCMTimeZero;
timing.decodeTimeStamp = kCMTimeInvalid;
size_t sampleSizeArray[1] = {4};
// 3.create sample buffer
status = CMSampleBufferCreate(kCFAllocatorDefault,NULL,false, NULL,NULL,format,(CMItemCount)num,1,&timing,0,NULL, &sbuf);
// 4.sample buffer set data buffer from audio buffer list
status = CMSampleBufferSetDataBufferFromAudioBufferList(sbuf, kCFAllocatorDefault, kCFAllocatorDefault, 0, audioData);
if (status != noErr) {
NSLog(@"sample buffer set data buffer from audio buffer list failed -- %d",(int)status);
return nil;
}
return sbuf;
}
Наконец, сохраните видео и аудио данные как test.mov
file.Question - воспроизведение файла с Quicktime, но без звука. Воспроизведите файл с VLC , имейте звук. И можете очистить аудиофайл формата aac с помощью ffmpeg.
Я думаю, что проблема в том, AudioBufferList
в CMSampleBuffer
, пропуская некоторую информацию приводит к Quicktime не может играть правильно.