qt_ffmpeg_client/mainwindow.cpp

165 lines
5.0 KiB
C++
Raw Normal View History

2024-09-09 11:33:41 +00:00
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QFileDialog>
#include <QFileInfo>
#include <iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 选择修改分辨率
connect(ui->en_mdf_res, SIGNAL(stateChanged(int)), this, SLOT(en_mdf_res_state_change(int)));
en_mdf_res_state_change(Qt::Unchecked);
// bt_browse_press
connect(ui->bt_browse, SIGNAL(clicked()), this, SLOT(press_bt_browse()));
// bt_start_change
connect(ui->bt_start_change, SIGNAL(clicked()), this, SLOT(ffmpeg_run()));
//ffmpeg_info
connect(&ffmpeg_process, SIGNAL(readyReadStandardOutput()), this, SLOT(ffmpeg_info_read()));
// def_cfg
connect(ui->bt_def_cfg, SIGNAL(clicked()), this, SLOT(def_cfg()));
// hor_change
connect(ui->bt_hor_change, SIGNAL(clicked()), this, SLOT(hor_change_event()));
// ver_change
connect(ui->bt_ver_change, SIGNAL(clicked()), this, SLOT(ver_change_event()));
}
void MainWindow::hor_change_event()
{
ui->hor_val->setText("-1");
}
void MainWindow::ver_change_event()
{
ui->ver_val->setText("-1");
}
void MainWindow::ffmpeg_info_read()
{
cout << "ffmpeg_info_read" << endl;
QString ffmpeg_info = ffmpeg_process.readAllStandardOutput();
if (ffmpeg_info.isEmpty())
{
cout << "No output from ffmpeg" << endl;
}
else
{
ui->lb_info->setText(ffmpeg_info);
ui->lb_info->repaint();
cout << "ffmpeg_info: " << ffmpeg_info.toStdString() << endl;
}
}
void MainWindow::def_cfg(){
en_mdf_res_state_change(Qt::Checked);
ui->en_mdf_res->setChecked(true);
ui->hor_val->setText("640");
ui->ver_val->setText("-1");
ui->cb_fps_change->setChecked(true);
ui->le_fps_val->setText("5");
}
void MainWindow::ffmpeg_run()
{
cout << "ffmpeg_run" << endl;
// deal cmd
QString cmd = "./ffmpeg.exe -i ";
if (ui->le_src_file->text().isEmpty())
{
cout << "src file is empty" << endl;
return;
}
cmd += ui->le_src_file->text();
if (ui->cb_fps_change->isChecked())
{
cmd += " -r ";
cmd += ui->le_fps_val->text();
}
if (ui->en_mdf_res->isChecked())
{
cmd += " -vf scale=";
cmd += ui->hor_val->text();
cmd += ":";
cmd += ui->ver_val->text();
}
if (ui->le_dest_file->text().isEmpty())
{
cout << "dest file is empty" << endl;
return;
}
cmd += " ";
cmd += ui->le_dest_file->text();
cmd += ui->cb_dest_file_end->currentText();
cout << "cmd: " << cmd.toStdString() << endl;
ffmpeg_process.setProcessChannelMode(QProcess::MergedChannels);
ffmpeg_process.start(cmd);
if (!ffmpeg_process.waitForStarted())
{
cout << "ffmpeg_run failed to start" << endl;
return;
}
while (!ffmpeg_process.waitForFinished())
{
// QString ffmpeg_info = ffmpeg_process.readAll();
// cout << "ffmpeg_info: " << ffmpeg_info.toStdString() << endl;
// ui->lb_info->setText(ffmpeg_info);
// _sleep(1);
}
QMessageBox::information(this, "提示", "转换完成!");
// QString ffmpeg_info = ffmpeg_process.readAllStandardOutput();
// if (ffmpeg_info.isEmpty())
// {
// cout << "No output from ffmpeg" << endl;
// }
// else
// {
// ui->lb_info->setText(ffmpeg_info);
// cout << "ffmpeg_info: " << ffmpeg_info.toStdString() << endl;
// }
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::en_mdf_res_state_change(int state)
{
cout << "en_mdf_res_state_change" << endl;
if (state == Qt::Checked)
{
this->ui->hor_val->setFocusPolicy(Qt::StrongFocus);
this->ui->hor_val->setEnabled(true);
this->ui->hor_val->setStyleSheet("background-color: white;");
this->ui->ver_val->setFocusPolicy(Qt::StrongFocus);
this->ui->ver_val->setEnabled(true);
this->ui->ver_val->setStyleSheet("background-color: white;");
}
else
{
this->ui->hor_val->setFocusPolicy(Qt::NoFocus);
this->ui->hor_val->setEnabled(false);
this->ui->hor_val->setText("");
this->ui->hor_val->setStyleSheet("background-color: gray;");
this->ui->ver_val->setFocusPolicy(Qt::NoFocus);
this->ui->ver_val->setEnabled(false);
this->ui->ver_val->setText("");
this->ui->ver_val->setStyleSheet("background-color: gray;");
}
}
void MainWindow::press_bt_browse()
{
QString select_file = QFileDialog::getOpenFileName(this, tr("选择一个目录"), tr("*.*"));
if (!select_file.isEmpty())
{
// 用户选择了一个目录,处理选择的路径
// 这里可以是输出到控制台或其他操作
ui->le_src_file->setText(select_file);
QFileInfo file_info(select_file);
QString baseName = file_info.completeBaseName(); // 获取不带扩展名的文件名
ui->le_dest_file->setText(file_info.path()+tr("/")+baseName);
}
}