Ubuntu 20.04下GStreamer推流PNG图片出现绿屏问题求助
问题描述
尝试通过GStreamer管道实现PNG图片的视频流传输,发送端借助OpenCV的VideoWriter推送图片,接收端用gst-launch拉流,但实际运行后接收端显示绿屏。
发送端代码
auto rtsp_pipeline = "appsrc ! video/x-raw,format=BGR,width= 1920, height= 1080,framerate=10/1 ! videoscale ! videoconvert ! x264enc tune=zerolatency bitrate=80000 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port= 5010"; output_stream = cv::VideoWriter(rtsp_pipeline, cv::CAP_GSTREAMER, 0, frame_rate, cv::Size(1920, 1080), true); timer_ = this->create_wall_timer(100ms, std::bind(&PipelineSimulator::timer_callback, this)); // 回调函数 img_ = cv::imread(this->filenames[current_index]); output_stream.write(img_);
接收端管道
gst-launch-1.0 udpsrc port=5010 ! application/x-rtp,encoding-name=H264,payload=96 ! rtph264depay ! h264parse ! decodebin ! videoconvert ! autovideosink
排查与解决建议
- 颜色格式对齐
OpenCV读取PNG默认是BGR格式,但GStreamer的appsrc与OpenCV配合时可能存在格式协商问题。可以尝试将发送端管道的format=BGR改为format=BGRx,同时添加capsfilter强制约束格式:
appsrc ! capsfilter caps="video/x-raw,format=BGR,width=1920,height=1080,framerate=10/1" ! videoconvert ! ...
- 强制关键帧生成
x264enc在zerolatency模式下关键帧间隔可能过大,导致接收端无法初始化解码。强制设置关键帧间隔:
x264enc tune=zerolatency bitrate=80000 speed-preset=superfast key-int-max=10 ! ...
key-int-max=10表示每10帧生成一个关键帧,确保接收端能快速获取解码所需的关键帧。
- 同步缓冲区与尺寸
确认发送端frame_rate变量值为10.0,与定时器100ms周期(对应10fps)匹配,避免帧率不匹配导致缓冲区异常。同时确保读取的PNG图片尺寸严格为1920x1080,若不符则在写入前统一调整:
cv::resize(img_, img_, cv::Size(1920, 1080)); output_stream.write(img_);
- 优化接收端管道
添加queue元素缓解流同步问题,关闭同步避免时钟不匹配:
gst-launch-1.0 udpsrc port=5010 ! application/x-rtp,encoding-name=H264,payload=96 ! rtph264depay ! h264parse ! queue ! decodebin ! videoconvert ! autovideosink sync=false
- 启用调试排查
设置环境变量export GST_DEBUG=3启动发送端,查看管道元素报错;接收端添加-v参数查看caps协商情况:
gst-launch-1.0 -v udpsrc port=5010 ! application/x-rtp,encoding-name=H264,payload=96 ! rtph264depay ! h264parse ! decodebin ! videoconvert ! autovideosink
通过调试信息确认格式协商是否成功、是否有元素异常。
内容的提问来源于stack exchange,提问作者Andrea Begni
相关产品推荐
相关产品推荐

