Skip to content

Instantly share code, notes, and snippets.

@robmiller
Last active August 29, 2015 13:57
Show Gist options
  • Save robmiller/9814585 to your computer and use it in GitHub Desktop.
Save robmiller/9814585 to your computer and use it in GitHub Desktop.
Take a webcam shot and save it in the pictures directory. Depends on tenderlove's av_capture: `gem install av_capture`
#!/usr/bin/env ruby
require 'av_capture'
require 'pathname'
session = AVCapture::Session.new
dev = AVCapture.devices.find(&:video?)
dir = Pathname(File.expand_path("~/Pictures/av"))
Dir.mkdir(dir) unless dir.exist?
# If we've got an argument, use it as a filename suffix
suffix = "_#{ARGV[0]}" || ""
filename = dir + "#{Time.now.strftime('%Y-%m-%d-%H%M%S')}#{suffix}.jpg"
session.run_with(dev) do |connection|
# The camera -- or my MBP's iSight, anyway -- seems to need half a second
# or so to focus and set the correct exposure
sleep 0.5
File.open(filename, "w") { |f| f.write connection.capture }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment