【问题标题】:Starting recording at exact timestamp在确切的时间戳开始录制
【发布时间】:2016-08-26 17:43:50
【问题描述】:

我正在尝试以确切的时间戳开始录制视频。

我已经设法通过使用找到的方法获取每个帧的时间戳:herehere

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{ 
    CMTime time = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
    ...
    // check if time > timestampToStart, use these frames

}

通过这样做,我只能使用我指定的时间戳之后的帧。

问题是在 timstampToStart 处使用的第一帧不会完全,而是在 timstampToStart 加上一个可以在 0-1/fps 之间的小间隔(通常 fps = 24)。

如何确保使用的第一帧与我想要的时间戳完全(或非常接近)

【问题讨论】:

    标签: ios objective-c avfoundation avcapturesession


    【解决方案1】:

    当您需要时间关键的功能时,请使用NSThread

    - (IBAction)startCapture:(id) sender
     {
        NSDate * startTime = [now dateByAddingTimeInterval: 10.12345];
    
        NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(capture:) object:startTime];
        [thread start];
    }
    
    - (void)capture:(id) context
    {
        NSDate * startTime = (NSDate *) context;
        [NSThread sleepUntilDate:startTime];
    
        // Do your things...
    }
    

    除非您的 CPU 非常繁忙,否则线程将在设定时间的几纳秒内唤醒。但是,创建线程需要一些时间,因此不要将开始时间设置得如此紧密。 OS X 上的线程创建时间约为~90 microseconds。Apple 没有说明iOS 上的时间。

    【讨论】:

    • 感谢您的回答。但是,在我的情况下,“做你的事情”是需要时间的,并且以这种方式开始不会使我录制的视频的第一帧正好在我想要的时间戳上
    • 然后在sleepUntilDate调用之前进行
    • 缓冲流来自 avcapturesession,不幸的是,这种方式不能“暂停”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多