Skip to content

Commit

Permalink
Only save images from screencast: Images can be combined later
Browse files Browse the repository at this point in the history
  • Loading branch information
singhprd committed Jan 3, 2025
1 parent e94d418 commit b421183
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 25 deletions.
3 changes: 3 additions & 0 deletions lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class Page
# @return [Downloads]
attr_reader :downloads

# Control Screencasting
#
# @return [nil]
attr_reader :screencaster

def initialize(client, context_id:, target_id:, proxy: nil)
Expand Down
1 change: 0 additions & 1 deletion lib/ferrum/page/screencast.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true
require "ferrum/screencaster"


module Ferrum
class Page
module Screencast
Expand Down
29 changes: 5 additions & 24 deletions lib/ferrum/screencaster.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,26 @@
# frozen_string_literal: true

require 'open3'
require 'stringio'
#TODO these ^ need to be added to gemfile
require 'concurrent-ruby'

module Ferrum
class Screencaster
# attr_reader :message, :default_prompt
include Concurrent::Async

def initialize(page)
@page = page #might be wrong
@stdin = StringIO.new
@stdout = StringIO.new
@stderr = StringIO.new
@wait_thr = nil
@page = page
end

def add_frame(params)
warn 'frame'
@page.command("Page.screencastFrameAck", sessionId: params["sessionId"])
img = params["data"]
img_decoded = Base64.decode64(img)
@stdin.write(img_decoded)

ts = (Time.now.to_f * 1000).to_i
File.binwrite("img_#{ts}.jpeg", Base64.decode64(params["data"]))
end

def start_screencast #(options)
cmd = "ffmpeg -y -f image2pipe -i - -c:v libx264 -preset slow -crf 22 -r 1 -an -f mp4 -movflags +faststart output_video2.mp4"
@stdin, @stdout, @stderr, @wait_thr = Open3.popen3(cmd)
@page.command("Page.startScreencast", format: "jpeg") #, **options)
def start_screencast
@page.command("Page.startScreencast", format: "jpeg")
end

def stop_screencast
warn 'stopped'
@page.command("Page.stopScreencast")
@stdin.close
@stdout.close
@stderr.close
@wait_thr.join
end
end
end

0 comments on commit b421183

Please sign in to comment.