Skip to content

Commit

Permalink
Background writing frames to disk; put them in a dir; start from 0 to…
Browse files Browse the repository at this point in the history
… more easily use ffmpeg to create videos from them
  • Loading branch information
singhprd committed Jan 4, 2025
1 parent 6206429 commit 7f0e355
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/ferrum/screencaster.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
# frozen_string_literal: true
require 'fileutils'

# Combine the resulting frames together into a video with:
# ffmpeg -y -framerate 30 -i 'frame-%d.jpeg' -c:v libx264 -r 30 -pix_fmt yuv420p try2.mp4
#
module Ferrum
class Screencaster
def initialize(page)
@page = page
@frame_number = 0
@threads = []
@base_dir = ""
end

def add_frame(params)
warn "frame"

@page.command("Page.screencastFrameAck", sessionId: params["sessionId"])

ts = (Time.now.to_f * 1000).to_i
File.binwrite("img_#{ts}.jpeg", Base64.decode64(params["data"]))
t = Thread.new { File.binwrite("#{recordings_dir}/frame-#{@frame_number}.jpeg", Base64.decode64(params["data"])) }
@frame_number += 1
@threads << t
true
end

def recordings_dir
return @recordings_dir if defined? @recordings_dir

session_id = @page.client.session_id
@recordings_dir = FileUtils.mkdir_p("#{@base_dir}/screencast_recordings/#{session_id}/").first
@recordings_dir
end

def start_screencast
def start_screencast(base_dir = "")
@base_dir = base_dir
@page.command("Page.startScreencast", format: "jpeg")
end

def stop_screencast
warn "joining threads"
@threads.each(&:join)
warn "stopped"
@page.command("Page.stopScreencast")
end
Expand Down

0 comments on commit 7f0e355

Please sign in to comment.