Skip to content

Instantly share code, notes, and snippets.

@jeffkreeftmeijer
Forked from smparkes/gist:545987
Created April 13, 2011 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffkreeftmeijer/917805 to your computer and use it in GitHub Desktop.
Save jeffkreeftmeijer/917805 to your computer and use it in GitHub Desktop.
#!/usr/bin/env macruby
framework 'AppKit'
framework 'QTKit'
framework 'QuartzCore'
framework 'CoreVideo'
load_bridge_support_file 'QTKit-capturefix.bridgesupport'
class AppDelegate
def applicationDidFinishLaunching(notification)
@window = NSWindow.alloc.initWithContentRect([200, 300, 640, 480],
styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask,
backing:NSBackingStoreBuffered,
defer:false)
@window.title = 'MacRuby: The Definitive Guide'
@window.level = 3
@window.delegate = self
@window.display
@window.orderFrontRegardless
@window.makeMainWindow
@window.makeKeyWindow
@session = QTCaptureSession.alloc.init
device = QTCaptureDevice.defaultInputDeviceWithMediaType QTMediaTypeVideo
device.open nil
input = QTCaptureDeviceInput.deviceInputWithDevice device
@session.addInput input, error:nil
output = QTCaptureDecompressedVideoOutput.alloc.init
output.automaticallyDropsLateVideoFrames = true
output.delegate = self
key = KCVPixelBufferPixelFormatTypeKey
value = NSNumber.numberWithUnsignedInt KCVPixelFormatType_32BGRA
pixelBufferAttributes = NSDictionary.dictionaryWithObject value, forKey:key
pixelBufferAttributes = {KCVPixelBufferPixelFormatTypeKey:KCVPixelFormatType_32BGRA}
output.pixelBufferAttributes = pixelBufferAttributes
@session.addOutput output, error:nil
main = CALayer.layer
main.frame = @window.contentView.frame
@window.contentView.layer = main
@window.contentView.wantsLayer = true
capture = QTCaptureLayer.alloc.initWithSession @session
capture.frame = @window.contentView.frame
mirror = CGAffineTransformMakeScale -1, 1
capture.affineTransform = mirror
main.addSublayer capture
@session.startRunning
end
def captureOutput captureOutput,
didOutputVideoFrame:videoFrame,
withSampleBuffer:sampleBuffer,
fromConnection:connection
p captureOutput, videoFrame, sampleBuffer,connection
p CVPixelBufferGetWidth videoFrame
end
end
app = NSApplication.sharedApplication
app.delegate = AppDelegate.new
app.run
<?xml version="1.0"?>
<!DOCTYPE signatures SYSTEM
"file://localhost/System/Library/DTDs/BridgeSupport.dtd">
<signatures version="0.9">
<cftype name='CVBufferRef' type='^{__CVBuffer=}'/>
</signatures>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment