0

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.

Tuan Ho Si
  • 25
  • 5
  • 1
    What framerate is your input video? – Rob Napier Aug 29 '20 at 19:04
  • I added this line `imagePicker.videoQuality = .typeHigh` and everything work fine. But I still don't how many frame is it? – Tuan Ho Si Aug 29 '20 at 19:13
  • 2
    Instead of making the image picker controller do the work of rewriting the video, why don't you return to the original PHAsset and get its video data directly? – matt Aug 29 '20 at 19:14
  • I still don't have knowledge about PHAsset. In my case, I want to get 10 frames per second from camera. I tried to record video and get frames from that video. May be it isn't a good solution. Do you have any idea? – Tuan Ho Si Aug 29 '20 at 19:20
  • 1
    So you are using the image picker controller to function as the camera? You didn't say that in your question. Again, a better option would be to use AVFoundation where you have more control over what the camera does. See for example https://stackoverflow.com/questions/14039251/ios-avcapturesession-how-to-get-set-the-number-of-frames-per-second-recorded – matt Aug 29 '20 at 19:30

0 Answers0