本文介绍了如何在Swift中将CMSampleBuffer转换为数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将CMSampleBuffer转换为Data格式.我正在使用一个第三方框架来执行与音频相关的任务.该框架为我提供了CMSampleBuffer对象中的流式(即实时音频)音频.

I need to convert CMSampleBuffer to Data format. I am using one Third party framework for audio related task. That framework gives me the streaming (i.e Real Time audio) audio in CMSampleBuffer object.

赞:

func didAudioStreaming(audioSample: CMSampleBuffer!) {
    //Here I need to conver this to Data format.
    //Because I am using GRPC framework for Audio Recognization,
}

请向我提供将CMSampleBuffer转换为Data的步骤.

Please provide me the steps to convert the CMSampleBuffer to Data.

仅供参考

    let formatDesc:CMFormatDescription? = CMSampleBufferGetFormatDescription(audioSample)

    <CMAudioFormatDescription 0x17010d890 [0x1b453ebb8]> {
    mediaType:'soun'
    mediaSubType:'lpcm'
    mediaSpecific: {
        ASBD: {
            mSampleRate: 16000.000000
            mFormatID: 'lpcm'
            mFormatFlags: 0xc
            mBytesPerPacket: 2
            mFramesPerPacket: 1
            mBytesPerFrame: 2
            mChannelsPerFrame: 1
            mBitsPerChannel: 16     }
        cookie: {(null)}
        ACL: {(null)}
        FormatList Array: {(null)}
    }
    extensions: {(null)}
}

推荐答案

尝试下面的代码将CMSampleBuffer转换为NSData.

Try below code to convert CMSampleBuffer to NSData.

let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
let height = CVPixelBufferGetHeight(imageBuffer!)
let src_buff = CVPixelBufferGetBaseAddress(imageBuffer!)
let data = NSData(bytes: src_buff, length: bytesPerRow * height)
CVPixelBufferUnlockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))

编辑-

对于AudioBuffer,请使用以下代码-

For AudioBuffer use below code -

var audioBufferList = AudioBufferList()
var data = Data()
var blockBuffer : CMBlockBuffer?

CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, nil, &audioBufferList, MemoryLayout<AudioBufferList>.size, nil, nil, 0, &blockBuffer)

let buffers = UnsafeBufferPointer<AudioBuffer>(start: &audioBufferList.mBuffers, count: Int(audioBufferList.mNumberBuffers))

for audioBuffer in buffers {
    let frame = audioBuffer.mData?.assumingMemoryBound(to: UInt8.self)
    data.append(frame!, count: Int(audioBuffer.mDataByteSize))
}

这篇关于如何在Swift中将CMSampleBuffer转换为数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 19:26