gpt4 book ai didi

ios - 从 AAC 数据创建 CMSampleBuffer

转载 作者:行者123 更新时间:2023-11-29 11:27:44 27 4
gpt4 key购买 nike

我正在使用同时录制音频和视频的外部摄像头。

我的应用程序接收具有以下结构的 AAC 格式的音频:

struct AudioPacket {
let timestamp: TimeInterval
let data: Data
let asbd: AudioStreamBasicDescription
let magicCookie: Data
let audioSpecificConfigData: Data
}

AudioStreamBasicDescription 属性具有以下内容:

- mSampleRate : 48000.0
- mFormatID : 1633772320
- mFormatFlags : 0
- mBytesPerPacket : 0
- mFramesPerPacket : 1024
- mBytesPerFrame : 0
- mChannelsPerFrame : 1
- mBitsPerChannel : 0
- mReserved : 0

我正在使用 AVAssetWriter 将音频和视频写入文件,并且需要向 Assets 编写器提供 CMSampleBuffer-s。

那么如何将上面的 AudioPacket 结构转换为 CMSampleBuffer 呢?

最佳答案

不是一个确切的答案,但我会尝试类似下面的方法:

func convertPacket(_ packet: AudioPacket) -> CMSampleBuffer? {
var data = packet.data
var asbd = packet.asbd
var magicCookie = packet.magicCookie
var blockBuffer: CMBlockBuffer?
var formatDescription: CMFormatDescription?
var sampleBuffer: CMSampleBuffer?
CMBlockBufferCreateWithMemoryBlock(
allocator: nil, memoryBlock: &data, blockLength: data.count,
blockAllocator: nil, customBlockSource: nil, offsetToData: 0,
dataLength: data.count, flags: 0, blockBufferOut: &blockBuffer
)
CMAudioFormatDescriptionCreate(
allocator: nil, asbd: &asbd, layoutSize: 0, layout: nil,
magicCookieSize: magicCookie.count, magicCookie: &magicCookie,
extensions: nil, formatDescriptionOut: &formatDescription
)
CMAudioSampleBufferCreateWithPacketDescriptions(
allocator: nil, dataBuffer: blockBuffer, dataReady: true,
makeDataReadyCallback: nil, refcon: nil, formatDescription: formatDescription!,
sampleCount: 1, // <- provide correct number
presentationTimeStamp: CMTime(seconds: packet.timestamp, preferredTimescale: 100),
packetDescriptions: nil, sampleBufferOut: &sampleBuffer
)
return sampleBuffer
}

关于ios - 从 AAC 数据创建 CMSampleBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56960092/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com