Playing MP3 File
File.cd(__DIR__)
Logger.configure(level: :error)
Mix.install([
{:membrane_core, "~> 1.0"},
{:membrane_file_plugin, "~> 0.17.0"},
{:membrane_mp3_mad_plugin, "~> 0.18.3"},
{:membrane_ffmpeg_swresample_plugin, "~> 0.20.2"},
{:membrane_aac_fdk_plugin, "~> 0.18.8"},
{:membrane_kino_plugin, github: "membraneframework-labs/membrane_kino_plugin", tag: "v0.3.2"}
])
Description
This is example of loading MP3 audio from the file, transcoding it to the AAC codec, and playing it via Membrane.Kino.Player.
Pipeline definition
Defines simple linear pipeline of the given structure:
-
Load
MP3file from the file. -
Transcode
MP3toAAC(it is required byKino.Player):-
Decode
MP3format toRawAudio, -
Change
sample_formatfroms24letos16le(it is required byFDK.Encoder), -
Encode it to
AACformat.
-
Decode
-
Fill in audio stream to the player via
:audioinput pad.
import Membrane.ChildrenSpec,
only: [{:child, 2}, {:child, 3}, {:via_in, 2}]
alias Membrane.{
File,
MP3,
FFmpeg,
RawAudio,
AAC,
Kino
}
audio_path = "./assets/sample.mp3"
kino = Membrane.Kino.Player.new(audio: true)
spec =
child(:file_source, %File.Source{location: audio_path})
|> child(:decoder_mp3, MP3.MAD.Decoder)
|> child(:converter, %FFmpeg.SWResample.Converter{
input_stream_format: %RawAudio{channels: 2, sample_format: :s24le, sample_rate: 48_000},
output_stream_format: %RawAudio{channels: 2, sample_format: :s16le, sample_rate: 44_100}
})
|> child(:encoder_aac, AAC.FDK.Encoder)
|> via_in(:audio)
|> child(:player, %Kino.Player.Sink{kino: kino})
:ok
Player
Run pipeline:
alias Membrane.RCPipeline, as: RC
pipeline = RC.start!()
RC.exec_actions(pipeline, spec: spec)
kino