【问题标题】:How to get the current captured timestamp of Camera data from CMSampleBufferRef in iOS如何从 iOS 中的 CMSampleBufferRef 获取当前捕获的相机数据时间戳
【发布时间】:2020-12-07 19:25:50
【问题描述】:

我开发了一个 iOS 应用程序,它将捕获的相机数据保存到一个文件中,我使用了

(void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

捕获 CMSampleBufferRef 并将其编码为 H264 格式,并使用 AVAssetWriter 将帧保存到文件中。

我关注the sample source code创建了这个应用:

现在我想获取保存的视频帧的时间戳来创建一个新的电影文件。为此,我做了以下几件事

  1. 找到文件并创建AVAssestReader来读取文件

    CMSampleBufferRef sample = [asset_reader_output copyNextSampleBuffer];   
    CMSampleBufferRef buffer;
    
    while ([assestReader status] == AVAssetReaderStatusReading) {
        buffer = [asset_reader_output copyNextSampleBuffer];
    
        // CMSampleBufferGetPresentationTimeStamp(buffer);
    
        CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(buffer);
        UInt32 timeStamp = (1000 * presentationTimeStamp.value) / presentationTimeStamp.timescale;
    
        NSLog(@"timestamp %u", (unsigned int) timeStamp);
        NSLog(@"reading");
    
        // CFRelease(buffer);
    }
    

打印的值给了我一个错误的时间戳,我需要获取帧的捕获时间。

有什么方法可以获取帧捕获时间戳?

我已阅读 an answer 以获取时间戳,但它没有正确阐述我上面的问题。

更新:

我在写入文件之前读取了示例时间戳,它给了我xxxxx 值(33333.23232)。在我尝试读取文件后,它给了我不同的价值。有什么具体原因吗??

【问题讨论】:

  • @fireclaw48 我找到了这个链接,但它没有给出正确的答案,我认为这是设置新的时间戳
  • 我也遇到了同样的问题,你得到答案了吗?

标签: ios objective-c avfoundation cmsamplebufferref


【解决方案1】:

文件时间戳与捕获时间戳不同,因为它们是相对于文件开头的。这意味着它们是您想要的捕获时间戳,减去捕获的第一帧的时间戳:

 presentationTimeStamp = fileFramePresentationTime + firstFrameCaptureTime

所以从文件中读取时,这应该计算出你想要的捕获时间戳:

 CMTime firstCaptureFrameTimeStamp = // the first capture timestamp you see
 CMTime presentationTimeStamp = CMTimeAdd(CMSampleBufferGetPresentationTimeStamp(buffer), firstCaptureFrameTimeStamp);

如果您在应用启动之间进行此计算,则需要序列化和反序列化第一帧捕获时间,您可以使用 CMTimeCopyAsDictionaryCMTimeMakeFromDictionary 进行此操作。

您可以通过AVAssetWritermetadata 属性将其存储在输出文件中。

【讨论】:

  • 谢谢您的回答我会检查这个,为什么捕获时间戳不等于实际时间戳,我打印了这个值,它与网络中的不同
  • 好问题,我不确定为什么捕获时间戳与您开始捕获的时间无关(为什么 它们 不从 0 开始)。
  • 无论如何我可以连接到从相机委托方法接收到真实时间戳的演示时间戳,??我必须为此使用地图吗?但这似乎是一种资源浪费
  • 在我的情况下,必须为整部电影创建一个单独的剪辑,为此我必须从后端服务器同步时间戳,为此,我需要连接到演示时间-带有实际时间戳的戳记,我用元数据试试这个,
猜你喜欢
  • 2018-10-18
  • 1970-01-01
  • 2011-02-16
  • 2021-12-04
  • 2014-09-04
  • 2012-03-04
  • 2017-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多