【GStreamer】GStreamer播放本地视频

本机设备

虚拟机安装Ubuntu20.04

GStreamer播放本地视频

以下是使用GStreamer播放视频的基本步骤:

安装GStreamer

首先,需要在系统上安装GStreamer。安装方法取决于操作系统。以Ubuntu为例,使用以下命令安装GStreamer及其相关插件:

sudo apt-get update
sudo apt-get install gstreamer1.0-tools gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly

检查安装

~/Videos$ gst-inspect-1.0 --version
gst-inspect-1.0 version 1.16.3
GStreamer 1.16.3
https://launchpad.net/distros/ubuntu/+source/gstreamer1.0
~/Videos$ gst-inspect-1.0 --plugin
vmnc:  vmncdec: VMnc video decoder
shm:  shmsrc: Shared Memory Source
shm:  shmsink: Shared Memory Sink
autoconvert:  autoconvert: Select convertor based on caps
autoconvert:  autovideoconvert: Select color space convertor based on caps
ttmlsubs:  ttmlparse: TTML subtitle parser
ttmlsubs:  ttmlrender: TTML subtitle renderer
openexr:  openexrdec: OpenEXR decoder
gaudieffects:  burn: Burn
...

显示本地视频命令

在GStreamer中显示视频通常涉及创建一个包含源(source)、解码器(decoder)、视频转换(video conversion,如果需要的话)和视频汇(video sink)的管道(pipeline)。视频汇是负责将视频帧渲染到显示设备(如屏幕)上的元素。

GStreamer命令行示例,用于播放并显示视频文件:

gst-launch-1.0 filesrc location=/path/to/your/video.mp4 ! decodebin ! videoconvert ! autovideosink
  • filesrc 是一个源元素,用于从指定位置读取文件。 location 属性设置了视频文件的路径。
  • !是管道分隔符,用于连接不同的GStreamer元素。
  • decodebin是一个特殊的元素,它能够自动检测视频流中的编码格式,并调用适当的解码器进行解码。
  • videoconvert用于将视频帧转换为适合显示设备的格式。虽然在一些情况下这不是必需的(因为autovideosink可能会自动处理转换),但它在确保兼容性和优化性能方面通常是一个好习惯。
  • autovideosink 是一个视频汇元素,它会自动选择最适合当前环境的视频输出方法(如X Window System、Wayland、DirectFB、Windows GDI等)。

显示本地视频示例

~/Videos$ gst-launch-1.0 filesrc location=ROBOT.mp4 ! decodebin ! videoconvert ! autovideosink
Setting pipeline to PAUSED ...
Pipeline is PREROLLING ...
Missing element: H.264 (Main Profile) decoder
WARNING: from element /GstPipeline:pipeline0/GstDecodeBin:decodebin0: Delayed linking failed.
Additional debug info:
./grammar.y(506): gst_parse_no_more_pads (): /GstPipeline:pipeline0/GstDecodeBin:decodebin0:
failed delayed linking some pad of GstDecodeBin named decodebin0 to some pad of GstVideoConvert named videoconvert0
ERROR: from element /GstPipeline:pipeline0/GstDecodeBin:decodebin0/GstQTDemux:qtdemux0: Internal data stream error.
Additional debug info:
qtdemux.c(6619): gst_qtdemux_loop (): /GstPipeline:pipeline0/GstDecodeBin:decodebin0/GstQTDemux:qtdemux0:
streaming stopped, reason not-linked (-1)
ERROR: pipeline doesn't want to preroll.
Setting pipeline to NULL ...
Freeing pipeline ...

错误信息表明GStreamer安装中缺少H.264(Main Profile)解码器。这通常是因为没有安装包含必要编解码器插件的GStreamer包。在大多数Linux发行版中,GStreamer的编解码器插件是分开的包,因为它们可能受到专利和版权法律的限制。

需要安装包含H.264解码器的GStreamer插件包。这个包的名字在不同的Linux发行版中可能有所不同。以下是一些常见Linux发行版中可能需要的包名:

# Ubuntu/Debian:
sudo apt-get install gstreamer1.0-plugins-ugly gstreamer1.0-libav

gstreamer1.0-plugins-ugly 包含了一些可能受到版权或专利限制的编解码器,包括H.264。gstreamer1.0-libav 提供了对FFmpeg编解码器的支持,这些编解码器也可以处理H.264。
再运行~/Videos$ gst-launch-1.0 filesrc location=ROBOT.mp4 ! decodebin ! videoconvert ! autovideosink🆗。
在这里插入图片描述

学习参考资料

  1. gstreamer_tutorial
在C语言中,使用GStreamer 1.0库的`uridecodebin`元素来播放本地视频涉及到创建一个GStreamer管道,该管道会从指定的URI读取视频数据,解码并显示。以下是一个简单的示例代码,展示了如何使用GStreamer库来播放本地视频文件: ```c #include <gst/gst.h> int main(int argc, char *argv[]) { GstElement *pipeline, *uridecodebin, *videoconvert, *autovideosink; GstBus *bus; GstMessage *msg; gchar *video_uri = "file:///path/to/your/video.mp4"; // 替换为你的视频文件路径 /* 初始化GStreamer */ gst_init(&argc, &argv); /* 创建新管道 */ pipeline = gst_pipeline_new("test-pipeline"); /* 创建uridecodebin元素 */ uridecodebin = gst_element_factory_make("uridecodebin", "uridecodebin"); g_object_set(G_OBJECT(uridecodebin), "uri", video_uri, NULL); /* 创建其他需要的元素 */ videoconvert = gst_element_factory_make("videoconvert", "videoconvert"); autovideosink = gst_element_factory_make("autovideosink", "autovideosink"); /* 将uridecodebin的视频输出链接到videoconvert */ g_object_set(G_OBJECT(uridecodebin), "video-sink", videoconvert, NULL); /* 将videoconvert的输出链接到autovideosink */ gst_bin_add_many(GST_BIN(pipeline), uridecodebin, videoconvert, autovideosink, NULL); if (gst_element_link_many(uridecodebin, videoconvert, autovideosink, NULL) != TRUE) { g_printerr("Elements could not be linked.\n"); gst_object_unref(GST_OBJECT(pipeline)); return -1; } /* 将uridecodebin添加到管道 */ gst_bin_add(GST_BIN(pipeline), uridecodebin); /* 设置播放状态 */ g_print("Now set pipeline to playing and activate ghost pads\n"); gst_element_set_state(pipeline, GST_STATE_PLAYING); /* 运行消息循环,等待消息 */ bus = gst_element_get_bus(pipeline); do { msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS); /* 检查消息类型 */ if (msg != NULL) { GError *err; gchar *debug_info; /* 处理错误消息 */ switch (GST_MESSAGE_TYPE(msg)) { case GST_MESSAGE_ERROR: gst_message_parse_error(msg, &err, &debug_info); g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME(msg->src), err->message); g_printerr("Debugging information: %s\n", debug_info ? debug_info : "none"); g_clear_error(&err); g_free(debug_info); gst_element_set_state(pipeline, GST_STATE_NULL); break; case GST_MESSAGE_EOS: g_print("End-Of-Stream reached.\n"); break; case GST_MESSAGE_STATE_CHANGED: /* 消息可以被忽略 */ break; default: /* 不知道如何处理的其他消息 */ break; } gst_message_unref(msg); } } while (msg != NULL); /* 清理 */ gst_object_unref(GST_OBJECT(bus)); gst_element_set_state(pipeline, GST_STATE_NULL); gst_object_unref(GST_OBJECT(pipeline)); return 0; } ``` 在运行此代码之前,请确保已经安装了GStreamer库,并且在编译时链接了正确的GStreamer插件。以下是一个编译示例(假设你的源代码文件名为`play_video.c`): ```bash gcc play_video.c -o play_video $(pkg-config --cflags --libs gstreamer-1.0) ``` 请替换`/path/to/your/video.mp4`为你的视频文件路径。程序会创建一个播放本地视频文件的GStreamer管道,并且在终端输出相应的调试信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Eternal-Student

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值