ndrean

ndrean

I want to transform programmatically an image with FFmpeg. I know you have nice libraries Image, Vix, Stb_image but I want to use the capacity of FFmpeg to take buffer as input. I don’t see how System.cmd nor Port can accept inputs from stdin so I sought for something.

I found Porcelain and erlexec. I saw the last commit was 4 years ago for Porcelaine. Not saying it’s bad but erlexec has its last commit 2 months ago.

I can do it with Porcelaine but not with erlexec and I don’t see why. Maybe someone has some experience with it.

file = File.read!("img.png")

porcelaine = Porcelain.spawn("ffmpeg", ["-i", "-", "img.jpg"], in: :receive, out: :stream)
 {:input, data} = Porcelain.Process.send_input(porcelaine, file)
File.write!("img.jpg", file)
#=> success

but not erlexec: “bad arguments”.

{:ok, pid, os_pid} = :exec.run("ffmpeg -i - img.jpg", [:stdin, stdout: "img2.jpg"])
 :ok = :exec.send(os_pid, file)

[error] GenServer :exec terminating
** (stop) :einval
Last message: {:EXIT, #Port<0.6>, :einval}

Is it related to the way you pass strings to the function?

Showing Posts 1 to 8

al2o3cr

al2o3cr

The stdout option to exec expects a value matching the output_dev_opt spec:

https://github.com/saleyn/erlexec/blob/f5d9995448362489b349a1452cc3b1c0588d62c5/src/exec.erl#L306-L320

You’re passing a binary there, which isn’t on the list. Try a charlist (single quotes, or ~c instead.

ndrean

ndrean OP

I removed the stdout option, same result.

In their example, they use: exec:send(I, <<"Test data\n">>). . Not sure what this means because I can’t stringify a file.

saleyn

saleyn

Perhaps you are not using the options correctly?

Terminal1:

iex(2)> :exec.start([])
{:ok, #PID<0.110.0>}
iex(3)> {:ok, _, i} = :exec.run_link("while true; do read x; echo \"Got: $x\"; done", [:stdin, {:stdout, "/tmp/output"}])
{:ok, #PID<0.111.0>, 2271}
iex(4)> :exec.send(i, "Test1\n")
:ok
iex(5)> :exec.send(i, "Test2\n")
:ok

Terminal2:

$ tail -f /tmp/output
Got: Test1
Got: Test2
ndrean

ndrean OP

Yes, you are right. But in fact, I don’t need the :stdout option as FFmpeg knows what to do - namely save into “img.jpg” - as it is included in the command argument.
FFmpeg only needs to know what his input is, stdin.

A test.
In a shell, I pipe from stdin and it works.
In IEX, I send an empty file in the buffer, it works, but when I pass an image, it fails.

In the shell:

> file img.png
# img.png: PNG image data, 1596 x 732, 8-bit/color RGBA, non-interlaced

> cat img.png | ffmpeg -i - out.jpg -y

> file out.jpg
# out.jpg: JPEG image data, JFIF standard 1.02, aspect ratio, density 1x1, segment length 16, comment: "Lavc61.3.100", baseline, precision 8, 1596x732, components 3

#=> success

In IEX:

> {:ok, _, i} = :exec.run("ffmpeg -i - img.jpg", [:stdin])

# a dummy example: I pass an empty file from stdin
> f = File.read!("test.png")
""
> System.cmd("file",  ["test.png"])
{"test.png: empty\n", 0}

> :exec.send(i, f)
:ok
# it did nothing (not file created), but it did not fail!

# the "real" file from stdin
>  System.cmd("file",  ["img.png"])
{"img.png: PNG image data, 1596 x 732, 8-bit/color RGBA, non-interlaced\n", 0}

> f = File.read!("img.png")
<<137, 80, 78, 71,...>>

:exec.send(i, f)
** (stop) :einval

The difference I see is that I am passing data as a string in the first case, and then a binary.

However, it seems to be able to use binaries, as shown below, so I don’t know.

In IEX:

> t = <<1,2>>
> File.write("test2.txt", t, [:binary])
> f = File.read!("test2.txt")
<<1,2>>
> {:ok, _, i} = :exec.run("cat", [:stdin, {:stdout, "bin.txt"}])
> :exec.send(i, f)
:ok
> File.read!("bin.txt")
<<1,2>>
saleyn

saleyn

I see. The issue is that ffmpeg needs to know that the input sent to it is complete. Otherwise, it’ll sit waiting for more data.

When you use pipes in the shell, the head of the pipe (cat img.png) closes the pipe upon sending the entire content of the file, which helps ffmpeg detect the eof.

In case of erlexec you need to tell the child process that there’s an eof condition like this:

> f = File.read!("test.png")
> {:ok, _, i} = :exec.run_link("ffmpeg -y -i - img.jpg", [:stdin, :stderr])
> :exec.send(i, f)
> :exec.send(i, :eof)
> flush       # This will print out the output of ffmpeg written to stderr and sent to the caller's terminal

This will fix your issue.

It is illustrated here.

ndrean

ndrean OP

Ahh, I tried to append \n (or ‘10’ in binary ?) to the file as in the example, but I failed.

With almost zero knowledge of Erlang, and discovering that that atoms are Elixir specific, I wasn’t sure at all what it was.
Thanks for your time.

akash-akya

akash-akya

@ndrean I created Exile & ExCmd specifically to support moving large amount of data between external process. If you are interested you can check them out.

I created a Livebook sometime back using ffmpeg : Community Livebooks Thread - Share Yours! - #4 by akash-akya

ndrean

ndrean OP

Looks nice! Many thanks!
I did not found ex_cmd, more Elixir style.
I will look more closely when I find why System.find_executable("ffmepg") returns nil ! (Livebook only).

I solved my problem with Porcelain by using a GenServer. The use case is receiving chunks as strings via a WebSocket. In the GenServer, I spawn Porcelain to start FFmpeg to read from stdin, and then just forward messages/chunks continuously to the GenServer and Porcelain feeds pipe:0.

I see you request files via a GET request, but in my case I want to receive octet-stream via HTTP and am stuck so I will definitively look more closely.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews