03 Qt6.2.3获取摄像头录制YUV文件,逐帧获取录制,使用QVideoSink

Qt6.2.3获取摄像头录制YUV文件,逐帧获取录制,使用QVideoSink

  • 必须要使用到QVideoSink,相应的依赖模块有:QMediaCaptureSession、QMediaDevices、QCamera、QVideoFrame

  • 具体的使用方法是:
    1、创建一个QMediaCaptureSession:
    QMediaCaptureSession m_captureSession;
    2、设置摄像头:
    m_captureSession.setCamera(new QCamera(QMediaDevices::defaultVideoInput()));
    3、设置YUV逐帧输出:
    m_captureSession.setVideoSink(new QVideoSink m_videoSink);
    4、设置YUV格式:
    void QVideoSink::setVideoFrame(const QVideoFrame &frame)
    5、主动获取一帧YUV数据:
    QVideoFrame QVideoSink::videoFrame() const
    6、被动接收每一帧:
    connect(m_videoSink, &QVideoSink::videoFrameChanged, YourClass, yourParseFunction);
    7、获取帧数据:
    videoFrame.map(QVideoFrame::ReadOnly);
    m_file.write((const char *)videoFrame.bits(0和1), videoFrame.mappedBytes(0和1));

  • mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCameraFormat>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_camera = new QCamera(QMediaDevices::defaultVideoInput());
    m_captureSession.setCamera(m_camera);

    m_captureSession.setVideoSink(&m_videoSink);
    connect(&m_videoSink, &QVideoSink::videoFrameChanged, this, &MainWindow::on_frame_changed);

    m_camera->start();
    m_file.setFileName("test.nv12");
    m_file.open(QIODevice::WriteOnly | QIODevice::Truncate);
}

MainWindow::~MainWindow()
{
    m_camera->stop();
    m_file.close();

    delete ui;
}

void MainWindow::on_frame_changed(const QVideoFrame &frame)
{
    QVideoFrame videoFrame = frame;
    /**
     * 我的电脑上返回的格式是Format_NV12:18,每个像素1.5字节,一帧1382400字节,
     * 如果你需要用别的YUV或者RGB格式,则需要自己设置QVideoSink或者QCamera格式
     */
    QVideoFrameFormat::PixelFormat pixelFormat = videoFrame.pixelFormat();
    int width = videoFrame.width();
    int height = videoFrame.height();
    int planeCount = videoFrame.planeCount();
    uchar *pdata = nullptr;
    int len = 0;

    videoFrame.map(QVideoFrame::ReadOnly);

    for (int i = 0; i < planeCount; i++) {
        pdata = videoFrame.bits(i);
        len = videoFrame.mappedBytes(i);
        m_file.write((const char *)pdata, len);
    }

    frame_num++;
    qDebug("%d, %d, %d, %d, %d, %p, %d", frame_num,
           pixelFormat, width, height, planeCount, pdata, len);

    videoFrame.unmap();
}
  • mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCamera>
#include <QMediaCaptureSession>
#include <QMediaDevices>
#include <QVideoSink>
#include <QVideoFrame>
#include <QFile>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QMediaCaptureSession m_captureSession;
    QVideoSink m_videoSink;
    QCamera *m_camera;
    QFile m_file;
    int frame_num = 0;

private slots:
    void on_frame_changed(const QVideoFrame &frame); // 一帧视频到来的信号
};
#endif // MAINWINDOW_H
  • main.cpp:
/**
 * \brief Qt6.2.3要逐帧获取YUV需要使用QVideoSink和QMediaCaptureSession
 * \details 编译运行后,窗口弹出即开始录制YUV文件,关闭窗口后文件保存
 * \note 保存的文件在本工程文件夹上级目录中的debug*目录下的test.nv12
 *       我的电脑摄像头默认输出是YUV NV12格式1280 * 720
 *       播放方式是安装ffmpeg软件后,在test.nv12文件目录下使用命令:
 *       ffplay -f rawvideo -pix_fmt nv12 -video_size 1280x720 test.nv12
 */

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
  • 005_yuv_input_save_file.pro:
QT       += core gui
QT       += multimedia

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
  • mainwindow.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QPushButton" name="pushButton">
      <property name="text">
       <string>窗口不要开太久!开几秒就够了,否则存储的YUV文件会把你的硬盘塞满!</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用和,您提到的是如何使用ffmpeg和Qt实现音频播放。根据引用,在之前的章节中,已经介绍了如何使用ffmpeg对视频进行解码和渲染,但忽略了音频数据。因此,本文将介绍如何使用ffmpeg解码音频,并制作一个音乐播放器。根据引用,该音乐播放器的特点是只解码音频数据,忽略视频图像数据,主要演示了ffmpeg的基本使用过程,包括如何解码音频数据、转换音频数据格式,并通过QAudioOutput进行播放。 根据引用,ffmpeg提供了一个例程来实现硬件解码,可以参考该例程来进行实现。您可以在ffmpeg的文档目录下找到该例程的代码文件,路径为ffmpeg\doc\examples\decode_audio.c。 因此,使用ffmpeg和Qt可以实现音频播放功能,您可以按照引用中的解码流程进行实现,包括解码音频数据、转换音频数据格式,并使用QAudioOutput进行播放。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [QT软件开发-基于FFMPEG设计视频播放器-解码音频(三)](https://blog.csdn.net/xiaolong1126626497/article/details/126836582)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值