Advertisement
Kuzminov

Untitled

Jun 14th, 2020
1,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.73 KB | None | 0 0
  1. require 'fileutils'
  2. require 'pry'
  3. require 'sys/proctable'
  4.  
  5. class ScreenRecord
  6.   SCREEN_NUMBER = Time.now.strftime('%m%d%H%M%S').to_i.freeze
  7.  
  8.   include Sys
  9.   attr_accessor :buffersize, :grab, :crf, :extension,
  10.                 :preset, :tune, :video_codec, :video_size
  11.  
  12.   def initialize
  13.     @grab         = SCREEN_NUMBER
  14.     @crf          = 25
  15.     @extension    = 'mkv'
  16.     @logging      = '-loglevel 0'
  17.     @preset       = 'ultrafast'
  18.     @stats        = '-nostats'
  19.     @tune         = 'zerolatency'
  20.     @video_codec  = 'libx264'
  21.     @video_size   = '1920x1080'
  22.     @xvfb_command = "Xvfb :#{SCREEN_NUMBER} -screen 0 1920x1080x24"
  23.     @xvfb_thread  = Thread.start { `#{@xvfb_command}` }
  24.   end
  25.  
  26.   def find_pid_from_command(command)
  27.     process = proc { ProcTable.ps(ppid: Process.pid).detect { |cmd| cmd.cmdline == command } }
  28.  
  29.     WaitUtil.wait_for_condition(
  30.       'PID is defined',
  31.       timeout_sec: 3,
  32.       delay_sec: 3
  33.     ) { process.call&.any? }
  34.     process.call.pid
  35.   end
  36.  
  37.   def start_record(file_name)
  38.     puts "Starting video recording and create file in #{file_name}"
  39.     video_file_path = "#{file_name}.#{@extension}"
  40.     command_video_run =
  41.       "ffmpeg -y #{@stats} #{@logging} -f x11grab -s #{@video_size} -i :#{@grab} "\
  42.       "-c:v #{@video_codec} -preset #{@preset} -tune #{@tune} -crf #{@crf} #{video_file_path}"
  43.  
  44.     @video_run_thread = Thread.start { Helpers.system_execute(command_video_run) }
  45.     @video_run_pid = find_pid_from_command(command_video_run)
  46.     @xvfb_pid = find_pid_from_command(@xvfb_command)
  47.   end
  48.  
  49.   def stop_record
  50.     puts 'Stop video recording'
  51.     [@video_run_thread, @xvfb_thread].each(&:kill)
  52.     [@video_run_pid, @xvfb_pid].each { |pid| Process.kill('KILL', pid) }
  53.   end
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement