Skip to content

Instantly share code, notes, and snippets.

@hugobarauna
Created August 9, 2023 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugobarauna/d3c4eaf834920ceceafc9a71a9dc43f7 to your computer and use it in GitHub Desktop.
Save hugobarauna/d3c4eaf834920ceceafc9a71a9dc43f7 to your computer and use it in GitHub Desktop.
Membrane Dashboard using KinoMembrane and Livebook

Membrane pipeline dashboard

Logger.configure(level: :info)

Mix.install([
  :kino_membrane,
  :membrane_hackney_plugin,
  :membrane_portaudio_plugin,
  :membrane_ffmpeg_swresample_plugin,
  :membrane_mp3_mad_plugin,
  {:kino, "~> 0.9.4"}
])

Dashboard

start_pipeline = fn mp3_url ->
  import Membrane.ChildrenSpec
  alias Membrane.RCPipeline

  pipeline = RCPipeline.start_link!()

  RCPipeline.exec_actions(pipeline,
    spec:
      child(:hackney, %Membrane.Hackney.Source{
        location: mp3_url,
        hackney_opts: [follow_redirect: true]
      })
      |> via_in(:input, auto_demand_size: 10)
      |> child(:decoder, Membrane.MP3.MAD.Decoder)
      |> child(:converter, %Membrane.FFmpeg.SWResample.Converter{
        output_stream_format: %Membrane.RawAudio{
          sample_format: :s16le,
          sample_rate: 48000,
          channels: 2
        }
      })
      |> child(:portaudio, Membrane.PortAudio.Sink),
    playback: :playing
  )

  pipeline
end
form =
  Kino.Control.form(
    [
      mp3_url:
        Kino.Input.url("MP3 url",
          default:
            "https://raw.githubusercontent.com/membraneframework/membrane_demo/master/simple_pipeline/sample.mp3"
        )
    ],
    submit: "Submit"
  )

Kino.listen(form, fn %{data: %{mp3_url: url}} ->
  pipeline = start_pipeline.(url)
  KinoMembrane.pipeline_dashboard(pipeline) |> Kino.render()
end)

form
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment