Skip to content

Instantly share code, notes, and snippets.

@seanlilmateus
Created December 3, 2011 19:24
Show Gist options
  • Save seanlilmateus/1427886 to your computer and use it in GitHub Desktop.
Save seanlilmateus/1427886 to your computer and use it in GitHub Desktop.
Video Recording using Macruby & AVFoundation Framework;
#!/usr/local/bin/macruby
framework 'Cocoa'
framework 'AVFoundation'
# because we love blocks, we make NSTimer blocks :-)
class NSTimer
def self.scheduledTimerWithTimeInterval interval, repeats: repeat_flag, block: block
self.scheduledTimerWithTimeInterval interval,
target: self,
selector: 'executeBlockFromTimer:',
userInfo: block,
repeats: repeat_flag
end
def self.timerWithTimeInterval interval, repeats: repeat_flag, block: block
self.timerWithTimeInterval interval,
target: self,
selector: 'executeBlockFromTimer:',
userInfo: block,
repeats: repeat_flag
end
def self.executeBlockFromTimer aTimer
blck = aTimer.userInfo
time = aTimer.timeInterval
blck[time] if blck
end
end
class VideoRecordDelegate
attr_accessor :window
def initialize
self.conformsToProtocol(Protocol.protocolWithName('NSApplicationDelegate'))
end
def applicationDidFinishLaunching aNotification
@window.delegate = self
captureSession = AVCaptureSession.alloc.init
# looking for Video device, if found add into the session
videoDevice = AVCaptureDevice.defaultDeviceWithMediaType AVMediaTypeVideo
if videoDevice
NSLog("got videoDevice")
videoInput = AVCaptureDeviceInput.deviceInputWithDevice videoDevice, error:nil
captureSession.addInput videoInput if videoInput
end
# looking for Audio device, if found add into the session
audioDevice = AVCaptureDevice.defaultDeviceWithMediaType AVMediaTypeAudio
if audioDevice
NSLog("got AudioDevice")
audioInput = AVCaptureDeviceInput.deviceInputWithDevice audioDevice, error:nil
captureSession.addInput audioInput if audioInput
end
# set Preview layer
preview_layer = AVCaptureVideoPreviewLayer.layerWithSession captureSession
preview_layer.sublayerTransform = CATransform3DMakeScale(-1.0, 1.0, 0.0)
preview_layer.frame = window.contentView.bounds
preview_layer.videoGravity = AVLayerVideoGravityResizeAspectFill
captureMoviePath = "#{NSHomeDirectory()}/Desktop/macruby.mp4"
captureMovieURL = NSURL.alloc.initFileURLWithPath captureMoviePath
NSLog("recording to #{captureMovieURL.absoluteString}")
# initiate a capture movie URL
captureMovieOutput = AVCaptureMovieFileOutput.alloc.init
captureSession.addOutput captureMovieOutput
captureSession.startRunning
# delete macruby.mp4 if it exists
if NSFileManager.defaultManager.fileExistsAtPath captureMoviePath
NSFileManager.defaultManager.removeItemAtPath captureMoviePath, error:nil
end
# note: must have a delegate
captureMovieOutput.startRecordingToOutputFileURL captureMovieURL, recordingDelegate:self
window.contentView.setWantsLayer true
window.contentView.layer.addSublayer preview_layer
window.center
captureSession.startRunning
# we want to stop after 20 seconds
stop_recording_timer = NSTimer.timerWithTimeInterval 20.0, repeats: false, block: -> time do
# we stop recording
captureMovieOutput.stopRecording
# stop the capture Session screen get off
captureSession.stopRunning
end
NSRunLoop.currentRunLoop.addTimer stop_recording_timer, forMode:NSDefaultRunLoopMode #NSEventTrackingRunLoopMode
end
# capture Delegates
def captureOutput captureOutput, didStartRecordingToOutputFileAtURL:fileURL, fromConnections:connections
NSLog("Started recording to #{fileURL.absoluteString}")
end
def captureOutput captureOutput, didFinishRecordingToOutputFileAtURL:outputFileURL, fromConnections:connections, error:error
if error
NSLog("Failed to record: #{error.localizedDescription}")
else
NSLog("Finished recording to #{outputFileURL.absoluteString}")
end
end
def windowWillClose(sender); exit(1); end
end
# Create the Application
application = NSApplication.sharedApplication
application.delegate = VideoRecordDelegate.new
# create the Application Window
frame = [0.0, 0.0, 500, 300]
window = NSWindow.alloc.initWithContentRect frame,
styleMask: NSTitledWindowMask | NSClosableWindowMask,
backing: NSBackingStoreBuffered,
defer: false
window.display
window.makeKeyAndOrderFront(nil)
application.delegate.window = window
application.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment