gpt4 book ai didi

ios - iOS中如何从CMSampleBufferRef获取Camera数据的当前捕获时间戳

转载 作者:技术小花猫 更新时间:2023-10-29 10:53:08 29 4
gpt4 key购买 nike

我开发了一个 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)。在我尝试读取文件后,它给了我不同的值(value)。这有什么具体原因吗??

最佳答案

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

 presentationTimeStamp = fileFramePresentationTime + firstFrameCaptureTime

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

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

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

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

关于ios - iOS中如何从CMSampleBufferRef获取Camera数据的当前捕获时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29296162/

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