akash-akya

akash-akya

Hi everyone,

I made a small Elixir library called FFix.

FFix helps you build ffmpeg commands and filter graphs using Elixir data and functions, instead of manually joining long -filter_complex, -map, and option strings.

It does not try to hide ffmpeg. Filter names, options, stream mappings, codecs, and muxer options are still ffmpeg-shaped. FFix just gives you a nicer way to compose and inspect them from Elixir.

Examples

Examples assume import FFix and import FFix.Filter.

cmd =
  command(
    "input.mp4",
    fn src ->
      src[:video]
      |> crop(w: 720, h: 720)
    end,
    fn cropped, src ->
      output("square.mp4", video: cropped, audio: src[:audio])
    end
  )

FFix.to_argv(cmd)

This builds a command like:

ffmpeg -i input.mp4 \
  -filter_complex '[0:v]crop=w=720:h=720[out0]' \
  -map '[out0]' \
  -map 0:a \
  square.mp4

A more useful example is when the filter graph starts branching.

This creates a vertical video from a normal landscape video. It splits the video into two branches: one branch becomes a blurred background, and the other is scaled and placed on top.

cmd =
  command(
    "input.mp4",
    fn src ->
      [bg_src, fg_src] = split(src[:video], outputs: 2)

      bg =
        bg_src
        |> scale(w: 1080, h: 1920, force_original_aspect_ratio: :increase)
        |> crop(w: 1080, h: 1920)
        |> gblur(sigma: 30)

      fg =
        fg_src
        |> scale(w: 1080, h: -1)

      bg
      |> overlay(fg, x: expr("(W-w)/2"), y: expr("(H-h)/2"))
    end,
    fn video, src ->
      output("vertical.mp4",
        video: video,
        audio: src[:audio],
        "c:v": :libx264,
        "c:a": :copy,
        shortest: true
      )
    end
  )

The same thing in raw ffmpeg form is roughly:

ffmpeg -i input.mp4 \
  -filter_complex "\
[0:v]split=outputs=2[bg_src][fg_src];\
[bg_src]scale=w=1080:h=1920:force_original_aspect_ratio=increase,crop=w=1080:h=1920,gblur=sigma=30[bg];\
[fg_src]scale=w=1080:h=-1[fg];\
[bg][fg]overlay=x=(W-w)/2:y=(H-h)/2[outv]" \
  -map "[outv]" \
  -map 0:a \
  -c:v libx264 \
  -c:a copy \
  -shortest \
  vertical.mp4

FFix is mostly useful when these commands are not static strings, and you need to build them from user input, stored config, templates, or application logic.


It also supports streaming. For example, this reads WAV data through stdin and streams MP3 data back through stdout:

cmd =
  command(
    input(:stdin, f: :wav),
    fn src ->
      src[:audio]
    end,
    fn audio ->
      output(:stdout,
        audio: audio,
        f: :mp3,
        "c:a": :libmp3lame
      )
    end
  )

cmd
|> FFix.stream!(stdin: File.stream!("input.wav", [], 8192))
|> Stream.filter(fn
  {:stdout, _chunk} -> true
  _event -> false
end)
|> Stream.map(fn {:stdout, chunk} -> chunk end)
|> Enum.into(File.stream!("output.mp3", [:write, :binary]))

And one command can write multiple outputs. For example, generate MP4 and WebM versions from the same input:

cmd =
  command(
    "input.mov",
    fn src ->
      [src[:video], src[:audio]]
    end,
    fn [video, audio] ->
      [
        output("video.mp4",
          video: video,
          audio: audio,
          "c:v": :libx264,
          "c:a": :aac,
          movflags: [:faststart]
        ),

        output("video.webm",
          video: video,
          audio: audio,
          "c:v": :"libvpx-vp9",
          "c:a": :libopus
        )
      ]
    end
  )

Some things FFix supports today:

  • Build ffmpeg commands and filter graphs with Elixir data/functions instead of raw string assembly.
  • Generated helpers and docs for ffmpeg filters like scale, crop, overlay, drawtext, fps, etc.
  • Named graph outputs, multiple inputs/outputs, and stream selectors like src[:video], src[:audio], src[audio: 1].
  • Parsing and serializing filter graphs.
  • to_argv/1 for the command boundary, plus a thin runner for stdin/stdout streaming, collected results, progress events, and ffmpeg logs.

For more details, the docs have the full API and more examples. There is an intro Livebook too.

The goal is to stay close to ffmpeg and keep the core simple. FFix models commands and filter graphs as normal Elixir data, and only serializes them to ffmpeg syntax at the boundary. I’ve used some version of this library for my own ffmpeg tasks for a few years. It was sitting in my backlog for a long time, so I finally cleaned it up and organised it with some help from an LLM agent.

Feedback is very welcome, especially on the API shape and scope.

Install:

def deps do
  [
    {:ffix, "~> 0.1.0"}
  ]
end

Note: ffmpeg must be available at compile time, because FFix generates filter helpers/docs from local ffmpeg metadata.

Links:

https://github.com/akash-akya/ffix

Showing Posts 1 to 1

manhvu

manhvu

Uses pipes to pass arguments to ffmpeg is a nice way.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews