Class: Headless::VideoRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/headless/video/video_recorder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(display, dimensions, options = {}) ⇒ VideoRecorder

Returns a new instance of VideoRecorder.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/headless/video/video_recorder.rb', line 7

def initialize(display, dimensions, options = {})
  CliUtil.ensure_application_exists!('ffmpeg', 'Ffmpeg not found on your system. Install it with sudo apt-get install ffmpeg')

  @display = display
  @dimensions = dimensions[/.+(?=x)/]

  @bin_file_path = options.fetch(:bin_file_path, CliUtil.path_to('ffmpeg'))
  # divine version - tested on:
  # ffmpeg version 2.3.1
  # ffmpeg version 0.10.9-7:0.10.9-1~quantal1
  # ffmpeg 0.8.10-6:0.8.10-0ubuntu0.12.10.1
  @bin_version   = options.fetch(:bin_version, guess_ffmpeg_version!)
  @pid_file_path = options.fetch(:pid_file_path, "/tmp/.headless_ffmpeg_#{@display}.pid")
  @tmp_file_path = options.fetch(:tmp_file_path, "/tmp/.headless_ffmpeg_#{@display}.mov")
  @log_file_path = options.fetch(:log_file_path, '/dev/null')
  @codec = options.fetch(:codec, 'qtrle')
  @frame_rate = options.fetch(:frame_rate, 30).to_i
  @nomouse = options.fetch(:nomouse, false)
  @audio = options.fetch(:audio, false)
end

Instance Attribute Details

#bin_file_pathObject

Returns the value of attribute bin_file_path.



5
6
7
# File 'lib/headless/video/video_recorder.rb', line 5

def bin_file_path
  @bin_file_path
end

#bin_versionObject

Returns the value of attribute bin_version.



5
6
7
# File 'lib/headless/video/video_recorder.rb', line 5

def bin_version
  @bin_version
end

#capture_withObject

Returns the value of attribute capture_with.



5
6
7
# File 'lib/headless/video/video_recorder.rb', line 5

def capture_with
  @capture_with
end

#log_file_pathObject

Returns the value of attribute log_file_path.



5
6
7
# File 'lib/headless/video/video_recorder.rb', line 5

def log_file_path
  @log_file_path
end

#pid_file_pathObject

Returns the value of attribute pid_file_path.



5
6
7
# File 'lib/headless/video/video_recorder.rb', line 5

def pid_file_path
  @pid_file_path
end

#tmp_file_pathObject

Returns the value of attribute tmp_file_path.



5
6
7
# File 'lib/headless/video/video_recorder.rb', line 5

def tmp_file_path
  @tmp_file_path
end

Instance Method Details

#capture_running?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/headless/video/video_recorder.rb', line 32

def capture_running?
  !!CliUtil.read_pid(@pid_file_path)
end

#guess_ffmpeg_version!Object



28
29
30
# File 'lib/headless/video/video_recorder.rb', line 28

def guess_ffmpeg_version!
  (Gem::Version.new(`#{@bin_file_path} -version`[/(?:ffmpeg )(?:version )?((?:(\d+)\.)?(?:(\d+)\.)?(\*|\d+))/, 1])) unless @bin_file_path.empty? rescue Gem::Version.new('0')
end

#pauseObject



61
62
63
# File 'lib/headless/video/video_recorder.rb', line 61

def pause
  CliUtil.signal_process(@pid_file_path, 'STOP')
end

#resumeObject



65
66
67
# File 'lib/headless/video/video_recorder.rb', line 65

def resume
  CliUtil.signal_process(@pid_file_path, 'CONT')
end

#start_captureObject



52
53
54
55
56
57
58
59
# File 'lib/headless/video/video_recorder.rb', line 52

def start_capture
  CliUtil.fork_process("#{capture_with} #{@tmp_file_path}", @pid_file_path, @log_file_path)
  at_exit do
    exit_status = $!.status if $!.is_a?(SystemExit)
    stop_and_discard
    exit exit_status if exit_status
  end
end

#stop_and_discardObject



81
82
83
84
85
86
87
88
# File 'lib/headless/video/video_recorder.rb', line 81

def stop_and_discard
  CliUtil.kill_process(@pid_file_path, :wait => true)
  begin
    FileUtils.rm(@tmp_file_path)
  rescue Errno::ENOENT
    # that's ok if the file doesn't exist
  end
end

#stop_and_save(path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/headless/video/video_recorder.rb', line 69

def stop_and_save(path)
  CliUtil.kill_process(@pid_file_path, :wait => true, :sig => 'INT')
  if File.exists? @tmp_file_path
    begin
      FileUtils.mv(@tmp_file_path, path)
        true
    rescue Errno::EINVAL
      false
    end
  end
end