How to save a RTSP video stream to MP4 file via gstreamer?
Asked Answered
N

3

14

I need to get a video stream from my camera via RTSP and save it to a file. All of this needs to be done via gstreamer.

After some google searching, I tried the following:

gst-launch-1.0 rtspsrc location=rtsp://192.168.1.184/live2.sdp ! queue ! rtph264depay ! avdec_h264 ! mp4mux ! filesink location=result3.mp4

but it gives the error: "Erroneous pipeline: could not link avdec_h264-0 to mp4mux0"

gst-launch-1.0 rtspsrc location=rtsp://192.168.1.184/live2.sdp ! queue ! rtph264depay ! h264parse ! mp4mux ! filesink location=result3.mp4

It starts doing work, but the result file is not playable via VLC.

What is the right command to do? And if you choose between h264parse and avdec_h264, could you please explain why?

Narine answered 15/9, 2014 at 3:39 Comment(1)
@downvote: I see nothing wrong with this question except for a title. @Juster: 1) you might try it without h264parse at all. 2) does the stream play at all?Lindon
S
18

You need to add -e flag (end of stream) so that mp4mux can finalize file or else you'll get corrupted non playable file.

 gst-launch -e rtspsrc location=url ! decodebin ! x264enc ! mp4mux ! filesink location=file.mp4
Snips answered 20/9, 2014 at 13:5 Comment(1)
In my case this command made playable file but all frames look like first frame. As if video is freezed.Elicia
D
11

If your rtspsrc stream is already encoded in H264, just write to mp4 container directly, instead of doing codec process.

Here is my gst-launch-1.0 command for recording rtsp to mp4:

$ gst-launch-1.0 -e rtspsrc location=rtsp://admin:[email protected]/rtsph2641080p protocols=tcp ! rtph264depay ! h264parse ! mp4mux ! filesink location=~/camera.mp4

If you want to do something like modifying width, height (using videoscale), colorspace (using videoconvert), framerate (using capsfilter), etc., which should do based on capability of video/x-raw type, you should decode from video/x-h264 to video/x-raw.

And, after modifying, you should encode again before linking to mux element (like mp4mux, mpegtsmux, matroskamux, ...).

It seems like you are not sure when to use video decoder. Here simply share some experience of using video codec:

  1. If source has been encoded, and I want to write to the container with the same encode, then the pipeline will like:

    src ! ... ! mux ! filesink

  2. If source has been encoded, and I want to write to the container with different encode, or I want to play with videosink, then the pipeline will like:

    src ! decode ! ... ! encode ! mux ! filesink src ! decode ! ... ! videosink

  3. If source hasn't been encoded (like videotestsrc), and I want to write to the container, then the pipeline will like:

    src ! encode ! mux ! filesink

Note: It costs high cpu resources when doing codec ! So, if you don't need to do codec work, don't do that.

You can check out src, sink, mux, demux, enc, dec, convert, ..., etc. elements using convenient tool gst-inspect-1.0. For example:

$ gst-inspect-1.0 | grep mux

to show all available mux elements.

Deina answered 7/11, 2018 at 8:48 Comment(2)
why is h264parse needed? the de-payloader should already be h264, shouldn't mp4mux be able to read that? (note, when I remove it, my mp4 file is 500 bytes and doesn't grow)Nitroparaffin
The mp4mux is not able to read raw h264 data, which the stream-format is "byte-stream"(raw). Another solution is to pipe with encoder and decoder, but the cost of CPU will be higher. See different between parser and encoder. You can also refer to the sink and src of rtph264depay, mp4mux, and h264parse.Deina
B
8

The second command looks correct. Raw h264 video data is a bit tricky because it has two characteristics--"alignment" and "stream-format", which can vary. h264parse can transform h264 data into the form needed for different h264-related GStreamer elements.

avdec_h264 is a decoder element. You don't want to decode the data since you're apparently not displaying it. You're putting encoded h264 data from an RTSP stream into an mp4 container file.

If the file doesn't play, you should check that the stream is good, or try other media players and see if they work (mplayer, Media Player, Quicktime, whatever).

You could also try muxing into a matroska container file using the "matroskamux" element.

Bessel answered 17/9, 2014 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.