FFix.Command (ffix v0.1.0)

Copy Markdown View Source

Canonical representation of a full ffmpeg command.

A command stores global options, ordered inputs, an optional filtergraph, and ordered outputs. It is still just data until FFix.to_argv/1, FFix.run/1, or another boundary function serializes it.

Prefer FFix.command/3 for the function-based API. Use this module directly when you want to construct or transform %FFix.Command{} values in smaller steps.

Examples

input = FFix.Command.input("input.mp4", ss: "00:00:03", stream_loop: -1)
video = input[:video]

command =
  FFix.Command.new(
    global: [y: true],
    inputs: [input],
    outputs: [FFix.Command.output("out.mp4", video, vcodec: :copy)]
  )

FFix.to_argv(command)
#=> [
#=>   "ffmpeg",
#=>   "-y",
#=>   "-ss",
#=>   "00:00:03",
#=>   "-stream_loop",
#=>   "-1",
#=>   "-i",
#=>   "input.mp4",
#=>   "-map",
#=>   "0:v",
#=>   "-vcodec",
#=>   "copy",
#=>   "out.mp4"
#=> ]

Output option keys are rendered as ffmpeg CLI option names. For options with stream specifiers, use an atom or string key that already contains the specifier, for example :"c:v" or "metadata:s:a:0".

Summary

Construction

Appends global ffmpeg options to a command.

Returns an empty command.

Builds a command from already-normalized command data.

Inputs

Builds an input declaration.

Builds an input with options, or appends an input to a command.

Appends an input declaration with options to a command.

Filtergraph

Sets the filtergraph for a command.

Outputs

Builds an output declaration from explicit sources.

Builds an output with options, or appends an output to a command.

Appends an output declaration with options to a command.

Validation

Validates command structure.

Serialization

Serializes a command to ffmpeg argv.

Serializes a command as a shell-escaped string for logs and debugging.

Construction

global(command, options)

@spec global(
  t(),
  keyword()
) :: t()

Appends global ffmpeg options to a command.

Global options are rendered before inputs:

FFix.Command.new()
|> FFix.Command.global(y: true, loglevel: :error)

new()

@spec new() :: t()

Returns an empty command.

This is useful when building a command step by step with global/2, input/3, graph/2, and output/4.

new(options)

@spec new(keyword()) :: t()

Builds a command from already-normalized command data.

This lower-level constructor expects ordered input and output structs. For the function callback API, use FFix.command/3.

src = FFix.Command.input("input.mp4")

FFix.Command.new(
  inputs: [src],
  outputs: [FFix.Command.output("out.mp4", src[:video], "c:v": :copy)]
)

Inputs

input(source)

Builds an input declaration.

Input options are rendered before -i:

src = FFix.Command.input("input.mp4", ss: "00:00:03")
src[:video]
src[:audio]

Prefer shaping inputs in FFix.command/3 rather than storing labels on the input itself.

input(command, source)

@spec input(
  FFix.Command.Input.source(),
  keyword()
) :: FFix.Command.Input.t()
@spec input(t(), FFix.Command.Input.source()) :: t()

Builds an input with options, or appends an input to a command.

Called as input(source, options), it returns an %FFix.Command.Input{}:

src = FFix.Command.input("input.mp4", ss: "00:00:03")

Called as input(command, source), it appends an input without options and returns the updated command:

FFix.Command.new()
|> FFix.Command.input("input.mp4")

input(command, source, options)

@spec input(t(), FFix.Command.Input.source(), keyword()) :: t()

Appends an input declaration with options to a command.

FFix.Command.new()
|> FFix.Command.input("input.mp4", ss: "00:00:03")

Filtergraph

graph(command, graph)

@spec graph(t(), FFix.Graph.t()) :: t()

Sets the filtergraph for a command.

Outputs

output(target, sources)

Builds an output declaration from explicit sources.

This is lower-level than FFix.output/2: pass graph exports, graph export names, graph export indexes, or direct input streams explicitly.

src = FFix.Command.input("input.mp4")
FFix.Command.output("copy.mp4", [src[:video], src[:audio]], c: :copy)

Output options are rendered after -map entries and before the target.

output(command, target, sources)

@spec output(FFix.Command.Output.target(), source() | [source()], keyword()) ::
  FFix.Command.Output.t()
@spec output(t(), FFix.Command.Output.target(), source() | [source()]) :: t()

Builds an output with options, or appends an output to a command.

Called as output(target, sources, options), it returns an %FFix.Command.Output{}:

FFix.Command.output("copy.mp4", [src[:video], src[:audio]], c: :copy)

Called as output(command, target, sources), it appends an output without options and returns the updated command.

output(command, target, sources, options)

@spec output(t(), FFix.Command.Output.target(), source() | [source()], keyword()) ::
  t()

Appends an output declaration with options to a command.

FFix.Command.new()
|> FFix.Command.input("input.mp4")
|> FFix.Command.output("copy.mp4", 0, c: :copy)

Validation

validate!(command)

@spec validate!(t()) :: t()

Validates command structure.

This checks declared inputs, graph references, output sources, and filtered graph export mappings. It does not run ffmpeg or inspect media files.

Serialization

to_argv(command)

@spec to_argv(t()) :: [String.t()]

Serializes a command to ffmpeg argv.

The returned list is the canonical boundary for executing a command. Prefer it over shell strings when passing argv to another process.

to_shell_string(command)

@spec to_shell_string(t()) :: String.t()

Serializes a command as a shell-escaped string for logs and debugging.

Types

option()

@type option() :: {atom() | String.t(), term()}

source()

@type source() :: FFix.Graph.Export.t() | FFix.Stream.t() | atom() | non_neg_integer()

t()

@type t() :: %FFix.Command{
  global_options: [option()],
  graph: FFix.Graph.t() | nil,
  inputs: [FFix.Command.Input.t()],
  outputs: [FFix.Command.Output.t()]
}