使用 AVAssetWriter 录制视频时获取视频时长

Get duration of video while it's being recorded with AVAssetWriter

我有一个 class 可以将视频和音频录制到 .mp4 文件中。我使用 AVAssetWriterAVAssetWriterInput 来完成这样的任务

如何在使用这种方法录制视频时获取视频的时长(AVAssetWriter)?

您可以在开始 startSession 时保存开始时间

let startTimeStamp = CMSampleBufferGetPresentationTimeStamp(sample)
fileWriter.startSession(atSourceTime: startTimeStamp)
startTime = Double(startTimeStamp.value) / Double(startTimeStamp.timescale)

你将获得的每个缓冲区都这样做:

let currentTimeStamp = CMSampleBufferGetPresentationTimeStamp(sample)
let currentTime = Double(currentTimeStamp.value) / Double(currentTimeStamp.timescale)

print("Duration - \(currentTime - startTime)")