How to get frame on millisecond of video

791 Views Asked by At

I am trying to get frames of milliseconds of video. but when I got frames of 10 seconds video with 24 fps then I got same 24 frame in every seconds. but I actually need to get frame in milliseconds not 24 same frames.

I have got got 24 frames per seconds but that 24 frames are same as first. I need 24 different frames. How can I pick frame in millisecond?

Below is my code for review:

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)
}
0

There are 0 best solutions below