我正在尝试获取以毫秒为单位的视频帧。但是当我得到 24 fps 的 10 秒视频帧时,我每秒得到相同的 24 帧。但我实际上需要以毫秒为单位获取帧,而不是 24 个相同的帧。
我每秒有 24 帧,但 24 帧与第一帧相同。我需要 24 个不同的帧。如何以毫秒为单位选择帧?
以下是我的审查代码:
var generator : AVAssetImageGenerator!
func getAllFrames(_ videoUrl : URL, _ fps : Float)
{
let asset:AVAsset = AVAsset(url: videoUrl)
let duration : Float = Float(CMTimeGetSeconds(asset.duration))
generator = AVAssetImageGenerator(asset:asset)
generator.appliesPreferredTrackTransform = true
var frameImages = [UIImage]()
let totalFrames = (Int(duration) + 1) * Int(fps)
for index in 0..<totalFrames
{
let time = Float(index) / fps
if let image = getFrame(fromTime: Float64(time), fps: Int32(fps + 1.0))
{
images.append(image)
}
}
generator = nil
return images
}
// get frame in milli second
private func getFrame(fromTime:Float64, fps : Int32) -> UIImage?
{
let time:CMTime = CMTimeMakeWithSeconds(fromTime, preferredTimescale: fps)
let image:CGImage
do
{
try image = generator.copyCGImage(at:time, actualTime:nil)
}
catch
{
return nil
}
return UIImage(cgImage: image)
}