Class: VNCRec::Writers::FFmpeg
- Inherits:
-
Object
- Object
- VNCRec::Writers::FFmpeg
- 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
- #close ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(filename, opts = {}) ⇒ FFmpeg
constructor
A new instance of FFmpeg.
-
#size ⇒ Integer
Filesize.
- #write(data) ⇒ Object
Constructor Details
#initialize(filename, opts = {}) ⇒ FFmpeg
Note:
Choose -acodec option in :ffmpeg_out_opts
accordingly.
Returns a new instance of FFmpeg.
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
#close ⇒ Object
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
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 |
#size ⇒ Integer
Returns 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 |