Tool/software: Linux
Hello,
I want to use gstreamer to encode a video file,and here is my code:
#include <gst/gst.h> int main(int argc, char *argv[]) { GstElement *pipeline, *src, *video_parse, *myvpe, *enc, *sink, *capsfilter1; GstCaps *caps1; GstBus *bus; GstMessage *msg; GstStateChangeReturn ret; gst_init(&argc, &argv); pipeline = gst_pipeline_new("videoencoder"); src = gst_element_factory_make("filesrc", "src"); video_parse = gst_element_factory_make("videoparse", "video_parse"); myvpe = gst_element_factory_make("videoconvert", "myvpe"); capsfilter1 = gst_element_factory_make("capsfilter", "capsfilter1"); enc = gst_element_factory_make("ducatih264enc", "enc"); sink = gst_element_factory_make("filesink", "sink"); if (!pipeline || !src || !video_parse || !myvpe || !capsfilter1 || !enc || !sink) { g_printerr("One element could not be created.Exiting.\n"); return -1; } g_object_set(G_OBJECT(src), "location", "/mnt/akiyo_qcif.yuv", NULL); g_object_set(G_OBJECT(video_parse), "width", "176", NULL); g_object_set(G_OBJECT(video_parse), "height", "144", NULL); g_object_set(G_OBJECT(video_parse), "format", 2, NULL); g_object_set(G_OBJECT(video_parse), "framerate", 25,1, NULL); caps1 = gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, "NV12", "width", G_TYPE_INT, 176, "height", G_TYPE_INT, 144, "framerate", GST_TYPE_FRACTION, 25, 1, NULL); g_object_set(G_OBJECT(capsfilter1), "caps", caps1, NULL); gst_object_unref(caps1); g_object_set(G_OBJECT(sink), "location", "/mnt/b2b.h264", NULL); gst_bin_add_many(GST_BIN(pipeline), src, video_parse, myvpe, capsfilter1, enc, sink, NULL); if (gst_element_link_many(src, video_parse, myvpe, capsfilter1, enc, sink) != TRUE) { 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; }
Then,I ran this code.It encountered a problem:
(gst-test:1185): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed
Elements could not be linked.
However,I use the same pipeline with command line successful:
gst-launch-1.0 filesrc location=/mnt/akiyo_qcif.yuv ! videoparse width=176 height=144 format=i420 ! videoconvert ! 'video/x-raw, format=(string)NV12, framerate=(fraction)25/1, width=(int)176, height=(int)144' ! ducatih264enc ! filesink location=x.h264
so,could you tell me how to deal with this problem.Thank you.