module ScreenRecorder::Screenshot

All screenshot related code

Public Instance Methods

screenshot(filename, resolution = nil) click to toggle source

Takes a screenshot in the current context (input) - desktop or current window

# File lib/screen-recorder/screenshot.rb, line 7
def screenshot(filename, resolution = nil)
  ScreenRecorder.logger.debug "Screenshot filename: #{filename}, resolution: #{resolution}"
  cmd = screenshot_cmd(filename: filename, resolution: resolution)
  process = execute_command(cmd)
  exit_code = wait_for_process_exit(process) # 0 (success) or 1 (fail)
  if exit_code&.zero?
    ScreenRecorder.logger.info "Screenshot: #{filename}"
    return filename
  end
  ScreenRecorder.logger.error 'Failed to take a screenshot.'
  nil
end
screenshot_cmd(filename:, resolution: nil) click to toggle source

Parameters to capture a single frame

# File lib/screen-recorder/screenshot.rb, line 23
def screenshot_cmd(filename:, resolution: nil)
  resolution = resolution ? resolution_arg(resolution) : nil
  # -f overwrites existing file
  "#{ffmpeg_bin} -f #{options.capture_device} -i #{options.input} -framerate 1 -frames:v 1 #{resolution}#{filename}"
end

Private Instance Methods

resolution_arg(size) click to toggle source

Returns OS specific video resolution arg for ffmpeg

# File lib/screen-recorder/screenshot.rb, line 34
def resolution_arg(size)
  # macOS likes -s, windows and linux like -video_size
  OS.mac? ? "-s #{size} " : "-video_size #{size} "
end