ios - 如何为AVWriter写入设置CMSampleBuffer的时间戳

标签 ios avfoundation audio-recording cmsamplebufferref core-media

我正在使用 AVFoundation 来捕获和录制音频。有些问题我不太明白。

基本上我想从 AVCaptureSession 捕获音频并使用 AVWriter 写入它,但是我需要对从 AVCaptureSession 获得的 CMSampleBuffer 的时间戳进行一些调整。我阅读了 CMSampleBuffer 的文档,我看到了两个不同的时间戳术语:“演示时间戳”和“输出演示时间戳”。两者有何不同?

假设我从 AVCaptureSession 获得一个 CMSampleBuffer(用于音频)实例,我想使用 AVWriter 将它写入一个文件,我应该使用什么函数将 CMTime“注入(inject)”缓冲区以设置演示时间戳它在结果文件中?

谢谢。

最佳答案

使用 CMSampleBufferGetPresentationTimeStamp,这是缓冲区被捕获的时间,应该在回放同步时“呈现”。引用 WWDC 2012 session 520 的话:“演示时间是缓冲区中第一个样本被麦克风拾取的时间”。

如果你用

启动AVWriter
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];

然后附加样本

if(videoWriterInput.readyForMoreMediaData) [videoWriterInput appendSampleBuffer:sampleBuffer];

成品视频中的帧数会和CMSampleBufferGetPresentationTimeStamp一致(我查过了)。如果你想在添加样本时修改时间,你必须使用 AVAssetWriterInputPixelBufferAdaptor

关于ios - 如何为AVWriter写入设置CMSampleBuffer的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14786619/

相关文章:

ios - 如何使用设备支持的文件从 Xcode 10.2.1 在 iOS 13 设备中运行/安装应用程序

objective-c - 如何从应用程序委托(delegate)访问 View Controller 变量......反之亦然?

xcode - 在 Swift 中返回一个未包装的可选?

ios - 在表格 View 中缓存视频

android - Audiorecord.read() 值的含义

ios - 在屏幕上滑动(相同的)图像,但不使用任何额外的 UIImageViews

ios - dyld : Library not loaded: @rpath/libswiftCore. 动态库

ios - 捕获完成后从 AVCapturePhotoOutput 获取使用的曝光持续时间和 ISO 值

android - 在 Android 上分析 AudioTrack(Visualizer 的替代品)

ios - AVAudioRecorder 可以用来录制来自您的 iOS 应用程序的音频吗?