gstreamer向appsrc发送帧画面的代码

442 篇文章 12 订阅
32 篇文章 21 订阅

  这个代码可以正常运行,黑白画面交替:

#include <gst/gst.h>

#define VIDEO_WIDTH   384
#define VIDEO_HEIGHT  288
#define VIDEO_FORMAT  "RGB16"
#define PIXEL_SIZE    2

static void cb_need_data (GstElement *appsrc, guint       unused_size, gpointer    user_data)
{
    static gboolean white = FALSE;
    static GstClockTime timestamp = 0;
    GstBuffer *buffer;
    guint size;
    GstFlowReturn ret;

    size = VIDEO_WIDTH * VIDEO_HEIGHT * PIXEL_SIZE;
    buffer = gst_buffer_new_allocate (NULL, size, NULL);

    /* this makes the image black/white */
    gst_buffer_memset (buffer, 0, white ? 0xFF : 0x00, size);

    white = !white;

    GST_BUFFER_PTS (buffer) = timestamp;
    GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale_int (1, GST_SECOND, 2);

    timestamp += GST_BUFFER_DURATION (buffer);

    g_signal_emit_by_name (appsrc, "push-buffer", buffer, &ret);
    gst_buffer_unref (buffer);

    if (ret != GST_FLOW_OK)
    {
        /* something wrong, stop pushing */
        g_main_loop_quit (loop);
    }
}

gint main (gint   argc, gchar *argv[])
{
    GstElement *pipeline, *appsrc, *conv, *videosink;
    GMainLoop *loop;

    /* init GStreamer */
    gst_init (NULL, NULL);
    loop = g_main_loop_new (NULL, FALSE);

    /* setup pipeline */
    pipeline  = gst_pipeline_new ("pipeline");
    appsrc    = gst_element_factory_make ("appsrc",       "source");
    conv      = gst_element_factory_make ("videoconvert", "conv");
    videosink = gst_element_factory_make ("xvimagesink",  "videosink");

    /* setup */
    g_object_set (G_OBJECT (appsrc), "caps",
        gst_caps_new_simple ("video/x-raw",
                     "format",    G_TYPE_STRING,     VIDEO_FORMAT,
                     "width",     G_TYPE_INT,        VIDEO_WIDTH,
                     "height",    G_TYPE_INT,        VIDEO_HEIGHT,
                     "framerate", GST_TYPE_FRACTION, 0, 1,
                     NULL), NULL);
    gst_bin_add_many (GST_BIN (pipeline), appsrc, conv, videosink, NULL);
    gst_element_link_many (appsrc, conv, videosink, NULL);

    /* setup appsrc */
    g_object_set (G_OBJECT (appsrc),
        "stream-type", 0,
        "format", GST_FORMAT_TIME, NULL);
    g_signal_connect (appsrc, "need-data", G_CALLBACK (cb_need_data), NULL);

    /* play */
    gst_element_set_state (pipeline, GST_STATE_PLAYING);
    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;
}
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
你可以使用GStreamer库来在C语言中抽取。下面是一个简单的示例代码,展示了如何使用GStreamer来抽取视频: ```c #include <gst/gst.h> static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data) { GMainLoop *loop = (GMainLoop *)data; switch (GST_MESSAGE_TYPE(msg)) { case GST_MESSAGE_EOS: g_print("End of stream\n"); g_main_loop_quit(loop); break; case GST_MESSAGE_ERROR: { gchar *debug; GError *error; gst_message_parse_error(msg, &error, &debug); g_free(debug); g_printerr("Error: %s\n", error->message); g_error_free(error); g_main_loop_quit(loop); break; } default: break; } return TRUE; } int main(int argc, char *argv[]) { GMainLoop *loop; GstElement *pipeline, *source, *decoder, *sink; GstBus *bus; gst_init(&argc, &argv); loop = g_main_loop_new(NULL, FALSE); if (argc != 2) { g_printerr("Usage: %s <video file>\n", argv[0]); return -1; } pipeline = gst_pipeline_new("video-player"); source = gst_element_factory_make("filesrc", "file-source"); decoder = gst_element_factory_make("decodebin", "decoder"); sink = gst_element_factory_make("autovideoconvert", "video-sink"); if (!pipeline || !source || !decoder || !sink) { g_printerr("Not all elements could be created.\n"); return -1; } g_object_set(G_OBJECT(source), "location", argv[1], NULL); gst_bin_add_many(GST_BIN(pipeline), source, decoder, sink, NULL); gst_element_link(source, decoder); gst_element_link(decoder, sink); g_signal_connect(decoder, "pad-added", G_CALLBACK(on_pad_added), sink); bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); gst_bus_add_watch(bus, bus_call, loop); gst_object_unref(bus); gst_element_set_state(pipeline, GST_STATE_PLAYING); g_main_loop_run(loop); gst_element_set_state(pipeline, GST_STATE_NULL); gst_object_unref(GST_OBJECT(pipeline)); g_main_loop_unref(loop); return 0; } ``` 这个示例代码创建了一个GStreamer管道,并使用`filesrc`元素从指定的视频文件中读取数据。然后,使用`decodebin`元素解码视频流,并使用`autovideoconvert`元素进行格式转换。你可以根据需要修改和扩展这个代码,以满足你的具体需求。 请确保在编译时链接GStreamer库,并在运行代码之前设置正确的视频文件路径。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柳鲲鹏

能给阁下一点帮助,非常荣幸

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值