【发布时间】:2021-09-16 17:11:59
【问题描述】:
我正在启动一个项目,该项目包括从 RTSP 服务器接收视频并使用 QT c++ 和 GSTreamer 在面板中显示它。我想接收每一帧作为 QImage 对象来运行我需要的一些功能。我知道我们可以使用 QTMultimedia 链接 Gstreamer 并查看视频,但在这种特殊情况下,我想要 QImages。我找到了一种方法(或者我认为我做到了)通过调用函数“gst_app_sink_pull_sample”从视频中获取样本。我们从中得到一个 GTSample。但是,我没有找到任何方法将其转换为例如易于转换为 QImage 的 JPG 原始数据。
我还找到了一种从 GSTSample 访问数据的方法:“How to get video stream frame-by-frame from Gstreamer pipeline? (without OpenCV)” 但是,再一次,我不知道如何将这些数据转换为 QImage。
/* Initialize GStreamer */
gst_init (NULL, NULL);
/* Create the elements */
GstElement * source = gst_element_factory_make ("videotestsrc", "source"); // HERE I'M USING THE GSTREAMER SOURCE TEST
GstElement * sink = gst_element_factory_make ("appsink", "sink");
GstAppSink *appsink = GST_APP_SINK(sink);
/* Create the empty pipeline */
GstElement * pipeline = gst_pipeline_new ("test-pipeline");
if (!pipeline || !source || !sink) {
g_printerr ("Not all elements could be created.\n");
return ;
}
/* Build the pipeline */
gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
if (gst_element_link (source, sink) != TRUE) {
g_printerr ("Elements could not be linked.\n");
gst_object_unref (pipeline);
return ;
}
/* Modify the source's properties */
g_object_set (source, "pattern", 0, NULL);
/* Start playing */
ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr ("Unable to set the pipeline to the playing state.\n");
gst_object_unref (pipeline);
return;
}
// My question starts HERE
GstSample *sample = nullptr;
do{
sample = gst_app_sink_pull_sample(appsink); // Get the frame
if (!sample) {
printf("sample is NULL\n");
}else{
// Get the raw data (or trying to...)
GstBuffer * buffer = gst_sample_get_buffer(sample);
GstMapInfo info;
gst_buffer_map(buffer, &info, GST_MAP_READ);
// Dumb way to check if the frame is being received properly
QImage imageAux((const unsigned char*)info.data, 100, 100, QImage::Format_RGB16);
imageAux.save("out.jpg"); //returns garbage
}
}while(sample);
【问题讨论】:
-
我想提出一个替代解决方案,我需要更多关于您的要求的信息:据我了解,您想要获取每一帧,将其转换为 QImage,修改它,但在那之后你在哪里想放置吗?您希望它在修改后显示在窗口中,还是您想用它做其他事情?
-
你好@eyllanesc,是的,我想以 Windows 形式显示它。