Class: VNCRec::Writers::FFmpeg

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

Overview

FFmpeg writer. Pipes video to FFmpeg instance exactly fps times per second. Audio addition is also supported (`:ffmpeg_ia`- and `:ffmpeg_out_opts strings`)

Instance Method Summary collapse

Constructor Details

#initialize(filename, opts = {}) ⇒ FFmpeg

Note:

Choose -acodec option in :ffmpeg_out_opts accordingly.

Returns a new instance of FFmpeg

Parameters:

  • filename (String)

    a name for video file. Should contain extension i.e. .mp4 of .flv.

  • opts (Hash) (defaults to: {})

    options:

    • fps

    • pix_fmt (see :colormode)

    • geometry

    • ffmpeg_iv_opts

    • ffmpeg_ia_opts

    • ffmpeg_out_opts

    See Recorder#initialize for descriptions


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vncrec/writers.rb', line 48

def initialize(filename, opts = {})
  @filename = filename
  @fps = opts[:fps] || 12
  pf = opts.fetch(:pix_fmt) { fail 'Undefined pixel format' }
  @pix_fmt = get_pix_fmt pf
  @size = opts.fetch(:geometry) { fail 'Undefined frame size' }
  @frame_length = frame_length
  @ffmpeg_iv_opts = opts[:ffmpeg_iv_opts]
  @ffmpeg_ia_opts = opts[:ffmpeg_ia_opts]
  @ffmpeg_out_opts = opts[:ffmpeg_out_opts]
  @cmd = "ffmpeg -y -s #{@size} -r #{@fps} -f rawvideo -pix_fmt #{@pix_fmt[:string]} \
  #{@ffmpeg_iv_opts} \
-i pipe:0 \
  #{@ffmpeg_ia_opts} \
  #{@ffmpeg_out_opts} #{@filename} &>/dev/null"
  @data_avail = false
  spawn
end

Instance Method Details

#closeObject


78
79
80
81
82
83
84
85
86
87
# File 'lib/vncrec/writers.rb', line 78

def close
  Process.kill('KILL', @pid)
  Timeout.timeout(5) do
    Process.waitpid(@pid)
  end
rescue Timeout::Error
  raise 'Writer hanged'
rescue Errno::ESRCH, Errno::ECHILD
  raise 'No writer running'
end

#closed?Boolean

Returns:

  • (Boolean)

89
90
91
92
93
94
95
96
97
98
# File 'lib/vncrec/writers.rb', line 89

def closed?
  Timeout.timeout(0.05) do
    Process.waitpid(@pid)
    return true
  end
rescue Timeout::Error
  return false
rescue Errno::ECHILD
  return true
end

#sizeInteger

Returns filesize. If no file created yet 0 is returned.

Returns:

  • (Integer)

    filesize. If no file created yet 0 is returned.


102
103
104
105
106
107
# File 'lib/vncrec/writers.rb', line 102

def size
  s = File.size(@filename)
  return s
rescue Errno::ENOENT
  return 0
end

#write(data) ⇒ Object


67
68
69
70
71
72
73
74
75
76
# File 'lib/vncrec/writers.rb', line 67

def write(data)
  begin
    written = @pipe_to_writer.syswrite(data)
  rescue Errno::EPIPE
    raise 'No writer running'
  end
  fail 'Not enough data is piped to writer' if written % @frame_length != 0
  @pipe_to_writer.flush
  @data_avail = true
end