eloy

eloy

Saving RTP stream to file using membrane framework

I’m playing around with membrane, trying to save an RTP stream from an IP camera to file. Using code from some demos I managed to get the the video stream and send it to a hls stream to make sure it works. At this point my code looks like this:

  def handle_init(opts) do
    %{audio_port: audio_port, video_port: video_port} = opts

    spec = %ParentSpec{
      children: [
        video_src: %Membrane.Element.UDP.Source{
          local_port_no: video_port,
        },
        audio_src: %Membrane.Element.UDP.Source{
          local_port_no: audio_port,
        },
        rtp: %RTP.SessionBin{
          secure?: false,
        },
      ],
      links: [
        link(:video_src) |> via_in(:rtp_input) |> to(:rtp),
        link(:audio_src) |> via_in(:rtp_input) |> to(:rtp)
      ]
    }

    {{:ok, spec: spec}, %{}}
  end

  defp handle_stream(%{video: video_ssrc, table: table}) do
    spec = %ParentSpec{
      children: %{
        video_parser: %Membrane.H264.FFmpeg.Parser{framerate: {30, 1}, alignment: :au, attach_nalus?: true},
        video_payloader: Membrane.MP4.Payloader.H264,
        video_cmaf_muxer: Membrane.MP4.CMAF.Muxer,

        hls: %Membrane.HTTPAdaptiveStream.Sink {
          manifest_module: Membrane.HTTPAdaptiveStream.HLS,
          target_window_duration: 10 |> Membrane.Time.seconds(),
          storage: %Membrane.HTTPAdaptiveStream.Storages.FileStorage{directory: "output"}
        }


      },
      links: [
        link(:rtp)
        |> via_out(Pad.ref(:output, video_ssrc))
        |> to(:video_parser)

        |> to(:video_payloader)
        |> to(:video_cmaf_muxer)
        |> via_in(:input)

        |> to(:hls)
      ],
      stream_sync: :sinks
    }

    [spec: spec]
  end

I’d like to replace the hls output and save the stream to a avi or mp4 file. Is it possible?

Thanks!

Most Liked

mickel8

mickel8

Membrane Core Team

Hi @eloy,
I am not sure if this is possible with current mp4_plugin.

What you can try is to save this stream as h264 video and play it with ffmpeg or use ffmpeg to convert it to mp4 file.

To save something to file

You can use membrane_file_plugin.

To play h264 video using ffmpeg

ffplay -f h264 video.h264.
The -f option stands for format.

To convert h264 file to mp4 one

ffmpeg -framerate 24 -i video.h264 -c copy video.mp4(source)
The -i option stands for input.
The -c option stands for codec so that -c copy means that streams will be copied into output file i.e. no reencoding will be performed.
The -framerate is video framerate. I think you can try to omit it.

mat-hek

mat-hek

Membrane Core Team

RTP and RTMP are totally different protocols and the blog shows how to dump to HLS, not MP4 :wink: Membrane can dump both RTP and RTMP to HLS, while dumping to MP4 requires transcoding. Dumping to MP4 without transcoding is not really straightforward, but support for that is coming too: Opus payloader and allowing in-band parameters with H264 Payloads by daniel-jodlos · Pull Request #38 · membraneframework/membrane_mp4_plugin · GitHub.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement