微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在特定时间从视频获取帧-快速-以编程方式

如何解决在特定时间从视频获取帧-快速-以编程方式

我正在尝试弄清楚如何使用CMTime结构从视频中检索某些帧,我一直在网上搜索,我想已经掌握了它的工作原理,但我不知道在这种特定情况下如何应用它: 这是我要拍摄帧的地方的图形表示:

video Frame locations

这是我为执行此操作而实现的代码

    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 举报,一经查实,本站将立刻删除。