1. 问题
使用QML的Camera
+videoRecorder(Camera)
+VideoOutput
实现显示加录像功能。在Ubuntu上运行正常,视频流畅。但是在开发板上(RK3568)上出现明显卡顿,无法正常录像。
2. 解决方案
- 将摄像头数据通过
gstreamer
共享内存到某个位置(shmsink
) - qml中使用
MediaPlayer
显示和保存视频(MediaPlayer可以使用gstreamer
命令)
3. 代码
3.1 shell中共享摄像头数据
gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,format=YUY2, width=640, height=480, framerate=30/1 ! videoconvert ! shmsink socket-path=/tmp/foo sync=true wait-for-connection=false shm-size=10000000
3.2 qml代码(显示+存储)
import QtQuick 2.15
import QtQuick.Window 2.15
import QtMultimedia 5.15
import QtQuick.Controls 2.15Window {width: 1000height: 800y: 0visible: truetitle: qsTr("Hello World")Rectangle{id: _mTopwidth: 500height: 600x: 0visible:truecolor: "blue"//显示MediaPlayer{id: gstreamTestsource: "gst-pipeline: shmsrc socket-path=/tmp/foo ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! videoconvert! autovideosink "Component.onCompleted:{play()}}//录像的输出VideoOutput{width: 500height: 500x: 0fillMode: Image.Stretchvisible: truesource: gstreamTest}}Rectangle{id: _mTop1width: 500height: 600x: 500visible:truecolor: "red"//保存MediaPlayer{id: gstreamTest1source: "gst-pipeline: shmsrc socket-path=/tmp/foo ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! videoconvert ! avenc_mpeg4 ! filesink location=1.mp4"Component.onCompleted:{play()}}}
}
3.3 qml代码(多个窗口显示)
import QtQuick 2.15
import QtQuick.Window 2.15
import QtMultimedia 5.15
import QtQuick.Controls 2.15Window {width: 1000height: 800y: 0visible: truetitle: qsTr("Hello World")Rectangle{id: _mTopwidth: 500height: 600x: 0visible:truecolor: "blue"//显示MediaPlayer{id: gstreamTestsource: "gst-pipeline: shmsrc socket-path=/tmp/foo ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! videoconvert! autovideosink "Component.onCompleted:{play()}}//录像的输出VideoOutput{width: 500height: 500x: 0fillMode: Image.Stretchvisible: truesource: gstreamTest}}Rectangle{id: _mTop1width: 500height: 600x: 500visible:truecolor: "red"//显示MediaPlayer{id: gstreamTest1source: "gst-pipeline: shmsrc socket-path=/tmp/foo ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! videoconvert! autovideosink "Component.onCompleted:{play()}}VideoOutput{width: 500height: 500fillMode: Image.Stretchvisible: truesource: gstreamTest1}}
}
4. 效果
目前在Ubuntu效果不错,开发板中gstreamer
缺少插件,安装插件后再测试效果。