<div dir="ltr"><div dir="ltr"><div>Hello.</div><div><br></div><div>I have a small problem. I don't know how to write a program that takes a photo using a webcam. I know how to do that using <i>gst-launch-1.0</i>, though.</div><div style="text-align:center"><b>gst-launch-1.0 -e v4l2src device=/dev/video0 num-buffers=1 ! videoconvert ! pngenc ! filesink location=sample.png sync=true</b></div><div style="text-align:left">This works. A photo of me is successfully saved as <i>sample.png</i>.</div><div style="text-align:left"><br></div><div style="text-align:left">What I'd like to do is to write GStreamer code equivalent to the command above. Here is my best try.</div><div style="text-align:left"><span style="font-family:verdana,sans-serif"><b>#include <gst/gst.h><br><br>int main(int argc, char **argv)<br>{<br>    // initialize return value<br>    int status;<br><br>    // initialize GStreamer<br>    gst_init(&argc, &argv);<br><br>    // create pipeline and other elements to use<br>    GstElement *pipeline = gst_pipeline_new("pipeline");<br>    GstElement *source = gst_element_factory_make("v4l2src", "source");<br>    GstElement *converter = gst_element_factory_make("videoconvert", "converter");<br>    GstElement *encoder = gst_element_factory_make("pngenc", "encoder");<br>    GstElement *file = gst_element_factory_make("filesink", "file");<br><br>    // add all elements to the pipeline and connect them<br>    gst_bin_add_many(GST_BIN(pipeline), source, converter, encoder, file, NULL);<br>    gst_element_link_many(source, converter, encoder, file, NULL);<br><br>    // set the properties<br>    g_object_set(source, "device", "/dev/video0", "num-buffers", 1, NULL);<br>    g_object_set(file, "location", "sample.png", "sync", 1, NULL);<br><br>    // play<br>    gst_element_set_state(pipeline, GST_STATE_PLAYING);<br><br>    // wait for EOS<br>    GstBus *bus = gst_element_get_bus(pipeline);<br>    GstMessage *msg = gst_bus_timed_pop(bus, GST_CLOCK_TIME_NONE);<br><br>    // determine return value using msg<br>    switch (GST_MESSAGE_TYPE(msg))<br>    {<br>        case GST_MESSAGE_ERROR:<br>            status = -1;<br>            break;<br>        case GST_MESSAGE_EOS:<br>            status = 0;<br>            break;<br>        default:<br>            status = 1;<br>            break;<br>    }<br><br>    // clean up<br>    gst_element_set_state(pipeline, GST_STATE_NULL);<br>    if (msg != NULL)<br>        gst_message_unref(msg);<br>    gst_object_unref(bus);<br>    gst_object_unref(pipeline);<br><br>    // exit from the program and return status<br>    return status;<br>}</b></span></div><div style="text-align:left">The problem with this code is that really creates <i>sample.png</i>, but the file is corrupted and cannot be opened. Why the code above doesn't work as intended and what do I need to change to make it work the way I want?</div><div style="text-align:left"><br></div><div style="text-align:left">Thank you for reading this. Have a nice day.<br></div><div style="text-align:left"><b></b></div><div style="text-align:center"><b></b></div></div></div>