ndrean

ndrean

Can't make erlexec work with stdin as input

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?

Marked As Solved

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.

Also Liked

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

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New

Other popular topics Top

William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement