Skip to content

Instantly share code, notes, and snippets.

@hirohitokato
Created December 17, 2013 05:52
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirohitokato/8000638 to your computer and use it in GitHub Desktop.
Save hirohitokato/8000638 to your computer and use it in GitHub Desktop.
Video capturing with the highest fps.
@import AVFoundation;
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic)AVCaptureSession *session;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
BOOL result = [self setupCaptureSessionWithHighestFps];
if (result) {
[self.session startRunning];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)setupCaptureSessionWithHighestFps
{
// キャプチャセッションを作成
self.session = [[AVCaptureSession alloc] init];
// セッションのプリセットを「インプット設定を優先」に変更
self.session.sessionPreset = AVCaptureSessionPresetInputPriority;
// 背面カメラを検索
AVCaptureDevice *targetDevice = nil;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if (device.position == AVCaptureDevicePositionBack) {
targetDevice = device;
break;
}
}
if (!targetDevice) return NO;
// カメラのフォーマット一覧を取得
NSArray *formats = targetDevice.formats;
// カメラのフォーマット一覧から、最高fpsかつ最大サイズのフォーマットを検索
// (420f,420vにはこだわらない)
Float64 maxFrameRate = .0f;
int32_t maxWidth = 0;
AVCaptureDeviceFormat *targetFormat = nil;
for (AVCaptureDeviceFormat *format in formats) {
// フォーマットのFPSを取得
AVFrameRateRange *frameRateRange = format.videoSupportedFrameRateRanges[0];
Float64 frameRate = frameRateRange.maxFrameRate; // フレームレート
// フォーマットのフレームサイズ(幅)を取得
CMFormatDescriptionRef desc = format.formatDescription;
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(desc);
int32_t width = dimensions.width; // フレームサイズ(幅)
// フレームレートとサイズの両方が大きい場合はフォーマットを保持
if (frameRate >= maxFrameRate && width >= maxWidth) {
targetFormat = format;
// 条件の更新
maxFrameRate = frameRate;
maxWidth = width;
}
}
if (!targetFormat) return NO;
// 検索したフォーマットをデバイスに設定し、fpsを上限値で指定
if ([targetDevice lockForConfiguration:nil]) {
targetDevice.activeFormat = targetFormat;
targetDevice.activeVideoMaxFrameDuration = CMTimeMake(1, maxFrameRate);
targetDevice.activeVideoMinFrameDuration = CMTimeMake(1, maxFrameRate);
[targetDevice unlockForConfiguration];
}
// インプットをデバイスから作成し、キャプチャセッションに設定
AVCaptureDeviceInput *input =
[AVCaptureDeviceInput deviceInputWithDevice:targetDevice error:nil];
if (![self.session canAddInput:input]) return NO;
[self.session addInput:input];
// アウトプットを作成し、追加
AVCaptureMovieFileOutput *output = [[AVCaptureMovieFileOutput alloc] init];
if (![self.session canAddOutput:output]) return NO;
[self.session addOutput:output];
// プレビューレイヤーをビューコントローラに設定
AVCaptureVideoPreviewLayer *previewLayer =
[[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
previewLayer.frame = self.view.frame;
previewLayer.contentsGravity = kCAGravityResizeAspectFill;
[self.view.layer addSublayer:previewLayer];
return YES;
}
@hirohitokato
Copy link
Author

動作させるにはiOS7が必要です。

【ここでも】あ、iOS7といえば 「上を目指すプログラマーのためのiPhoneアプリ開発テクニック iOS 7編」(URL )もよろしくお願いします!【宣伝】

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment