Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'fileutils'
- require 'pry'
- require 'sys/proctable'
- class ScreenRecord
- SCREEN_NUMBER = Time.now.strftime('%m%d%H%M%S').to_i.freeze
- include Sys
- attr_accessor :buffersize, :grab, :crf, :extension,
- :preset, :tune, :video_codec, :video_size
- def initialize
- @grab = SCREEN_NUMBER
- @crf = 25
- @extension = 'mkv'
- @logging = '-loglevel 0'
- @preset = 'ultrafast'
- @stats = '-nostats'
- @tune = 'zerolatency'
- @video_codec = 'libx264'
- @video_size = '1920x1080'
- @xvfb_command = "Xvfb :#{SCREEN_NUMBER} -screen 0 1920x1080x24"
- @xvfb_thread = Thread.start { `#{@xvfb_command}` }
- end
- def find_pid_from_command(command)
- process = proc { ProcTable.ps(ppid: Process.pid).detect { |cmd| cmd.cmdline == command } }
- WaitUtil.wait_for_condition(
- 'PID is defined',
- timeout_sec: 3,
- delay_sec: 3
- ) { process.call&.any? }
- process.call.pid
- end
- def start_record(file_name)
- puts "Starting video recording and create file in #{file_name}"
- video_file_path = "#{file_name}.#{@extension}"
- command_video_run =
- "ffmpeg -y #{@stats} #{@logging} -f x11grab -s #{@video_size} -i :#{@grab} "\
- "-c:v #{@video_codec} -preset #{@preset} -tune #{@tune} -crf #{@crf} #{video_file_path}"
- @video_run_thread = Thread.start { Helpers.system_execute(command_video_run) }
- @video_run_pid = find_pid_from_command(command_video_run)
- @xvfb_pid = find_pid_from_command(@xvfb_command)
- end
- def stop_record
- puts 'Stop video recording'
- [@video_run_thread, @xvfb_thread].each(&:kill)
- [@video_run_pid, @xvfb_pid].each { |pid| Process.kill('KILL', pid) }
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement