swift - 如何在录制时获取 AVCaptureMovieFileOutput 的当前长度以显示在屏幕上?

标签 swift video time avfoundation recording

正如标题所述,我希望在用户使用 AVFoundation 录制视频时显示当前的录制长度。我对 AVAudioRecorder 做了类似的事情,我创建了一个计时器来设置标签的文本,如下所示:

recordDurationLabel.text = timeString((audioRecorder?.currentTime)!)

我想知道是否有用于录制视频的“当前时间”功能?我唯一的其他想法是在 captureOutput:didStartRecordingToOutputFileAtURL 函数中启动重复的 1 秒计时器,但我想确保准确性。

提前致谢!

编辑: 目前,这是我在 captureOutput:didStartRecordingToOutputFileAtURL 委托(delegate)方法中实现的函数,但同样,拥有与视频直接相关的内容会更清晰。

func startRecordingUpdateTimer() {

    recordingUpdateTimer = NSTimer(timeInterval: 1.0/45.0, target: self, selector: #selector(recordedVideo), userInfo: nil, repeats: true)
    NSRunLoop.mainRunLoop().addTimer(recordingUpdateTimer!, forMode: NSRunLoopCommonModes)
}

func recordedVideo() {
    durationTime = durationTime + 1.0/45.0
    let durationString = timeString(durationTime)
    recordDurationLabel.text =  durationString
}

最佳答案

使用 AVCaptureMovieFileOutput,您可以使用 recordedDuration 获取电影的持续时间,它是只读的,并会在录制开始时返回 CMTime。请参阅AVCaptureFileOutput .

如果您使用 captureOutput:didOutputSampleBuffer:fromConnection:,您将收到 CMSampleBuffer 对象,其中包含您可以使用 访问的 CMTime >CMSampleBufferGetPresentationTimeStamp

执行诸如CMTimeSubstract之类的CMTime操作将允许您计算当前缓冲区与您记录的第一个缓冲区之间的时间差。

如果您从未操作过sampleBuffers,您应该看一下Apple 的示例代码:RosyWriter

关于swift - 如何在录制时获取 AVCaptureMovieFileOutput 的当前长度以显示在屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38443736/

相关文章:

ios - iOS 中的操作同步挑战

javascript - 我可以在 Swift 代码中运行 JavaScript 吗?

swift - 获取引用图像的名称 swift ARKit 1.5

python - pandas - 如何将列转换为日期时间对象

c# - 如何将秒转换为时分秒?

swift - 检测用户何时单击 NSTableView

android - MediaCodec - 将图像编码为 mp4 视频

video - 如何使用ffmpeg分块下载m3u8?

android - 如何禁用屏幕镜像?

java - 在Java中,有没有可能执行一个方法一段时间,达到时间限制就停止?