gpt4 book ai didi

swift - 如何将包含 AAC 数据的 AudioBufferList 转换为 CMSampleBuffer

转载 作者:行者123 更新时间:2023-12-04 10:52:35 24 4
gpt4 key购买 nike

我正在使用 AudioConverter转换未压缩 CMSampleBuffer通过 AVCaptureSession 被捕获至 AudioBufferList :

let status: OSStatus = AudioConverterFillComplexBuffer(
converter,
inputDataProc,
Unmanaged.passUnretained(self).toOpaque(),
&ioOutputDataPacketSize,
outOutputData.unsafeMutablePointer,
nil
)

我的输出 asbd 设置如下:
AudioStreamBasicDescription
- mSampleRate : 44100.0
- mFormatID : 1633772320
- mFormatFlags : 2
- mBytesPerPacket : 0
- mFramesPerPacket : 1024
- mBytesPerFrame : 0
- mChannelsPerFrame : 1
- mBitsPerChannel : 0
- mReserved : 0

我想转换 AudioBufferList回到 CMSampleBuffer包含压缩数据,以便我可以使用 AVAssetWriter 将其写入 mp4 文件(我已经想出了如何用视频来做到这一点),但到目前为止几乎没有。我试过咨询 this answer但在这种情况下,有 PCM 数据,它在这里似乎不可用。

我可以访问 AudioBufferList以及原始样本的presentationTimeStamp。我已经尝试了以下方法,但我不太确定如何计算 numSamples 以及这种方法是否有意义:
 func createCMSampleBuffer(_ data: UnsafeMutableAudioBufferListPointer, presentationTimeStamp: CMTime) -> CMSampleBuffer? {
let numSamples = // not sure how to get this

var status: OSStatus = noErr
var sampleBuffer: CMSampleBuffer?
var timing: CMSampleTimingInfo = CMSampleTimingInfo(
duration: CMTime(value: CMTimeValue(numSamples), timescale: presentationTimeStamp.timescale),
presentationTimeStamp: presentationTimeStamp,
decodeTimeStamp: CMTime.invalid
)

status = CMSampleBufferCreate(
allocator: kCFAllocatorDefault,
dataBuffer: nil,
dataReady: false,
makeDataReadyCallback: nil,
refcon: nil,
formatDescription: formatDescription,
sampleCount: CMItemCount(numSamples),
sampleTimingEntryCount: 1,
sampleTimingArray: &timing,
sampleSizeEntryCount: 0,
sampleSizeArray: nil,
sampleBufferOut: &sampleBuffer
)

guard status == noErr else {
return nil
}

status = CMSampleBufferSetDataBufferFromAudioBufferList(
sampleBuffer!,
blockBufferAllocator: kCFAllocatorDefault,
blockBufferMemoryAllocator: kCFAllocatorDefault,
flags: 0,
bufferList: data.unsafePointer
)

guard status == noErr else {
return nil
}

return sampleBuffer
}

最后我确实设法创建了一个 CMSampleBuffer ,但是当我尝试完成写作时,出现以下错误:
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x174442ac0 {Error Domain=NSOSStatusErrorDomain Code=-12735 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-12735), NSLocalizedDescription=The operation could not be completed}

最佳答案

我可以分享一些关于这方面的研究工作,它可能会帮助你......
CMSampleBufferSetDataBufferFromAudioBufferList returned error: -12731
解决方案:- https://lists.apple.com/archives/coreaudio-api/2014/Mar/msg00008.html

或者 Converting AudioBuffer to CMSampleBuffer with accurate CMTime

可能会帮助你...:)

关于swift - 如何将包含 AAC 数据的 AudioBufferList 转换为 CMSampleBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59401353/

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