You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将GStreamer音视频录制命令转换为C语言代码并获取示例

GStreamer音视频同步录制C语言实现示例

以下是完全对应你提供的GStreamer命令逻辑的C语言代码,实现音视频同步采集、编码、复用并保存到同一文件:

#include <gst/gst.h>
#include <glib.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("录制完成,收到EOS信号\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("错误发生: %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, *qmmfsrc, *c2venc, *h264parse, *video_queue, *mux;
    GstElement *alsasrc, *audio_queue, *audioconvert, *audioresample, *aacenc, *aacparse, *filesink;
    GstBus *bus;
    guint bus_watch_id;
    GstCaps *video_caps;

    // 初始化GStreamer
    gst_init(&argc, &argv);
    loop = g_main_loop_new(NULL, FALSE);

    // 创建各个元素
    pipeline = gst_pipeline_new("recording-pipeline");
    qmmfsrc = gst_element_factory_make("qtiqmmfsrc", "qmmf");
    c2venc = gst_element_factory_make("qtic2venc", "video-encoder");
    h264parse = gst_element_factory_make("h264parse", "h264-parser");
    video_queue = gst_element_factory_make("queue", "video-queue");
    mux = gst_element_factory_make("mpegtsmux", "mux");
    alsasrc = gst_element_factory_make("alsasrc", "audio-source");
    audio_queue = gst_element_factory_make("queue", "audio-queue");
    audioconvert = gst_element_factory_make("audioconvert", "audio-convert");
    audioresample = gst_element_factory_make("audioresample", "audio-resample");
    aacenc = gst_element_factory_make("avenc_aac", "aac-encoder");
    aacparse = gst_element_factory_make("aacparse", "aac-parser");
    filesink = gst_element_factory_make("filesink", "file-sink");

    // 检查元素是否创建成功
    if (!pipeline || !qmmfsrc || !c2venc || !h264parse || !video_queue || !mux ||
        !alsasrc || !audio_queue || !audioconvert || !audioresample || !aacenc || !aacparse || !filesink) {
        g_printerr("部分元素创建失败,请检查GStreamer插件是否安装完整\n");
        return -1;
    }

    // 设置元素属性
    g_object_set(qmmfsrc, "camera", 1, NULL);
    g_object_set(alsasrc, "device", "hw:0,0", "buffer-time", 50000, NULL);
    g_object_set(filesink, "location", "/data/Pratik/test.mp4", NULL);

    // 创建视频格式约束Caps
    video_caps = gst_caps_from_string("video/x-raw,format=NV12,width=1920,height=1080,framerate=20/1");

    // 将所有元素添加到管道
    gst_bin_add_many(GST_BIN(pipeline), qmmfsrc, c2venc, h264parse, video_queue, mux,
                     alsasrc, audio_queue, audioconvert, audioresample, aacenc, aacparse, filesink, NULL);

    // 链接视频分支:qtiqmmfsrc -> [格式约束] -> qtic2venc -> h264parse -> queue -> mux
    if (!gst_element_link_filtered(qmmfsrc, c2venc, video_caps)) {
        g_printerr("视频源与编码器链接失败\n");
        gst_object_unref(pipeline);
        return -1;
    }
    gst_caps_unref(video_caps); // 释放Caps资源

    if (!gst_element_link_many(c2venc, h264parse, video_queue, mux, NULL)) {
        g_printerr("视频分支元素链接失败\n");
        gst_object_unref(pipeline);
        return -1;
    }

    // 链接音频分支:alsasrc -> queue -> audioconvert -> audioresample -> avenc_aac -> aacparse -> mux
    if (!gst_element_link_many(alsasrc, audio_queue, audioconvert, audioresample, aacenc, aacparse, mux, NULL)) {
        g_printerr("音频分支元素链接失败\n");
        gst_object_unref(pipeline);
        return -1;
    }

    // 设置总线监听
    bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
    bus_watch_id = gst_bus_add_watch(bus, bus_call, loop);
    gst_object_unref(bus);

    // 启动管道
    g_print("开始录制...\n");
    gst_element_set_state(pipeline, GST_STATE_PLAYING);

    // 进入主循环
    g_main_loop_run(loop);

    // 停止录制并清理资源
    g_print("停止录制,清理资源...\n");
    gst_element_set_state(pipeline, GST_STATE_NULL);
    gst_object_unref(pipeline);
    g_source_remove(bus_watch_id);
    g_main_loop_unref(loop);

    return 0;
}

关键说明

  • 元素与属性匹配:完全对应原命令中的组件配置,包括摄像头编号、音频设备路径、输出文件位置等参数
  • 格式约束:通过gst_caps_from_string确保视频源输出符合要求的NV12格式、分辨率和帧率
  • 链路构建:分别搭建视频和音频处理链路,最终接入mpegtsmux复用器实现音视频合并
  • 消息监听:处理录制完成(EOS)和错误事件,保证程序正常退出
  • 资源清理:录制结束后停止管道,释放所有GStreamer资源,避免内存泄漏

编译时需链接GStreamer库,示例编译命令:

gcc -o gst_recorder gst_recorder.c $(pkg-config --cflags --libs gstreamer-1.0)

内容的提问来源于stack exchange,提问作者Pratik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 13:16:15