Class: AnimateIt::VideoRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/animate_it/video_renderer.rb

Defined Under Namespace

Classes: CancelledError

Constant Summary collapse

DEFAULT_PLAYWRIGHT_CLI =
"npx playwright".freeze
EXTENSION_FOR_FORMAT =
{
  mp4: "mp4",
  webm: "webm",
  mov: "mov",
  gif: "gif",
  png_sequence: nil,
  png: "png"
}.freeze
AUDIO_INCAPABLE_FORMATS =
%i[gif png_sequence png].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(composition:, host:, output_path:, frames_dir: nil, playwright_cli: nil, format: nil) ⇒ VideoRenderer

Returns a new instance of VideoRenderer.

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/animate_it/video_renderer.rb', line 35

def initialize(composition:, host:, output_path:, frames_dir: nil, playwright_cli: nil, format: nil)
  @composition = composition
  @output_format = format || composition.output_format
  @host = host.delete_suffix("/")
  @output_path = Pathname(output_path)
  @frames_dir = Pathname(frames_dir || Rails.root.join("tmp/animate_it/#{composition.id}"))
  @playwright_cli = playwright_cli || ENV.fetch("PLAYWRIGHT_CLI_EXECUTABLE_PATH", DEFAULT_PLAYWRIGHT_CLI)

  return unless audio_segments.any? && AUDIO_INCAPABLE_FORMATS.include?(@output_format)

  raise Error,
        "Composition #{composition.id} declares audio but format=#{@output_format} doesn't support audio. Use :webm/:mp4/:mov."
end

Instance Attribute Details

#compositionObject (readonly)

Returns the value of attribute composition.



33
34
35
# File 'lib/animate_it/video_renderer.rb', line 33

def composition
  @composition
end

#frames_dirObject (readonly)

Returns the value of attribute frames_dir.



33
34
35
# File 'lib/animate_it/video_renderer.rb', line 33

def frames_dir
  @frames_dir
end

#hostObject (readonly)

Returns the value of attribute host.



33
34
35
# File 'lib/animate_it/video_renderer.rb', line 33

def host
  @host
end

#output_formatObject (readonly)

Returns the value of attribute output_format.



33
34
35
# File 'lib/animate_it/video_renderer.rb', line 33

def output_format
  @output_format
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



33
34
35
# File 'lib/animate_it/video_renderer.rb', line 33

def output_path
  @output_path
end

#playwright_cliObject (readonly)

Returns the value of attribute playwright_cli.



33
34
35
# File 'lib/animate_it/video_renderer.rb', line 33

def playwright_cli
  @playwright_cli
end

Class Method Details

.audio_baseObject

Lazy: Rails.root is nil at gem-load time (Bundler.require runs before Rails::Application is fully initialized).



18
19
20
# File 'lib/animate_it/video_renderer.rb', line 18

def self.audio_base
  @audio_base ||= Rails.root.join("app/audio").freeze
end

Instance Method Details

#render(frame_range: nil, every_nth_frame: 1, props: {}, on_progress: nil, cancel_check: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/animate_it/video_renderer.rb', line 51

def render(frame_range: nil, every_nth_frame: 1, props: {}, on_progress: nil, cancel_check: nil)
  FileUtils.mkdir_p(frames_dir)
  FileUtils.mkdir_p(output_path.dirname)

  frame_list = frames(frame_range:, every_nth_frame:)
  capture_status = capture_frames(frame_list, props:, on_progress:, cancel_check:)

  if capture_status == :cancelled || cancel_check&.call
    frame_count = contiguous_frame_count
    encode_video(frame_count:) if frame_count.positive?
    raise CancelledError, "Render cancelled"
  end

  encode_video
  output_path
end