【问题标题】:Presentation time of Audio buffer and Video buffer are not equal音频缓冲区和视频缓冲区的呈现时间不相等
【发布时间】:2021-09-07 00:40:49
【问题描述】:

我正在尝试创建一个使用 AVFoundation 进行实时视频和音频录制的应用。 我还使用 AVAssetWriter 将缓冲区写入本地文件。

对于视频CMSampleBuffer,我使用AVCaptureSession 中的AVCaptureVideoDataOutputSampleBufferDelegate 输出,这很简单。

对于音频CMSampleBuffer,我正在从 AudioUnit 记录回调创建缓冲区。 我计算音频缓冲区的呈现时间的方式是这样的:

        var timebaseInfo = mach_timebase_info_data_t(numer: 0, denom: 0)
        let timebaseStatus = mach_timebase_info(&timebaseInfo)
        if timebaseStatus != KERN_SUCCESS {
            debugPrint("not working")
            return
        }
        let hostTime = time * UInt64(timebaseInfo.numer / timebaseInfo.denom)
        let presentationTIme = CMTime(value: CMTimeValue(hostTime), timescale: 1000000000)
        let duration = CMTime(value: CMTimeValue(1), timescale: CMTimeScale(self.sampleRate))

        var timing = CMSampleTimingInfo(
            duration: duration,
            presentationTimeStamp: presentationTIme,
            decodeTimeStamp: CMTime.invalid
        )

self.sampleRate是一个在录制开始时会发生变化的变量,但大多数时候是48000

当同时获得视频和音频的CMSampleBuffers 时,演示时间有很大的不同。

音频 - CMTime(value: 981750843366125, timescale: 1000000000, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0)

视频 - CMTime(value: 997714237615541, timescale: 1000000000, flags: __C.CMTimeFlags(rawValue: 1), epoch: 0)

这会在尝试将缓冲区写入文件时产生很大的差距。

我的问题是

  1. 我是否正确计算了音频缓冲区的呈现时间?如果是这样,我错过了什么?
  2. 如何确保音频和视频在同一时间范围内(我知道它们之间应该存在微小的毫秒差异)

【问题讨论】:

  • 你不会被time * UInt64(timebaseInfo.numer / timebaseInfo.denom) 截断吗?不应该是(time*numer)/denom吗?

标签: swift audio avfoundation audiounit cmsamplebuffer


【解决方案1】:

好吧,这是我的错。 正如 cmets 中的 Rhythmic Fistman 所建议的那样,我的时间计算被截断了:

let hostTime = time * UInt64(timebaseInfo.numer / timebaseInfo.denom)

更改为这个计算修复了它

let hostTime = (time * UInt64(timebaseInfo.numer)) / UInt64(timebaseInfo.denom)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 2016-01-10
    相关资源
    最近更新 更多