新增大窗口播放视频按键,窗口大小根据解码视频分辨率自动调整,关掉大窗口回到原来位置播放视频

This commit is contained in:
2024-09-05 09:25:01 +08:00
parent 4bd2d7eab5
commit 9a49eab4af
13 changed files with 2228 additions and 92 deletions

View File

@@ -4,6 +4,7 @@
FFmpegDecoder::FFmpegDecoder(QObject* parent) :
QThread(parent),
videoLabel(nullptr),
resolutionEdit(nullptr),
abort(false),
restart(false),
formatContext(nullptr),
@@ -33,32 +34,49 @@ FFmpegDecoder::~FFmpegDecoder()
void FFmpegDecoder::processVideo(int itemIndex)
{
#if 1
int width = 720;
int height = 1280;
#elif 1
int width = 640;
int height = 480;
QLabel* originalLabel = this->videoLabel;
if ((FocusWindowWidth != 0) && (FocusWindowHeight != 0)) {
qDebug() << "------ processVideo";
FocusWindowDialog* dialog = nullptr;
if (FocusWindowWidth * 16 == FocusWindowHeight * 9) {
dialog = new FocusWindowDialog(nullptr, QSize(540, 960));
}
else if (FocusWindowWidth * 9 == FocusWindowHeight * 16) {
dialog = new FocusWindowDialog(nullptr, QSize(960, 540));
}
else if (FocusWindowWidth * 4 == FocusWindowHeight * 3) {
dialog = new FocusWindowDialog(nullptr, QSize(480, 640));
}
else if (FocusWindowWidth * 3 == FocusWindowHeight * 4) {
dialog = new FocusWindowDialog(nullptr, QSize(640, 480));
}
else {
qDebug() << "------ Other scaled resolutions use 480x640";
dialog = new FocusWindowDialog(nullptr, QSize(480, 640));
}
#if 0
// 将视频显示的 QLabel 切换为对话框内的 videoDisplayLabel
this->videoLabel = dialog->videoDisplayLabel;
if ((dialog->exec() == QDialog::Accepted) || (dialog->exec() == QDialog::Rejected)) {
this->videoLabel = originalLabel;
}
#else
int width = 480;
int height = 640;
mutex.lock();
this->videoLabelTemp = dialog->videoDisplayLabel; // 更新临时标签
this->videoLabelChanged = true; // 设置标志位,表示标签已更改
mutex.unlock();
if ((dialog->exec() == QDialog::Accepted) || (dialog->exec() == QDialog::Rejected)) {
mutex.lock();
this->videoLabelTemp = originalLabel; // 还原回原始标签
this->videoLabelChanged = true; // 设置标志位
mutex.unlock();
}
#endif
qDebug() << "------ processVideo";
if (width * 16 == height * 9) {
FocusWindowDialog* dialog = new FocusWindowDialog(nullptr, QSize(540, 960));
dialog->exec();
delete dialog;
}
else if (width * 9 == height * 16) {
FocusWindowDialog* dialog = new FocusWindowDialog(nullptr, QSize(960, 540));
dialog->exec();
}
else if (width * 4 == height * 3) {
FocusWindowDialog* dialog = new FocusWindowDialog(nullptr, QSize(480, 640));
dialog->exec();
}
else if (width * 3 == height * 4) {
FocusWindowDialog* dialog = new FocusWindowDialog(nullptr, QSize(640, 480));
dialog->exec();
else {
qDebug() << "------ Please wait for the video to be decoded and rendered before clicking";
}
}
@@ -94,7 +112,7 @@ void FFmpegDecoder::run()
{
QFile file(filePath);
qint64 fileSize = 0;
QLabel* currentVideoLabel = videoLabel;
while (!isInterruptionRequested()) {
mutex.lock();
while (!restart && !abort) {
@@ -105,7 +123,7 @@ void FFmpegDecoder::run()
qDebug() << "Decoder thread aborting";
break;
}
QLabel* currentVideoLabel = videoLabel;
/*QLabel* currentVideoLabel = videoLabel;*/
QSize labelSize = currentVideoLabel->size();
mutex.unlock();
@@ -153,9 +171,18 @@ void FFmpegDecoder::run()
qWarning() << "Error during decoding";
break;
}
mutex.lock();
if (videoLabelChanged) {
currentVideoLabel = videoLabelTemp; // 更新 currentVideoLabel
videoLabelChanged = false; // 重置标志位
labelSize = currentVideoLabel->size();
}
mutex.unlock();
QImage img = avFrameToQImage(frame);
QImage scaledImage = img.scaled(labelSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
currentVideoLabel->setPixmap(QPixmap::fromImage(scaledImage));
//currentVideoLabel->setPixmap(QPixmap::fromImage(scaledImage));
QMetaObject::invokeMethod(currentVideoLabel, "setPixmap", Qt::QueuedConnection, Q_ARG(QPixmap, QPixmap::fromImage(scaledImage)));
QThread::msleep(10); // Simulate 25 FPS frame rate
}
}
@@ -266,12 +293,18 @@ QImage FFmpegDecoder::avFrameToQImage(AVFrame* frame)
{
int width = frame->width;
int height = frame->height;
// 这里注意切换镜头后是否改变分辨率去改变 isGotResolution
if (!isGotResolution && (width != 0) && (height != 0)) {
isGotResolution = true;
FocusWindowWidth = width;
FocusWindowHeight = height;
}
QString resolutionText = QString::number(width) + " x " + QString::number(height);
resolutionEdit->setText(resolutionText);
//resolutionEdit->setText(resolutionText);
QMetaObject::invokeMethod(resolutionEdit, "setText", Qt::QueuedConnection, Q_ARG(QString, resolutionText));
//qDebug() << "H264 video resolution: Width =" << frame->width << ", Height =" << frame->height;
AVPixelFormat pixFmt = (AVPixelFormat)frame->format;
if (!swsContext) {
swsContext = sws_getContext(width, height, pixFmt, width, height, AV_PIX_FMT_RGB24, SWS_BILINEAR, nullptr, nullptr, nullptr);
if (!swsContext) {

View File

@@ -56,9 +56,12 @@ private:
QWaitCondition condition;
QString filePath;
QLabel* videoLabel;
QLineEdit* resolutionEdit;
QLabel* videoLabelTemp; // 临时存储新标签
QLineEdit* resolutionEdit;
bool videoLabelChanged = false;
bool abort;
bool restart;
bool isGotResolution = false;
AVFormatContext* formatContext;
AVCodecContext* codecContext;
@@ -66,9 +69,10 @@ private:
AVPacket* packet;
SwsContext* swsContext;
int videoStreamIndex;
int FocusWindowWidth = 0;
int FocusWindowHeight = 0;
RingBuffer* ringBuffer;
//QLabel* singleFrameLabel; // 用于解码单帧的标签
};
#endif // FFMPEGDECODER_H

View File

@@ -1,3 +1,4 @@
// FocusWindow.h
#include <QDialog>
#include <QVBoxLayout>
#include <QLabel>
@@ -8,12 +9,13 @@ class FocusWindowDialog : public QDialog
Q_OBJECT
public:
QLabel* videoDisplayLabel;
FocusWindowDialog(QWidget* parent = nullptr, const QSize& labelSize = QSize(480, 640)) : QDialog(parent)
{
QVBoxLayout* layout = new QVBoxLayout(this);
QLabel* label = new QLabel("1111 This is a new window opened by clicking the new button.", this);
label->setFixedSize(labelSize); // 设置QLabel的固定大小
layout->addWidget(label);
videoDisplayLabel = new QLabel(this);
videoDisplayLabel->setFixedSize(labelSize); // 设置QLabel的固定大小
layout->addWidget(videoDisplayLabel);
QPushButton* closeButton = new QPushButton("Close", this);
layout->addWidget(closeButton);