前几天更新了使用树莓派制作家庭监控,一种方式是使用mjpg,网页显示,一种方式是使用系统提供的vlc播放器,两种方式都不是很完美,视频窗口不能嵌入到Qt的窗口内,这样操作起来很不方便,两种窗口独立,显得不完整。本着精益求精的原则,本文实现使用QT加载libvlc库,使用程序代码控制vlc播放器,进行摄像头视频采集。
1.安装libvlc相关库
root@raspberrypi:~# apt install libvlc-dev libvlccore-dev
2.配置QT工程文件
OPENCV_INCLUDE = /usr/include/opencv2
OPENCV_LIB = /usr/lib/arm-linux-gnueabihf
WIRINGPI_INCLUDE = /usr/include
WIRINGPI_LIB = /usr/lib
VLC_INCLUDE = /usr/include/vlc
VLC_LIB = /usr/lib/arm-linux-gnueabihf
INCLUDEPATH += \
$$OPENCV_INCLUDE \
$$WIRING_INCLUDE \
$$VLC_INCLUDE
LIBS += \
$$WIRINGPI_LIB/libwiringPi.so \
$$VLC_LIB/libvlccore.so \
$$VLC_LIB/libvlc.so
3.增加vlc显示
创建QLabel,用于显示video,稍微调整布局。
启动vlc,加载摄像头/dev/video0
libvlc_instance_t* instance = libvlc_new(0, nullptr);
// libvlc_media_t* media = libvlc_media_new_path(instance, "1.mp4");
libvlc_media_t* media = libvlc_media_new_location(instance,"v4l2:///dev/video0");
libvlc_media_player_t* player = libvlc_media_player_new_from_media(media);
libvlc_media_release(media);
libvlc_media_player_set_xwindow(player, m_display_label->winId());
libvlc_media_player_play(player);
注意事项:头文件的包含顺序,libvlc_renderer_discoverer要在libvlc_media_player前面包含,否则编译报错。
#include "libvlc_renderer_discoverer.h"
#include "libvlc_media_player.h"
也可以播放本地文件。
4.显示效果

5.桌面增加图标
制作desktop文件,放到Desktop文件夹下
[Desktop Entry]
Name=camera.desktop
Exec=sudo /home/pi/study/projects/vlc/build-Camera-Desktop-Release/Camera
Icon=/home/pi/study/projects/servo/Camera/home-camera.ico
Type=Application
Terminal=false
Categories=AudioVideo;Player;Recorder;

VLC作为一个不错的开源视频播放器,提供了丰富的功能。可以提供RTMP等推流,也可以拉流,实现网络点播直播等。在线文档https://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc.html