Class: MPlayer::Screenshoter

Inherits:
Object
  • Object
show all
Defined in:
lib/mplayer_screenshot.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, mplayer_cmd) ⇒ Screenshoter

Returns a new instance of Screenshoter.



13
14
15
16
# File 'lib/mplayer_screenshot.rb', line 13

def initialize(file, mplayer_cmd)
  @file = file
  @mplayer_cmd = mplayer_cmd
end

Instance Method Details

#cleanObject

Delete all png generated files excepted the wanted one which will be moved to where you specified. Usually, there should be only one file, “0000001.png”, but if something goes wrong, many files could be created, eating your freespace with tons of pngs, let’s avoid this.



43
44
45
46
47
48
49
50
51
# File 'lib/mplayer_screenshot.rb', line 43

def clean
  frames = Dir.glob('[0-9]*.png')
  frames.each do |file|
    File.delete(file) unless file == frames[-1]
  end
  
  File.copy frames[-1], @opts[:filename]
  File.delete frames[-1]
end

#mplayer(opts = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/mplayer_screenshot.rb', line 53

def mplayer(opts={})
  log = ""
  
  IO.popen("#{@mplayer_cmd} #{serialize_arguments(opts)} #{@file}") do |out|
    out.each_line { |line| log << line }
  end
  
  log
end

#normalize_pathObject



34
35
36
# File 'lib/mplayer_screenshot.rb', line 34

def normalize_path
  @opts[:filename] = File.expand_path @opts[:filename]
end

#serialize_arguments(opts = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/mplayer_screenshot.rb', line 63

def serialize_arguments(opts={})
  str = ""
  
  opts.each do |k,v|
    str << "-#{k} #{v} "
  end
  
  str
end

#take(opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mplayer_screenshot.rb', line 18

def take(opts={})
  raise "Missing :at value, aborting." unless opts[:at]
  raise "Missing :filename value, aborting." unless opts[:filename]
  
  @opts = opts.dup
  normalize_path
  Dir.chdir '/tmp' do 
    mplayer :frames  => 1, 
            :vo      => 'png', 
            :nosound => "",
            :ss      => opts[:at]
    clean
  end
  
end