In this recipe, we will create a simple video converter using Qt and FFmpeg, a leading multimedia framework that is free and open source. Although Qt does support playing video files through its widget, it does not support video conversion at the moment. Fear not! You can actually still achieve the same goal by making your program cooperate with another standalone program through the QProcess
class provided by Qt.
Let's make a simple video converter with the following steps:
C:/FFmpeg/
.mainwindow.ui
and we're going to work on the program's user interface. Its UI is very similar to the previous example, except we add an extra text edit widget to the canvas, just below the combo box:AVI
, MP4
, and MOV
:mainwindow.h
and add the following headers to the top:#include <QMainWindow> #include <QFileDialog> #include <QProcess> #include <QMessageBox> #include <QScrollBar> #include <QDebug>
public
keyword:public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); QProcess* process; QString outputText; QString fileName; QString outputFileName;
private slots: void on_browseButton_clicked(); void on_convertButton_clicked(); void processStarted(); void readyReadStandardOutput(); void processFinished();
mainwindow.cpp
and add the following code to the class constructor:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); process = new QProcess(this); connect(process, SIGNAL(started()), this, SLOT(processStarted())); connect(process,SIGNAL(readyReadStandardOutput()), this,SLOT(readyReadStandardOutput())); connect(process, SIGNAL(finished(int)), this, SLOT(processFinished())); }
void MainWindow::on_browseButton_clicked() { QString fileName = QFileDialog::getOpenFileName(this, "Open Video", "", "Video Files (*.avi *.mp4 *.mov)"); ui->filePath->setText(fileName); }
void MainWindow::processStarted() { qDebug() << "Process started."; ui->browseButton->setEnabled(false); ui->fileFormat->setEditable(false); ui->convertButton->setEnabled(false); }
void MainWindow::readyReadStandardOutput() { outputText += process->readAllStandardOutput(); ui->outputDisplay->setText(outputText); ui->outputDisplay->verticalScrollBar()->setSliderPosition(ui->outputDisplay->verticalScrollBar()->maximum()); }
void MainWindow::processFinished() { qDebug() << "Process finished."; if (QFile::exists(outputFileName)) { QMessageBox::information(this, "Success", "Video successfully converted."); } else { QMessageBox::information(this, "Failed", "Failed to convert video."); } ui->browseButton->setEnabled(true); ui->fileFormat->setEditable(true); ui->convertButton->setEnabled(true); }
The QProcess
class provided by Qt is used to start external programs and communicate with them. In this case, we started ffmpeg.exe
located in C:/FFmpeg/bin/
as a process and started communicating with it. We also sent it a set of arguments to tell it what to do when started. The arguments we used in this example are relatively basic; we only told FFmpeg the path to the source image and the output filename. For more information regarding the argument settings available in FFmpeg, check out https://www.ffmpeg.org/ffmpeg.html.
FFmpeg does more than just converting video files. You can also use it to convert audio files and even images. For more information regarding all the formats supported by FFmpeg, check out https://www.ffmpeg.org/general.html#File-Formats.
Other than that, you can also play a video or audio file by running ffplay.exe
, located in C:/FFmpeg/bin
, or print out the information of the video or audio file in human-readable fashion by running ffprobe.exe
. Check out FFmpeg's full documentation at https://www.ffmpeg.org/about.html.
There are lots of things you can do using this method. It means that you're not limited to what Qt provides and you can break out of such limitations by carefully selecting a third-party program that provides what you need. One such example is making your own anti-virus GUI by utilizing the command-line-only anti-virus scanners available on the market, such as Avira ScanCL, Panda Antivirus Command Line Scanner, SAV32CLI, ClamavNet, and so on. You can build your own GUI using Qt and essentially send commands to the anti-virus process to tell it what to do.
3.145.7.208