本机设备
虚拟机安装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
🆗。