GStreamer —— 2.18、Windows下Qt加载GStreamer库后运行 - “播放教程 6:音频可视化“(附:完整源码)

发布于:2025-03-13 ⋅ 阅读:(112) ⋅ 点赞:(0)
运行效果

在这里插入图片描述

介绍

     GStreamer 带有一组将音频转换为视频的元素。他们 可用于科学可视化或为您的音乐增添趣味 player 的本教程展示了:

          • 如何启用音频可视化

          • 如何选择可视化元素

     启用音频可视化实际上非常简单。设置相应的标志,当纯音频流为 found,它将实例化必要的元素来创建和显示可视化。

GStreamer相关运行库
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/include/gstreamer-1.0/gst
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/include
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/include/gstreamer-1.0
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/include/glib-2.0
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/lib/glib-2.0/include

LIBS += D:/Software/GStreamer/1.0/mingw_x86_64/lib/gstreamer-1.0.lib
LIBS += D:/Software/GStreamer/1.0/mingw_x86_64/lib/glib-2.0.lib
LIBS += D:/Software/GStreamer/1.0/mingw_x86_64/lib/gobject-2.0.lib

源码
#include <gst/gst.h>

/* playbin的flag */
typedef enum
{
    GST_PLAY_FLAG_VIS           = (1 << 3) /* 在没有视频流时启用可视化渲染。 */
} GstPlayFlags;

/* 如果这是可视化元素,则返回TRUE */
static gboolean filter_vis_features (GstPluginFeature *feature, gpointer data)
{
    GstElementFactory *factory;

    if (!GST_IS_ELEMENT_FACTORY (feature))
        return FALSE;
    factory = GST_ELEMENT_FACTORY (feature);
    if (!g_strrstr (gst_element_factory_get_klass (factory), "Visualization"))
        return FALSE;

    return TRUE;
}

int main(int argc, char *argv[])
{
    /* d初始化Streamer */
    gst_init (&argc, &argv);

    /* 获取所有可视化插件的列表 */
    GList *list = gst_registry_feature_filter (gst_registry_get (), filter_vis_features, FALSE, NULL);

    /* 打印他们的名字 */
    GstElementFactory *selected_factory = NULL;
    g_print("Available visualization plugins:\n");
    for (GList *walk = list; walk != NULL; walk = g_list_next (walk))
    {
        GstElementFactory *factory = GST_ELEMENT_FACTORY (walk->data);
        const gchar *name = gst_element_factory_get_longname (factory);
        g_print("  %s\n", name);

        if (selected_factory == NULL || g_str_has_prefix (name, "GOOM"))
        {
            selected_factory = factory;
        }
    }
    if (!selected_factory) { g_print ("No visualization plugins found!\n"); return -1; }

    /* 我们现在已经为可视化元素选择了一个工厂 */
    g_print ("Selected '%s'\n", gst_element_factory_get_longname (selected_factory));
    GstElement *vis_plugin = gst_element_factory_create (selected_factory, NULL);
    if (!vis_plugin){return -1;}

    /* 构建管道 */
    GstElement *pipeline = gst_parse_launch ("playbin uri=http://radio.hbr1.com:19800/ambient.ogg", NULL);

    /* 设置可视化标志 */
    guint flags;
    g_object_get (pipeline, "flags", &flags, NULL);
    flags |= GST_PLAY_FLAG_VIS;
    g_object_set (pipeline, "flags", flags, NULL);

    /* 为playbin设置vis插件 */
    g_object_set (pipeline, "vis-plugin", vis_plugin, NULL);

    /* 开始播放 */
    gst_element_set_state (pipeline, GST_STATE_PLAYING);

    /* 等待直到错误或结束 */
    GstBus *bus = gst_element_get_bus (pipeline);
    GstMessage *msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));

    /* 释放资源 */
    if (msg != NULL){ gst_message_unref (msg); }
    gst_plugin_feature_list_free (list);
    gst_object_unref (bus);
    gst_element_set_state (pipeline, GST_STATE_NULL);
    gst_object_unref (pipeline);
    return 0;
}

关注

笔者 - jxd


网站公告

今日签到

点亮在社区的每一天
去签到