我有以下代码将视频(60fps)导出到图像序列。然而,尽管有大量的 stackoverflow 示例,我似乎没有正确掌握 CMTime,因为图像 1-30 是相同的,然后 31-90 是相同的,然后是 91-130、131-150 ......就像生成的序列一样将是定格动画。
var seq=0
let duration:Float64 = CMTimeGetSeconds(asset.duration)
let fps = tracks.first!.nominalFrameRate
for index:Float64 in stride(from: 0, to: duration, by: Double(1/fps)) {
let time:CMTime = CMTimeMakeWithSeconds(index, preferredTimescale:Int32(NSEC_PER_SEC))
var image:CGImage
do {
try image = generator.copyCGImage(at:time, actualTime:nil)
let bitmapRep = NSBitmapImageRep(cgImage: image)
let data = bitmapRep.representation(using: .jpeg, properties: [:])
let oneimagepath=newpath.appendingPathComponent("Sequence.\(String(format: "%04d", seq)).jpg")
seq = seq+1
try data?.write(to: oneimagepath, options: .atomicWrite)
} catch {
print(error.localizedDescription)
}
}
问题出在我的 CMTime 还是其他地方?就像它将我的十进制时间转换为 int 一样。