简体   繁体   English

如何通过gstreamer将RTSP视频流保存到MP4文件?

[英]How to save a RTSP video stream to MP4 file via gstreamer?

I need to get a video stream from my camera via RTSP and save it to a file. 我需要通过RTSP从我的相机获取视频流并将其保存到文件中。 All of this needs to be done via gstreamer. 所有这些都需要通过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" 但它给出了错误:“错误的管道:无法将avdec_h264-0链接到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. 它开始工作,但结果文件无法通过VLC播放。

What is the right command to do? 什么是正确的命令? And if you choose between h264parse and avdec_h264, could you please explain why? 如果你在h264parse和avdec_h264之间做出选择,你能解释一下原因吗?

您需要添加-e标志(流结束),以便mp4mux可以最终确定文件,否则您将损坏不可播放的文件。

 gst-launch -e rtspsrc location=url ! decodebin ! x264enc ! mp4mux ! filesink location=file.mp4

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. 原始h264视频数据有点棘手,因为它有两个特征 - “对齐”和“流格式”,它们可以变化。 h264parse can transform h264 data into the form needed for different h264-related GStreamer elements. h264parse可以将h264数据转换为不同h264相关GStreamer元素所需的形式。

avdec_h264 is a decoder element. avdec_h264是一个解码器元素。 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. 您将来自RTSP流的已编码h264数据放入mp4容器文件中。

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). 如果文件没有播放,您应检查流是否正常,或尝试其他媒体播放器,看看它们是否有效(mplayer,Media Player,Quicktime等等)。

You could also try muxing into a matroska container file using the "matroskamux" element. 您还可以使用“matroskamux”元素尝试多路复用到matroska容器文件中。

If your rtspsrc stream is already encoded in H264, just write to mp4 container directly, instead of doing codec process. 如果你的rtspsrc流已经在H264中编码,只需直接写入mp4容器,而不是编写代码。

Here is my gst-launch-1.0 command for recording rtsp to mp4: 这是我用于将rtsp录制到mp4的gst-launch-1.0命令:

$ gst-launch-1.0 -e rtspsrc location=rtsp://admin:pass@192.168.85.7/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. 如果你想做一些事情,比如修改宽度,高度(使用视频),颜色空间(使用视频转换),帧速率(使用封顶滤镜)等,这应该基于视频/ x-raw类型的功能,你应该从视频解码/ x-h264到video / x-raw。

And, after modifying, you should encode again before linking to mux element (like mp4mux, mpegtsmux, matroskamux, ...). 并且,在修改之后,您应该在链接到mux元素之前再次编码(如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: 如果source已经编码,并且我想用相同的编码写入容器,那么管道将会:

    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: 如果source已被编码,并且我想用不同的编码写入容器,或者我想使用videosink,那么管道将会:

    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: 如果source尚未编码(如videotestsrc),并且我想写入容器,那么管道将会:

    src ! encode ! mux ! filesink

Note: It costs high cpu resources when doing codec ! 注意:执行编解码器时,它需要很高的CPU资源! 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 . 您可以使用方便的工具gst-inspect-1.0查看src,sink,mux,demux,enc,dec,convert,...等元素。 For example: 例如:

$ gst-inspect-1.0 | grep mux

to show all available mux elements. 显示所有可用的mux元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM