所以我使用AVAssetReader
得到CMSampleBufferRef
,然后我从CMBlockBufferRef
数据中获取了样本值。
然后,我使用自定义过滤器更改了此示例。
现在我有一个Shorts数组,我想用AVAssetWriter写回文件。
我的问题是如何创建一个CMSampleBufferRef
和一个CMBlockBufferRef
来发送给AVAssetWriter?
答案 0 :(得分:0)
看看这个apple developer page。
修改强> 根据您的意见,要直接从short数组创建samplebuffer,您应该使用Core Media Framework组件,即CMSampleBufferCreate
例如:
OSStatus status = noErr;
CMItemCount framesToProcess = 8192;
unsigned long sizeInBytes = framesToProcess * sizeof(SInt16);
// Init ABL from sample array
AudioBufferList *myAudioBufferList; // contains converted
myAudioBufferList = static_cast<AudioBufferList *>(calloc(1, offsetof(AudioBufferList, mBuffers) + (sizeof(AudioBuffer))));
myAudioBufferList->mNumberBuffers = 1;
myAudioBufferList->mBuffers[0].mNumberChannels = 1;
myAudioBufferList->mBuffers[0].mData = JoãoSamplesArrayPointer;
myAudioBufferList->mBuffers[0].mDataByteSize = (UInt32)sizeInBytes;
// sampleBuffer in (from the assetreader) you already have this
CMSampleBufferRef sampleBufferIn = [self.assetReaderAudioOutput copyNextSampleBuffer];
CMAudioFormatDescriptionRef format = CMSampleBufferGetFormatDescription(sampleBufferIn);
const AudioStreamBasicDescription *assetReaderOutputASBD = CMAudioFormatDescriptionGetStreamBasicDescription(format);
// setup output samplebuffer
CMSampleBufferRef sampleBufferOut = NULL;
CMSampleTimingInfo timing = { CMTimeMake(1, sampleRate), kCMTimeZero, kCMTimeInvalid };
// create description
status = CMAudioFormatDescriptionCreate(kCFAllocatorDefault, assetReaderOutputASBD, 0, NULL, 0, NULL, NULL, &format);
// create buffer
CMSampleBufferCreate( kCFAllocatorDefault, NULL, false, NULL, NULL, format, framesToProcess, 1, &timing, 0, NULL, &sampleBufferOut);
// put data into buffer from ABL (audio buffer list)
status = CMSampleBufferSetDataBufferFromAudioBufferList( sampleBufferOut, kCFAllocatorDefault, kCFAllocatorDefault, 0, myAudioBufferList );
// write to assetwriter audioinput
BOOL success = [self.assetWriterAudioInput appendSampleBuffer:sampleBufferOut];
CFRelease(sampleBufferOut);