class ScreenRecorder::Options

@since 1.0.0-beta11

Constants

DEFAULT_FPS
DEFAULT_LOG_FILE
DEFAULT_MAC_INPUT_PIX_FMT
DEFAULT_PIX_FMT
YUV420P_SCALING

Attributes

all[R]

Public Class Methods

new(options) click to toggle source
# File lib/screen-recorder/options.rb, line 15
def initialize(options)
  # @todo Consider using OpenStruct
  @all = verify_options options
  advanced[:input] = default_advanced_input.merge(advanced_input)
  advanced[:output] = default_advanced_output.merge(advanced_output)
  advanced[:log] ||= DEFAULT_LOG_FILE

  # Fix for using yuv420p pixel format for output
  # @see https://www.reck.dk/ffmpeg-libx264-height-not-divisible-by-2/
  advanced_output[:vf] = YUV420P_SCALING if advanced_output[:pix_fmt] == 'yuv420p'
end

Public Instance Methods

advanced() click to toggle source

Returns given values that are optional

# File lib/screen-recorder/options.rb, line 51
def advanced
  @all[:advanced] ||= {}
end
capture_device() click to toggle source

Returns capture device in use

# File lib/screen-recorder/options.rb, line 37
def capture_device
  determine_capture_device
end
framerate() click to toggle source

Returns given framerate

# File lib/screen-recorder/options.rb, line 58
def framerate
  ScreenRecorder.logger.warn '#framerate will not be available in the next release. Use #advanced instead.'
  advanced[:output][:framerate]
end
input() click to toggle source

Returns given input file or input

# File lib/screen-recorder/options.rb, line 30
def input
  @all[:input]
end
log() click to toggle source

Returns given log filename

# File lib/screen-recorder/options.rb, line 66
def log
  advanced[:log]
end
output() click to toggle source

Returns given output filepath

# File lib/screen-recorder/options.rb, line 44
def output
  @all[:output]
end
parsed() click to toggle source

Returns a String with all options parsed and ready for the ffmpeg process to use

# File lib/screen-recorder/options.rb, line 74
def parsed
  vals = "-f #{capture_device} "
  vals << parse_advanced(advanced_input)
  vals << "-i #{input} " unless advanced_input[:i] # Input provided by user
  vals << parse_advanced(advanced)
  vals << parse_advanced(advanced_output)
  vals << output
end

Private Instance Methods

advanced_input() click to toggle source
# File lib/screen-recorder/options.rb, line 100
def advanced_input
  advanced[:input] ||= {}
end
advanced_output() click to toggle source
# File lib/screen-recorder/options.rb, line 104
def advanced_output
  advanced[:output] ||= {}
end
default_advanced_input() click to toggle source
# File lib/screen-recorder/options.rb, line 108
def default_advanced_input
  {
    pix_fmt: OS.mac? ? DEFAULT_MAC_INPUT_PIX_FMT : nil
  }
end
default_advanced_output() click to toggle source
# File lib/screen-recorder/options.rb, line 114
def default_advanced_output
  {
    pix_fmt:   DEFAULT_PIX_FMT,
    framerate: advanced[:framerate] || DEFAULT_FPS
  }
end
default_capture_device() click to toggle source

Returns input capture device for current OS.

# File lib/screen-recorder/options.rb, line 159
def default_capture_device
  return 'gdigrab' if OS.windows?

  return 'x11grab' if OS.linux?

  return 'avfoundation' if OS.mac?

  raise 'Your OS is not supported. Feel free to create an Issue on GitHub.'
end
determine_capture_device() click to toggle source

Returns input capture device based on user given value or the current OS.

# File lib/screen-recorder/options.rb, line 146
def determine_capture_device
  # User given capture device or format from advanced configs Hash
  # @see https://www.ffmpeg.org/ffmpeg.html#Main-options
  return advanced_input[:f] if advanced_input[:f]

  return advanced_input[:fmt] if advanced_input[:fmt]

  default_capture_device
end
parse_advanced(opts) click to toggle source

Returns given Hash parsed and ready for ffmpeg to receive.

# File lib/screen-recorder/options.rb, line 131
def parse_advanced(opts)
  # @todo Replace arr with opts.each_with_object([])
  arr = []
  rejects = %i[input output log]
  # Do not parse input/output and log as they're placed separately in #parsed
  opts.reject { |k, _| rejects.include? k }
    .each do |k, v|
    arr.push "-#{k} #{v}" unless v.nil? # Ignore blank params
  end
  "#{arr.join(' ')} "
end
required_options() click to toggle source

Returns Array of required options as Symbols

# File lib/screen-recorder/options.rb, line 124
def required_options
  %i[input output]
end
verify_options(options) click to toggle source

Verifies the required options are provided and returns the given options Hash. Raises ArgumentError if all required options are not present in the given Hash.

# File lib/screen-recorder/options.rb, line 90
def verify_options(options)
  TypeChecker.check options, Hash
  TypeChecker.check options[:advanced], Hash if options[:advanced]
  missing_options = required_options.select { |req| options[req].nil? }
  err = "Required options are missing: #{missing_options}"
  raise(ArgumentError, err) unless missing_options.empty?

  options
end