2

我有一个使用 GStreamer 的小型 C 项目。我想加载视频并将其与字幕(textoverlay)和经过的时间(timeoverlay)一起显示。

我的想法是这样的:

- create a textoverlay and set a sample subtitle
- create a timeoverlay
- create a videosink
- put the three elements in a new bin and link them: 
  textoverlay -> timeoverlay -> videosink
- use playbin2 for playback and send the video from playbin2 to the new bin 

这是我的测试独立应用程序:

// Save as "test.c". Compile with: 
// gcc -o test `pkg-config --cflags --libs gtk+-2.0 gstreamer-0.10 gstreamer-interfaces-0.10` test.c
#include <gst/gst.h>
#include <gtk/gtk.h>

int main(int argc, char **argv) {
    // Init - GTK is only used here as a GUI hook
    gtk_init (&argc, &argv);
    gst_init(0, NULL);

    // Path to file - juts a simple demo file
    char uri[2048];
    sprintf(&uri[0], "file:///tmp/1.mpg");

    // Playbin and URI
    GstElement *playbin2 = gst_element_factory_make ("playbin2", "playbin2");
    g_object_set (G_OBJECT (playbin2), "uri", &uri[0], NULL);

    // Elements - videosink, textoverlay, timeoverlay
    GstElement *videosink = gst_element_factory_make ("sdlvideosink", "videosink");
    GstElement *textoverlay = gst_element_factory_make("textoverlay", "textoverlay");
    GstElement *timeoverlay = gst_element_factory_make("timeoverlay", "timeoverlay");

    // Set sample text in textoverlay
    g_object_set(G_OBJECT(textoverlay), "text", "Test Subtitle", NULL);

    // Create bin, add elements
    GstElement *mybin = gst_bin_new("mybin");
    gst_bin_add (GST_BIN (mybin), videosink);
    gst_bin_add (GST_BIN (mybin), textoverlay);
    gst_bin_add (GST_BIN (mybin), timeoverlay);

    // Get sink pad for textoverlay and make it a ghostpad for bin
    GstPad *pad_textoverlay_sink = gst_element_get_pad(textoverlay, "video_sink");
    gst_element_add_pad(mybin, gst_ghost_pad_new("sink", pad_textoverlay_sink));

    // Link elements: textoverlay -> timeoverlay -> videosink
    gst_element_link_pads(textoverlay, "src", timeoverlay, "sink");
    gst_element_link_pads(timeoverlay, "src", videosink, "sink");

    // Conect the bin to the playbin
    g_object_set (G_OBJECT (playbin2), "video-sink", mybin, NULL);

    // Play video
    gst_element_set_state (playbin2, GST_STATE_PLAYING);

    // GTK Main loop
    gtk_main ();
}

如果我将 textoverlay 的 src pad 链接到 videosink 的 sink pad(从而跳过 timeoverlay),我会得到一个视频和一个字幕 - 正如预期的那样。

如果我更改代码并使 timeoverlay 的 sink pad 成为 mybin 的 ghostpad,然后将 timeoverlay 的 src pad 链接到 videosink(从而跳过 textoverlay) - 我得到了一个经过时间的视频,正如预期的那样。

但是,当我尝试级联 textoverlay 和 timeoverlay 时,没有视频。它不依赖于videosink - 与xvimagesink 和ximagesink 相同。没有显示错误。使用 gstreamer-launch 它可以正常工作,所以很明显我在 C 中做错了什么——我只是不知道是什么。

任何帮助将不胜感激。

4

3 回答 3

1

更简单的解决方案是使用gst_element_link_many

gst_element_link_many(textoverlay, timeoverlay, videosink);
于 2013-08-11T11:30:26.833 回答
0

Textoverlay 也需要接收器上的文本流。最好使用 GST_DEBUG="*:2" 运行您的应用程序,然后您将看到有关此的警告。

顺便说一句。- 字符 uri[2048];- sprintf(&uri[0], "file:///tmp/1.mpg"); + gchar *uri = "file:///tmp/1.mpg"; :)

于 2012-10-16T10:00:07.123 回答
0

解决问题的方法是使用管道而不是 bin。

GstElement *mybin = gst_pipeline_new ("my-pipeline");

但是,为什么会这样,我仍然不清楚。GStreamer 文档说管道只是一个顶级 bin;我发现文档中没有限制可以在一个 bin 中放置多少元素或它们可以做什么。另一方面,playbin2 内部是一个管道,因此 mybin 可以是一个常规 bin(只要只使用 textoverlay 或 timeoverlay,它就可以作为常规 bin 工作)。

于 2012-10-16T13:53:55.117 回答