如何为AVWriter写入设置CMSampleBuffer的时间戳 [英] How to set timestamp of CMSampleBuffer for AVWriter writing

查看:721
本文介绍了如何为AVWriter写入设置CMSampleBuffer的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working with AVFoundation for capturing and recording audio. There are some issues I don't quite understand.

基本上我想从AVCaptureSession捕获音频并使用AVWriter写入它,但是我需要在时间戳中移动一些CMSampleBuffer我从AVCaptureSession获得。我阅读了CMSampleBuffer的文档。我看到两个不同的时间戳术语:'presentation timestamp'和'output presentation timestamp'。这两者有什么不同?

Basically I want to capture audio from AVCaptureSession and write it using AVWriter, however I need some shifting in the timestamp of the CMSampleBuffer I get from AVCaptureSession. I read documentation of CMSampleBuffer I see two different term of timestamp: 'presentation timestamp' and 'output presentation timestamp'. What the different of the two ?

假设我从AVCaptureSession获取CMSampleBuffer(用于音频)实例,并且我想使用AVWriter将其写入文件,我应该使用什么函数来注入一个CMTime到缓冲区,以便在结果文件中设置它的显示时间戳?

Let say I get a CMSampleBuffer (for audio) instance from AVCaptureSession, and I want to write it to a file using AVWriter, what function should I use to 'inject' a CMTime to the buffer in order to set the presentation timestamp of it in the resulting file ?

谢谢。

推荐答案

此处的示例代码块: http: //www.gdcl.co.uk/2013/02/20/iPhone-Pause.html
CMSampleBufferRef示例 - 是您的sampleBuffer,CMSampleBufferRef sout您的输出。 NewTimeStamp是你的时间戳。

Chunk of sample code from here: http://www.gdcl.co.uk/2013/02/20/iPhone-Pause.html CMSampleBufferRef sample - is your sampleBuffer, CMSampleBufferRef sout your output. NewTimeStamp is your time stamp.

CMItemCount count;
CMTime newTimeStamp = CMTimeMake(YOURTIME_GOES_HERE);
CMSampleBufferGetSampleTimingInfoArray(sample, 0, nil, &count);
CMSampleTimingInfo* pInfo = malloc(sizeof(CMSampleTimingInfo) * count);
CMSampleBufferGetSampleTimingInfoArray(sample, count, pInfo, &count);
for (CMItemCount i = 0; i < count; i++)
{
    pInfo[i].decodeTimeStamp = newTimeStamp; // kCMTimeInvalid if in sequence
    pInfo[i].presentationTimeStamp = newTimeStamp;

}
CMSampleBufferRef sout;
CMSampleBufferCreateCopyWithNewTiming(kCFAllocatorDefault, sample, count, pInfo, &sout);
free(pInfo);

这篇关于如何为AVWriter写入设置CMSampleBuffer的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆