Capture 60fps in iPhone app

10,848

Solution 1

After some tinkering, this answer has split into two parts:

How to capture frames at 60fps

The AVCaptureSessionPreset1280x720 on the iPhone4s/5.1, with frame durations set to:

connection.videoMinFrameDuration = CMTimeMake(1, 60);*
connection.videoMaxFrameDuration = CMTimeMake(1, 60);

gives you a stable, super smooth capture.

How to capture frames into a file @60fps
Capturing frames is all very well, but presumably you want to keep them.
As Brad noted in the comments, writing those frames to a file is another story. Sadly, no matter what configurations I tried, encoding the frames via an AVAssetWriter caused the capture rate to drop to the observed ~37fps and no amount of fiddling with alwaysDiscardsLateVideoFrames could change it. However, in this approach every single frame is copied from AVFoundation to your app, and then back again, which is quite pointless and very wearing for the bus. Luckily, AVFoundation has a class that removes this round trip: AVCaptureMovieFileOutput.

If you let AVFoundation do the writing for you then the iPhone4S can capture and encode frames + audio to a .mov file at 60fps* without breaking a sweat (~25% CPU).

While 60fps video capture is great feature, I can't help but feel a little disappointed as AVCaptureMovieFileOutput rules out a lot of fun things (e.g. realtime effects with GL shaders, recording start/stop without frame loss).

cake/eat it

*59 if you're still running iOS 5.0.1

Solution 2

I haven't try this before, maybe these are related:

Share:
10,848
Michel
Author by

Michel

Updated on June 18, 2022

Comments

  • Michel
    Michel about 2 years

    I am working on a project where we will be using iPhones as cameras for capturing a scene. When recording we need to record @60fps and not 30fps (as natively supported). So I am working on an app to do this as the iPhone 4S hardware supports 720p@60fps (if you jailbreak your phone you can achieve this).

    Does anybody know how to do this in Objective-C on iOS? Today I saw an app out there (slopro) that can record 60fps on non jailbroken phones. Any advice or tips is much appreciated.

  • Michel
    Michel about 12 years
    Alright, I will take a look at it. Does anybody know what the max supported values for these properties are?
  • Hailei
    Hailei about 12 years
    I just noticed that I gave some incorrect links (for Mac OS) in my answer. Sorry and I have removed the wrong ones.
  • Odrakir
    Odrakir about 12 years
    Hi, anyone had any luck with this?
  • Daniel
    Daniel about 12 years
    I have used these apis to set frame rates, not sure how high it goes, but its what you are looking for
  • Brad Larson
    Brad Larson about 12 years
    I can confirm that forcing the framerate to 60 FPS (you can indeed use 60 instead of 59 in the above code) works on the iPhone 4S, although not the new (Retina) iPad, but it only works for a 720p preset, not for 640x480 or 1080p. I was able to capture and record a movie at 59.87 FPS (dropped a couple of frames at the beginning), although I had to drop the audio and downsample the video to 640x480 to allow the AVAssetWriter to keep up in my implementation. It recorded at 37 FPS with audio recording and full 720p frames.
  • bluevoid
    bluevoid about 12 years
    On iOS 5.0.1 59fps seems to be the only rate that works for me. I'll update my answer after some more tests.
  • Brad Larson
    Brad Larson about 12 years
    My tests were on 5.1, and CMTimeMake(1, 60) delivered as advertised there. Given that this isn't exactly documented, there very well could be differences between the point updates.
  • bluevoid
    bluevoid about 12 years
    I think I've figured it out, but I feel cheated.
  • Michel
    Michel about 12 years
    I just did some tests today on an iPhone 4S with the input as: AVCaptureDeviceInput and output as: AVCaptureMovieFileOutput. Even when setting: connection.videoMinFrameDuration = CMTimeMake(1, 60); connection.videoMaxFrameDuration = CMTimeMake(1, 60); It does not seem to want to record more than 30fps (with AVCaptureSessionPreset1280x720).
  • Michel
    Michel about 12 years
    Ok, seems like I can only do 59 fps. Works fine for me. Even with sound on an iPhone 4S iOS 5.0.1.
  • Idan
    Idan almost 11 years
    My device respond that the max is 30 so setting it to (1, 60) makes an error. I'm using iPhone 5 running iOS 7. doing this settings under AVCaptureDeviceInput (can't do that under connection on iOS 7). Any help?