目标

GStreamer有一系列把音频转换成视频的element。他们可以用于科学的目的或者增加音乐播放器的趣味性。本教程展示:

如何允许音频的可视化

如何选择可视化element

介绍

在playbin2里面运行音频可视化是非常容易的。当遇到一个只有音频的流时,只需要正确地设置playbin2的一些标志就行了。它会自己创建必要的element然后显示的。

如果你想要增加实际的element的趣味性,你要自己实例化它然后通过vis-plug属性来告诉playbin2。

本教程GStreamer注册的所有关于可视化的element,选择了goom并传给了playbin2

一个有趣的音乐播放器

[objc] view
plain
 copy

  1. <span style="font-size:14px;">#include <gst/gst.h>
  2. /* playbin2 flags */
  3. typedef enum {
  4. << 3) /* Enable rendering of visualizations when there is no video stream. */
  5. } GstPlayFlags;
  6. /* Return TRUE if this is a Visualization element */
  7. static gboolean filter_vis_features (GstPluginFeature *feature, gpointer data) {
  8. GstElementFactory *factory;
  9. if (!GST_IS_ELEMENT_FACTORY (feature))
  10. return FALSE;
  11. factory = GST_ELEMENT_FACTORY (feature);
  12. if (!g_strrstr (gst_element_factory_get_klass (factory), "Visualization"))
  13. return FALSE;
  14. return TRUE;
  15. }
  16. int main(int argc, charchar *argv[]) {
  17. GstElement *pipeline, *vis_plugin;
  18. GstBus *bus;
  19. GstMessage *msg;
  20. GList *list, *walk;
  21. GstElementFactory *selected_factory = NULL;
  22. guint flags;
  23. /* Initialize GStreamer */
  24. gst_init (&argc, &argv);
  25. /* Get a list of all visualization plugins */
  26. list = gst_registry_feature_filter (gst_registry_get_default (), filter_vis_features, FALSE, NULL);
  27. /* Print their names */
  28. g_print("Available visualization plugins:\n");
  29. for (walk = list; walk != NULL; walk = g_list_next (walk)) {
  30. const gchar *name;
  31. GstElementFactory *factory;
  32. factory = GST_ELEMENT_FACTORY (walk->data);
  33. name = gst_element_factory_get_longname (factory);
  34. g_print("  %s\n", name);
  35. if (selected_factory == NULL || g_str_has_prefix (name, "GOOM")) {
  36. selected_factory = factory;
  37. }
  38. }
  39. /* Don't use the factory if it's still empty */
  40. /* e.g. no visualization plugins found */
  41. if (!selected_factory) {
  42. g_print ("No visualization plugins found!\n");
  43. ;
  44. }
  45. /* We have now selected a factory for the visualization element */
  46. g_print ("Selected '%s'\n", gst_element_factory_get_longname (selected_factory));
  47. vis_plugin = gst_element_factory_create (selected_factory, NULL);
  48. if (!vis_plugin)
  49. ;
  50. /* Build the pipeline */
  51. pipeline = gst_parse_launch ("playbin2 uri=http://radio.hbr1.com:19800/ambient.ogg", NULL);
  52. /* Set the visualization flag */
  53. g_object_get (pipeline, "flags", &flags, NULL);
  54. flags |= GST_PLAY_FLAG_VIS;
  55. g_object_set (pipeline, "flags", flags, NULL);
  56. /* set vis plugin for playbin2 */
  57. g_object_set (pipeline, "vis-plugin", vis_plugin, NULL);
  58. /* Start playing */
  59. gst_element_set_state (pipeline, GST_STATE_PLAYING);
  60. /* Wait until error or EOS */
  61. bus = gst_element_get_bus (pipeline);
  62. msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
  63. /* Free resources */
  64. if (msg != NULL)
  65. gst_message_unref (msg);
  66. gst_plugin_feature_list_free (list);
  67. gst_object_unref (bus);
  68. gst_element_set_state (pipeline, GST_STATE_NULL);
  69. gst_object_unref (pipeline);
  70. ;
  71. }
  72. </span>

工作流程

首先,我们都知道通过设置GST_PLAY_FLAG_VIS标志可以让playbin2的音频可视化。如果媒体文件里面已经包含了视频,那么这个标志就不起作用。

[objc] view
plain
 copy

  1. /* Set the visualization flag */
  2. g_object_get (pipeline, "flags", &flags, NULL);
  3. flags |= GST_PLAY_FLAG_VIS;
  4. g_object_set (pipeline, "flags", flags, NULL);

如果用户没有指定可视化插件,playbin2会使用goom来做(如果没有这个element就不能进行音频可视化)。本教程剩下的部分会显示如何来发现一个可用的可视化element并指定playbin2使用。

[objc] view
plain
 copy

  1. /* Get a list of all visualization plugins */
  2. list = gst_registry_feature_filter (gst_registry_get_default (), filter_vis_features, FALSE, NULL);

gst_registry_feature_filter()检查GStreamer当前所有注册的element并选择那些filter_vis_features函数返回TRUE的element。

[objc] view
plain
 copy

  1. /* Return TRUE if this is a Visualization element */
  2. static gboolean filter_vis_features (GstPluginFeature *feature, gpointer data) {
  3. GstElementFactory *factory;
  4. if (!GST_IS_ELEMENT_FACTORY (feature))
  5. return FALSE;
  6. factory = GST_ELEMENT_FACTORY (feature);
  7. if (!g_strrstr (gst_element_factory_get_klass (factory), "Visualization"))
  8. return FALSE;
  9. return TRUE;
  10. }

这里牵涉到一点关于GStreamer element的理论:每一个GStreamer在运行时加载的文件都被认为是插件。一个插件可以有多个功能,不同种类的功能。

这个函数简单的丢弃了所有不能批量复现的功能以及不包含“可视化”这个功能的插件。

[objc] view
plain
 copy

  1. /* Print their names */
  2. g_print("Available visualization plugins:\n");
  3. for (walk = list; walk != NULL; walk = g_list_next (walk)) {
  4. const gchar *name;
  5. GstElementFactory *factory;
  6. factory = GST_ELEMENT_FACTORY (walk->data);
  7. name = gst_element_factory_get_longname (factory);
  8. g_print("  %s\n", name);
  9. if (selected_factory == NULL || g_str_has_prefix (name, "GOOM")) {
  10. selected_factory = factory;
  11. }
  12. }

一旦我们有了可视化插件的列表,我们会打印出它们的名字(gst_element_factory_get_longname())并选择一个(比如:GOOM)。

[objc] view
plain
 copy

  1. /* We have now selected a factory for the visualization element */
  2. g_print ("Selected '%s'\n", gst_element_factory_get_longname (selected_factory));
  3. vis_plugin = gst_element_factory_create (selected_factory, NULL);
  4. if (!vis_plugin)
  5. ;

选中的工厂用来生成一个GstElement,通过vis-plugin属性来传给playbin2。

[objc] view
plain
 copy

  1. /* set vis plugin for playbin2 */
  2. g_object_set (pipeline, "vis-plugin", vis_plugin, NULL);

Bingo,就这么简单。

【GStreamer开发】GStreamer播放教程06——可视化音频的更多相关文章

  1. 【GStreamer开发】GStreamer播放教程09——数字音频传输

    目标 本教程展示GStreamer是如何进行数字音频传输的. 介绍 在常见的模拟格式外,高端的音频系统通常都接受数字格式,压缩的非压缩的都能接受.因为音频信号是从电脑传到音箱,用一种更有弹性的形态会更 ...

  2. 【GStreamer开发】GStreamer基础教程16——平台相关的element

    目标 虽然GStreamer是跨平台的framework,但不是所有的element都是在所有平台下都有的.比如,音频和视频的sink都非常依赖于当前的window系统,根据当前的平台需要选择不同的e ...

  3. gstreamer应用开发(播放器)之旅

    GStreamer开发,主要分为两块:应用开发.插件开发. 插件开发人员,通常是编解码库的作者(做出了编解码库后,希望gstreamer能用起来这个库,因此增加这个适配层).芯片原厂人员(将自家的hw ...

  4. 安装gstreamer开发环境

    ubuntu中安装gstreamer开发环境: * 安装gstreamer基本库,工具,以及插件 sudo apt--dev gstreamer-tools gstreamer0.-tools gst ...

  5. 【转】一步一步教你在Ubuntu12.04搭建gstreamer开发环境

    原文网址:http://blog.csdn.net/xsl1990/article/details/8333062 闲得蛋疼    无聊寂寞冷    随便写写弄弄 看到网上蛮多搭建gstreamer开 ...

  6. FLV提取AAC音频单独播放并实现可视化的频谱

    如上图,要实现对FLV直播流中音频的识别,并展示成一个音频相关的动态频谱. 一. 首先了解下什么是声音? 能量波,有频率有振幅,频率高低就是音调,振幅大小就是音量:采样率是对频率采样,采样精度是对幅度 ...

  7. [C#] NAudio 库的各种常用使用方式: 播放 录制 转码 音频可视化

    概述 在 NAudio 中, 常用类型有 WaveIn, WaveOut, WaveStream, WaveFileWriter, WaveFileReader 以及接口: IWaveProvider ...

  8. Apple Watch开发快速入门教程

     Apple Watch开发快速入门教程  试读下载地址:http://pan.baidu.com/s/1eQ8JdR0 介绍:苹果为Watch提供全新的开发框架WatchKit.本教程是国内第一本A ...

  9. 开发快平台(M302I小e开发板系列教程)

    开发快平台(M302I小e开发板系列教程) 开发块平台ESP8266模块相关理解 一. M302I小e开发板源码注释,源码基于:v1.4.0.8-u34.zip 1. user_main.c /*** ...

随机推荐

  1. codevs1504愚蠢的组合数 / RQNOJ愚蠢的组合数

    1504 愚蠢的组合数  时间限制: 2 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 最近老师教了狗狗怎么算组合数,狗狗又 ...

  2. npm 更新包

    方法一手动跟新: 手动修改package.json中依赖包版本,执行npm install --force,强制从远程下载所有包更新本地包 方法二使用第三方插件: npm install -g npm ...

  3. redis 业务锁 not exist 模式

    背景: 业务核心模块只能提交一次,原实现方案 前端加提交限制.后端加数据库业务逻辑判定,结果失效,api站点部署多台负载,切方法需要强求第三方接口 响应时间较慢 ,故放弃lock. 解决方案:redi ...

  4. win10常用快捷键(热键)总结

    win10常用快捷键总结: win+Ctrl+D 创建虚拟桌面 win+Ctrl+左(右)键 切换虚拟桌面 win+Ctrl+F4 关闭虚拟桌面 win+D 隐藏所有窗口/显示所有窗口 win+M 隐 ...

  5. cross socket tcp客户端开发

    cross socket tcp客户端开发 uses Net.SocketAPI, Net.CrossSocket.Base, Net.CrossSocket FCrossTcp: ICrossSoc ...

  6. 创建加载bean的实例

    一.创建实例 工程的结构如下图 1.创建接口 public interface Person { public void setName(String name); public String say ...

  7. Linux中进程的几种状态

    linux是一个多用户,多任务的系统,可以同时运行多个用户的多个程序,就必然会产生很多的进程,而每个进程会有不同的状态. Linux进程状态:R (TASK_RUNNING),可执行状态. 只有在该状 ...

  8. [Java/File]读取日文CSV文件不乱码

    try { StringBuilder sb=new StringBuilder(); sb.append("\nContent in File:'"+filePathname+& ...

  9. cv2.warpAffine 参数详解

    本文链接:https://blog.csdn.net/qq878594585/article/details/81838260本文为作者原创文章,未经同意严禁转载! opencv中的仿射变换在pyth ...

  10. Django HttpResponse与JsonResponse

    本文链接:https://blog.csdn.net/mr_hui_/article/details/86498509 我们编写一些接口函数的时候,经常需要给调用者返回json格式的数据,那么如何返回 ...