问题描述
我正在尝试弄清楚如何使用CMTime
结构从视频中检索某些帧,我一直在网上搜索,我想已经掌握了它的工作原理,但我不知道在这种特定情况下如何应用它:
这是我要拍摄帧的地方的图形表示:
这是我为执行此操作而实现的代码:
fileprivate func createImageFramesFromVideo(urlVideoPassed: URL,completion: @escaping ([UIImage]) -> Void) {
let finalAsset = AVAsset(url: urlVideoPassed)
let imageGenerator = AVAssetImageGenerator(asset: finalAsset)
let videoDuration = asset!.duration
//getting image snapshots more or less in half the video
let time1 = CMTime(value: videoDuration.value/2 - videoDuration.value/10,timescale: videoDuration.timescale)
let time2 = CMTime(value: videoDuration.value/2 - videoDuration.value/11,timescale: videoDuration.timescale)
let time3 = CMTime(value: videoDuration.value/2 - videoDuration.value/12,timescale: videoDuration.timescale)
let time4 = CMTime(value: videoDuration.value/2,timescale: videoDuration.timescale)
let time5 = CMTime(value: videoDuration.value/2 + videoDuration.value/12,timescale: videoDuration.timescale)
let time6 = CMTime(value: videoDuration.value/2 + videoDuration.value/11,timescale: videoDuration.timescale)
let time7 = CMTime(value: videoDuration.value/2 + videoDuration.value/10,timescale: videoDuration.timescale)
let times = [NSValue(time: time1),NSValue(time: time2),NSValue(time: time3),NSValue(time: time4),NSValue(time: time5),NSValue(time: time6),NSValue(time: time7)]
var imageArray : [UIImage] = []
let dispatchGroup = DispatchGroup()
times.forEach { _ in
dispatchGroup.enter()
}
imageGenerator.generateCGImagesAsynchronously(forTimes: times) { (time,image,time1,result,err) in
if let err = err {
print("there's an error in retrieving the images",err)
}
guard let imageTopass = image else {
return
}
let theImage = UIImage(cgImage: imageTopass)
imageArray.append(theImage)
dispatchGroup.leave()
}
//when all images are retrieved,passes them to completion block
dispatchGroup.notify(queue: .main) {
completion(imageArray)
}
}
我实施的CMTime
对我来说很有意义,但实际上并没有按照我链接的方案工作。实际上,我只检索7个帧,其中4个是相同的帧,而3个是另一个相同的帧。
我想知道我在做错什么以及如何解决这个问题。谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)