我正在尝试从相机中捕获帧和相关的运动数据。为了同步,我使用时间戳。视频和运动数据被写入文件,然后进行处理。在此过程中,我可以计算出每个视频的运动帧偏移。
结果发现,相同时间戳的运动数据和视频数据彼此之间的偏移量不同,从0.2秒到0.3秒不等。这个偏移量对于一个视频来说是恒定的,但对于不同的视频则有所不同。如果每次都是相同的偏移量,我就可以减去一些校准值,但事实并非如此。
有没有好的方法来同步时间戳?也许我没有正确记录它们?有没有更好的方法将它们带到同一参考帧上?
CoreMotion返回相对于系统正常运行时间的时间戳,因此我需要添加偏移量才能得到Unix时间:
uptimeOffset = [[NSDate date] timeIntervalSince1970] -
[NSProcessInfo processInfo].systemUptime;
CMDeviceMotionHandler blk =
^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error){
if(!error){
motionTimestamp = motion.timestamp + uptimeOffset;
...
}
};
[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical
toQueue:[NSOperationQueue currentQueue]
withHandler:blk];
为了高精度地获取帧时间戳,我使用了AVCaptureVideoDataOutputSampleBufferDelegate。它也与Unix时间偏移量相关:
-(void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
CMTime frameTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
if(firstFrame)
{
firstFrameTime = CMTimeMake(frameTime.value, frameTime.timescale);
startOfRecording = [[NSDate date] timeIntervalSince1970];
}
CMTime presentationTime = CMTimeSubtract(frameTime, firstFrameTime);
float seconds = CMTimeGetSeconds(presentationTime);
frameTimestamp = seconds + startOfRecording;
...
}