defmodule Ffmpeg do @moduledoc false def audio_stream!(stream) do # read from stdin and write to stdout proc_stream = Exile.stream!(~w(ffmpeg -i - -f mp3 -)) spawn_link(fn -> Stream.into(stream, proc_stream) |> Stream.run() end) proc_stream end def run do # :observer.start() # :timer.sleep(10000) IO.puts("Starting") {time, _} = :timer.tc(fn -> Exile.stream!(~w(ffmpeg -i - -f mp3 -), input: File.stream!("music_video.mkv", [], 65535)) |> Stream.into(File.stream!("music.mp3")) |> Stream.run() end) IO.puts("=============== TIME: #{time / 1000}") end def test_stderr do Exile.stream!(~w(ffmpeg -y -i pipe:0 -f mp3 pipe:1), input: File.stream!("danse.mkv", [], 65535), use_stderr: true ) |> Stream.transform( fn -> File.open!("music.mp3", [:write, :binary]) end, fn elem, file -> case elem do {:stdout, data} -> :ok = IO.binwrite(file, data) {:stderr, msg} -> :ok = IO.write(msg) end {[], file} end, fn file -> :ok = File.close(file) end ) |> Stream.run() end def test do script = """ for i in {1..10}; do echo "foo ${i}" echo "bar ${i}" >&2 done """ Exile.stream!(["sh", "-c", script], use_stderr: true) |> Enum.each(fn {stream, lines} -> String.split(lines, "\n", trim: true) |> Enum.each(fn line -> IO.puts("#{stream}: #{line}") end) end) end def random do cmd = ["sh", "-c", "head -c 1000000000 < /dev/urandom"] IO.inspect(cmd) {:ok, s} = Exile.Process.start_link(cmd) :ok = loop(s) Exile.Process.stop(s) end defp loop(s) do case Exile.Process.read(s) do {:ok, data} -> IO.inspect(IO.iodata_length(data), label: :read) loop(s) {:eof, _} -> :ok end end end