使用Swift从视频中抓取帧

前端之家收集整理的这篇文章主要介绍了使用Swift从视频中抓取帧前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在特定时间从视频中抓取帧.我正在调用我的抓取框架函数,其时间指定为秒为Float64.
问题是它没有抓住当前帧.它似乎忽略了小数.如果我用例如1.22和1.70调用函数,它将返回相同的帧.对于Swift来说,我是新手,所以我猜我没有把CMTime对象搞定.所以有人能看出这有什么问题吗?
func generateThumnail(url : NSURL,fromTime:Float64) -> UIImage {
    var asset :AVAsset = AVAsset.assetWithURL(url) as! AVAsset
    var assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
    assetImgGenerate.appliesPreferredTrackTransform = true
    var error       : NSError? = nil
    var time        : CMTime = CMTimeMakeWithSeconds(fromTime,600)        
    var img         : CGImageRef = assetImgGenerate.copyCGImageAtTime(time,actualTime: nil,error: &error)
    var frameImg    : UIImage = UIImage(CGImage: img)!
    return frameImg
}

var grabTime = 1.22
img = generateThumnail(urlVideo,fromTime: Float64(grabTime))
感谢@ eric-d发现这篇文章
iOS Take Multiple Screen Shots

我设法找出添加

assetImgGenerate.requestedTimeToleranceAfter = kCMTimeZero;
    assetImgGenerate.requestedTimeToleranceBefore = kCMTimeZero;

…对我的功能将做的伎​​俩.

我更新的函数如下所示:

func generateThumnail(url : NSURL,fromTime:Float64) -> UIImage {
    var asset :AVAsset = AVAsset.assetWithURL(url) as! AVAsset
    var assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
    assetImgGenerate.appliesPreferredTrackTransform = true
    assetImgGenerate.requestedTimeToleranceAfter = kCMTimeZero;
    assetImgGenerate.requestedTimeToleranceBefore = kCMTimeZero;
    var error       : NSError? = nil
    var time        : CMTime = CMTimeMakeWithSeconds(fromTime,fromTime: Float64(grabTime))
原文链接:https://www.f2er.com/swift/319992.html

猜你在找的Swift相关文章