Class: FFmpegCore::Compositor

Inherits:
Object
  • Object
show all
Defined in:
lib/ffmpeg_core/compositor.rb

Overview

Execute FFmpeg operations with multiple input files

Examples:

Overlay (picture-in-picture)

FFmpegCore::Compositor.new(
  ["background.mp4", "overlay.mp4"],
  "output.mp4",
  filter_complex: "[0:v][1:v]overlay=10:10[v]",
  maps: ["[v]", "0:a"]
).run

Concatenate

FFmpegCore::Compositor.new(
  ["part1.mp4", "part2.mp4"],
  "output.mp4",
  filter_complex: "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[v][a]",
  maps: ["[v]", "[a]"]
).run

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_paths, output_path, options = {}) ⇒ Compositor

Returns a new instance of Compositor.

Parameters:

  • input_paths (Array<String>)

    Paths or URLs to input files (at least one required)

  • output_path (String)

    Path to output file

  • options (Hash) (defaults to: {})

    Compositing options

Options Hash (options):

  • :video_codec (String)

    Video codec (e.g., “libx264”)

  • :audio_codec (String)

    Audio codec (e.g., “aac”)

  • :video_bitrate (String, Integer)

    Video bitrate (e.g., “1000k” or 1000)

  • :audio_bitrate (String, Integer)

    Audio bitrate (e.g., “128k” or 128)

  • :filter_complex (Array<String>, String)

    FFmpeg filter graph referencing input streams by index (e.g., “[0:v][1:v]overlay=0:0”). Array elements are joined with semicolons.

  • :maps (Array<String>, String)

    Stream maps selecting outputs from the filter graph (e.g., [“[v]”, “0:a”])

  • :duration (Float)

    Total duration in seconds, used for progress reporting

  • :custom (Array<String>)

    Raw FFmpeg flags appended verbatim (e.g., [“-shortest”])

Raises:

  • (ArgumentError)

    if input_paths is empty



40
41
42
43
44
45
46
# File 'lib/ffmpeg_core/compositor.rb', line 40

def initialize(input_paths, output_path, options = {})
  raise ArgumentError, "At least one input path is required" if Array(input_paths).empty?

  @input_paths = Array(input_paths).map(&:to_s)
  @output_path = output_path.to_s
  @options = options
end

Instance Attribute Details

#input_pathsObject (readonly)

Returns the value of attribute input_paths.



24
25
26
# File 'lib/ffmpeg_core/compositor.rb', line 24

def input_paths
  @input_paths
end

#optionsObject (readonly)

Returns the value of attribute options.



24
25
26
# File 'lib/ffmpeg_core/compositor.rb', line 24

def options
  @options
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



24
25
26
# File 'lib/ffmpeg_core/compositor.rb', line 24

def output_path
  @output_path
end

Instance Method Details

#run {|Float| ... } ⇒ String

Run the FFmpeg compositing command

Yields:

  • (Float)

    Progress ratio from 0.0 to 1.0 (requires :duration option)

Returns:

  • (String)

    Path to the output file

Raises:



54
55
56
57
58
59
60
# File 'lib/ffmpeg_core/compositor.rb', line 54

def run(&block)
  validate_inputs!
  ensure_output_directory!

  command = build_command
  execute_command(command, &block)
end