Is there a function to get 10 frames per second of video?

276 views Asked by At

I am trying to get 10 frames per second of video from imagePickerController. I use this function, but images from 0.1s -> 1s are the same, from 1.1 to 2s too. Is there anyway to do? This is my function:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true)
        let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as! URL
        getAllFrames(videoURL)
    }
var frames:[UIImage] = []
private var generator:AVAssetImageGenerator!
func getAllFrames(_ videoUrl: URL) {
        let asset:AVAsset = AVAsset(url: videoUrl)
        let duration:Double = CMTimeGetSeconds(asset.duration)
        self.generator = AVAssetImageGenerator(asset:asset)
        self.generator.appliesPreferredTrackTransform = true
        var i: Double = 0
        repeat {
            self.getFrame(fromTime: i)
            i = i + 0.1
        } while (i < duration)
        self.generator = nil
    } 
private func getFrame(fromTime:Double) {
        let time:CMTime = CMTimeMakeWithSeconds(fromTime, preferredTimescale: 600)
        let image:CGImage
        do {
            try image = self.generator.copyCGImage(at:time, actualTime:nil)
        } catch {
            return
        }
        self.frames.append(UIImage(cgImage:image))
    }

Or do I need to increase video quality? Thanks for help.

0

There are 0 answers