如何写一个AVAssetWriter我的自定义float / shorts数组?

时间:2015-01-20 15:05:27

标签: objective-c core-audio avassetwriter

所以我使用AVAssetReader得到CMSampleBufferRef,然后我从CMBlockBufferRef数据中获取了样本值。

然后,我使用自定义过滤器更改了此示例。

现在我有一个Shorts数组,我想用AVAssetWriter写回文件。

我的问题是如何创建一个CMSampleBufferRef和一个CMBlockBufferRef来发送给AVAssetWriter?

1 个答案:

答案 0 :(得分:0)

看看这个apple developer page

修改 根据您的意见,要直接从short数组创建samplebuffer,您应该使用Core Media Framework组件,即CMSampleBufferCreate

  1. 将short数组中的样本数据转换为AudioBufferList
  2. 将AudioBufferList转换为CMSampleBuffer。
  3. 例如:

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