-
Notifications
You must be signed in to change notification settings - Fork 4
/
muxer_isom.exs
65 lines (56 loc) · 1.9 KB
/
muxer_isom.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Mix.install([
:membrane_aac_plugin,
:membrane_h26x_plugin,
:membrane_hackney_plugin,
{:membrane_mp4_plugin, path: __DIR__ |> Path.join("..") |> Path.expand()}
])
defmodule Example do
use Membrane.Pipeline
@samples_url "https://raw.githubusercontent.com/membraneframework/static/gh-pages/samples/"
@video_url @samples_url <> "ffmpeg-testsrc.h264"
@audio_url @samples_url <> "test-audio.aac"
@output_file "example.mp4"
def start_link() do
Membrane.Pipeline.start_link(__MODULE__)
end
@impl true
def handle_init(_ctx, _opts) do
structure = [
child(:video_source, %Membrane.Hackney.Source{
location: @video_url,
hackney_opts: [follow_redirect: true]
})
|> child(:video_parser, %Membrane.H264.Parser{
generate_best_effort_timestamps: %{framerate: {30, 1}},
output_stream_structure: :avc1
}),
child(:audio_source, %Membrane.Hackney.Source{
location: @audio_url,
hackney_opts: [follow_redirect: true]
})
|> child(:audio_parser, %Membrane.AAC.Parser{out_encapsulation: :none, output_config: :esds}),
child(:muxer, Membrane.MP4.Muxer.ISOM)
|> child(:sink, %Membrane.File.Sink{location: @output_file}),
get_child(:audio_parser) |> get_child(:muxer),
get_child(:video_parser) |> get_child(:muxer)
]
{[spec: structure], %{}}
end
# The rest of the module is used only for pipeline self-termination after processing finishes
@impl true
def handle_element_end_of_stream(:sink, _pad, _ctx, state) do
{[terminate: :shutdown], state}
end
@impl true
def handle_element_end_of_stream(_child, _pad, _ctx, state) do
{[], state}
end
end
# Start and monitor the pipeline
{:ok, _supervisor_pid, pipeline_pid} = Example.start_link()
ref = Process.monitor(pipeline_pid)
# Wait for the pipeline to finish
receive do
{:DOWN, ^ref, :process, _pipeline_pid, _reason} ->
:ok
end