Capturing iSight from Macbook

Posted in Mac, Programming on Nov 04, 2008

Lately I’ve been writing Objective-C/Cocoa code, and lately I’ve learned how-to capture video from an iSight on a Macbook. It’s actually easy. Here is the code. and I’ve also attached the example application I was writing so you can take a look at it.

[Download Source]
[Download Application]

#import <Cocoa/Cocoa.h>
#import <QTKit/QTKit.h>


@interface MyRecorderController : NSObject {
  IBOutlet QTCaptureView * mCaptureView;
  QTCaptureSession       * mCaptureSession;
  QTCaptureDeviceInput   * mCaptureDeviceInput;
}

- (IBAction)startRecording: (id)sender;
- (IBAction)stopRecording: (id)sender;

@end



@implementation MyRecorderController

- (IBAction)startRecording: (id)sender
{
  // Create a new Capture Session
  mCaptureSession = [[QTCaptureSession alloc] init]; 
  
  //Connect inputs and outputs to the session
  BOOL success = NO;
  NSError *error;
  
  // Find a video device
  QTCaptureDevice *device = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
  if(device) {
    success = [device open:&error];
    if(!success) {
      // Handle Error!
    }
    // Add the video device to the session as device input
    mCaptureDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice:device];
    success = [mCaptureSession addInput:mCaptureDeviceInput error:&error];
    if(!success) {
      // Handle error
    }
    
    // Associate the capture view in the UI with the session
    [mCaptureView setCaptureSession:mCaptureSession];
    
    // Start the capture session runing
    [mCaptureSession startRunning];
    
  } // End if device
}

- (IBAction)stopRecording: (id)sender
{
  if([mCaptureSession isRunning]) {
     [mCaptureSession stopRunning];
     [mCaptureSession release];
     [mCaptureDeviceInput release];
  }
}


@end

Comments are closed.

  • Recent Comments