Class: Lolcommits::CaptureLinux

Inherits:
Capturer
  • Object
show all
Defined in:
lib/lolcommits/capturer/capture_linux.rb

Constant Summary collapse

MPLAYER_FPS =
25

Instance Attribute Summary

Attributes inherited from Capturer

#animated_duration, #capture_delay, #capture_device, #frames_location, #snapshot_location, #video_location

Instance Method Summary collapse

Methods inherited from Capturer

#initialize

Constructor Details

This class inherits a constructor from Lolcommits::Capturer

Instance Method Details

#captureObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lolcommits/capturer/capture_linux.rb', line 10

def capture
  debug 'LinuxCapturer: making tmp directory'
  tmpdir = Dir.mktmpdir

  # Default delay is 1s
  delay = capture_delay != 0 ? capture_delay : 1

  # There's no way to give a capture delay in mplayer, but a number of frame
  # mplayer's "delay" is actually a number of frames at 25 fps
  # multiply the set value (in seconds) by 25
  frames = delay.to_i * MPLAYER_FPS

  debug 'LinuxCapturer: calling out to mplayer to capture image'
  # mplayer's output is ugly and useless, let's throw it away
  _stdin, stdout, _stderr = Open3.popen3("mplayer -vo jpeg:outdir=#{tmpdir} #{capture_device_string} -frames #{frames} -fps #{MPLAYER_FPS} tv://")
  # looks like we still need to read the output for something to happen
  stdout.read

  debug 'LinuxCapturer: calling out to mplayer to capture image'

  # get last frame from tmpdir (regardless of fps)
  all_frames = Dir.glob("#{tmpdir}/*.jpg").sort_by do |f|
    File.mtime(f)
  end

  if all_frames.empty?
    debug 'LinuxCapturer: failed to capture any image'
  else
    FileUtils.mv(all_frames.last, snapshot_location)
    debug 'LinuxCapturer: cleaning up'
  end

  FileUtils.rm_rf(tmpdir)
end

#capture_device_stringObject



6
7
8
# File 'lib/lolcommits/capturer/capture_linux.rb', line 6

def capture_device_string
  @capture_device.nil? ? nil : "-tv device=\"#{@capture_device}\""
end