这里通过获取mp4中的图片例子来加深Gstreamer的理解,问问AI实现这样功能的命令,
得到
gst-launch-1.0 filesrc location=test.mp4 ! qtdemux ! queue ! h264parse ! avdec_h264 ! videoconvert ! jpegenc ! multifilesink location=output_image_%03d.jpg
在window环境上试了下,有问题
WARNING: erroneous pipeline: no element "avdec_h264"
修改为下面的命令后,可以保存图片了
gst-launch-1.0 filesrc location=test.mp4 ! decodebin ! jpegenc ! multifilesink location=output_image_%03d.jpg
接下来可以根据这个命令来生成对应的api代码了,目前AI生成的代码一般会有问题,需要修改,
#include <gst/gst.h>
#define FILE_PATH "D:\\2024\\06\\Test\\test.mp4"
static void pad_added_handler(GstElement* src, GstPad* new_pad, gpointer user_data) {
GstElement* dest = GST_ELEMENT(user_data);
GstPad* dest_pad = gst_element_get_static_pad(dest, "sink");
g_print(" pad '%s' ,sink pad '%s'.\n", GST_PAD_NAME(new_pad), GST_PAD_NAME(dest_pad));
if (gst_pad_is_linked(dest_pad)) {
g_printerr("Destination pad is already linked.\n");
goto exit;
}
if (gst_pad_link(new_pad, dest_pad) != GST_PAD_LINK_OK) {
g_printerr("Failed to link pads.\n");
goto exit;
}
exit:
gst_object_unref(dest_pad);
}
int main(int argc, char* argv[]) {
GstElement* pipeline, * filesrc, * decodebin, * jpegenc, * app_sink;
GstBus* bus;
GstMessage* msg;
GstStateChangeReturn ret;
gst_init(&argc, &argv);
pipeline = gst_pipeline_new("image-pipeline");
filesrc = gst_element_factory_make("filesrc", "filesrc");
decodebin = gst_element_factory_make("decodebin", "decodebin");
jpegenc = gst_element_factory_make("jpegenc", "jpegenc");
app_sink = gst_element_factory_make("multifilesink", "app_sink");
if (!pipeline || !filesrc || !decodebin || !jpegenc || !app_sink) {
g_printerr("Not all elements could be created.\n");
return -1;
}
g_object_set(filesrc, "location", FILE_PATH, NULL);
g_object_set(G_OBJECT(app_sink), "location", "output_image_%03d.jpg", NULL);
gst_bin_add_many(GST_BIN(pipeline), filesrc, decodebin, jpegenc, app_sink, NULL);
g_signal_connect(decodebin, "pad-added", G_CALLBACK(pad_added_handler), jpegenc);
if (!gst_element_link(filesrc, decodebin) || !gst_element_link(jpegenc, app_sink)) {
g_printerr("Elements could not be linked.\n");
gst_object_unref(pipeline);
return -1;
}
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 -1;
}
bus = gst_element_get_bus(pipeline);
msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
if (msg != NULL)
gst_message_unref(msg);
gst_object_unref(bus);
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
return 0;
}
在windows vs中,中文注释可能会导致程序问题,这个比较奇怪。
执行后,可以看到生成了很多图片