我有 n 个连续视频。我想将它们作为一个视频连续播放。例如,当第一个视频结束时,它将不间断地继续播放第二个视频。我的代码如下,我给出视频数量作为参数。创建管道后 gst_element_set_state(pipeline, GST_STATE_NULL);我在该部分中遇到错误。
如果有一个可以通过cmd运行的示例,我可以查看一下。我找不到我想要的例子。
#include <gst/gst.h>
int main(int argc, char* argv[]) {
gst_init(&argc, &argv);
// Set the number of videos (n)
const int num_videos = 3;
GstElement* pipeline = gst_pipeline_new("video_pipeline");
for (int i = 0; i < num_videos; ++i) {
gchar* filename;
gchar* pad_name;
GstElement* multifilesrc;
GstElement* qtdemux;
GstElement* h264parse;
GstElement* avdec_h264;
GstElement* videoconvert;
GstElement* autovideosink;
GstPad* sink_pad;
GstPad* src_pad;
// Generate the filename based on the naming convention (e.g., video0000.mp4)
filename = g_strdup_printf("video%04d.mp4", i);
pad_name = g_strdup_printf("sink_%d", i);
// Create multifilesrc element for each video
multifilesrc = gst_element_factory_make("multifilesrc", NULL);
g_object_set(multifilesrc, "location", filename, NULL);
// Create qtdemux, h264parse, avdec_h264, videoconvert, and autovideosink elements
qtdemux = gst_element_factory_make("qtdemux", NULL);
h264parse = gst_element_factory_make("h264parse", NULL);
avdec_h264 = gst_element_factory_make("avdec_h264", NULL);
videoconvert = gst_element_factory_make("videoconvert", NULL);
autovideosink = gst_element_factory_make("autovideosink", NULL);
// Add all elements to the pipeline
gst_bin_add_many(GST_BIN(pipeline), multifilesrc, qtdemux, h264parse, avdec_h264, videoconvert, autovideosink, NULL);
// Link the elements
gst_element_link_many(multifilesrc, qtdemux, h264parse, avdec_h264, videoconvert, autovideosink, NULL);
// Set up a pad to dynamically link each multifilesrc to the next element
sink_pad = gst_element_get_static_pad(multifilesrc, "src");
src_pad = gst_element_get_static_pad(qtdemux, "sink");
gst_pad_link(sink_pad, src_pad);
// Set the name of the sink pad dynamically
gst_element_class_add_pad_template(GST_ELEMENT_GET_CLASS(pipeline),
gst_static_pad_template_get(pad_name));
gst_element_set_name(multifilesrc, pad_name);
// Clean up
g_free(filename);
g_free(pad_name);
}
// Set the pipeline to playing state
gst_element_set_state(pipeline, GST_STATE_PLAYING);
// Set up the main loop
GMainLoop* loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
// Clean up
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(GST_OBJECT(pipeline));
g_main_loop_unref(loop);
return 0;
}
您不必使用
for
循环播放每个视频,而是提供文件名模式并在 multifilesrc
上设置索引属性,gstreamer 将为您处理增量。
例如
multifilesrc = gst_element_factory_make("multifilesrc", NULL);
g_object_set(multifilesrc, "location", "video%04d.mp4", "index", 0, "stop-index", 3, NULL);
GStreamer 将使用从 0 到
"video%04d.mp4"
(3) 的模式 index
和 stop-index
生成文件名 video0000.mp4
、video0001.mp4
、video0002.mp4
等,并按顺序播放。