How to do it…

Let's make a simple video converter with the following steps:

  1. Download FFmpeg (static package) from http://ffmpeg.zeranoe.com/builds and extract the contents to C:/FFmpeg/.
  2. Open Qt Creator and create a new Qt Widgets Application project by going to File | New File or Project.
  3. Open mainwindow.uiwe're going to work on the program's user interface. Its UI is very similar to the previous example, except that we add an extra text-edit widget to the canvas, just under the combo box:

  1. Double-click the combo box and a window will appear to edit the combo box. We will add three items to the combo box list by clicking the + button three times, and rename the items AVI, MP4, and MOV:

  1. Right-click on one of the push buttons and select Go to slot…, then click the OK button. A slot function will then be automatically added to your source files. Repeat this step for the other push button as well.
  2. Open mainwindow.h and add the following headers to the top:
#include <QMainWindow>
#include <QFileDialog>
#include <QProcess>
#include <QMessageBox>
#include <QScrollBar>
#include <QDebug>
  1. Add the following pointers under the public keyword:
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QProcess* process;
QString outputText;
QString fileName;
QString outputFileName;
  1. Add three extra slot functions under the two functions that Qt created for us previously in the Converting images recipe:
private slots:
void on_browseButton_clicked();
void on_convertButton_clicked();
void processStarted();
void readyReadStandardOutput();
void processFinished();

  1. Open 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, QProcess::started, this, MainWindow::processStarted);
connect(process, QProcess::readyReadStandardOutput, this, MainWindow::readyReadStandardOutput);
connect(process, QProcess::finished, this,
MainWindow::processFinished);
}
  1. Define what will happen when the Browse button is clicked, which in this case will open up the file dialog to allow us to choose the video file:
void MainWindow::on_browseButton_clicked() {
QString fileName = QFileDialog::getOpenFileName(this, "Open Video", "", "Video Files (*.avi *.mp4 *.mov)");
ui->filePath->setText(fileName);
}
  1. Define what will happen if the Convert button is clicked. Here, we are passing the filenames and arguments to FFmpeg and then the conversion process will be handled externally by FFmpeg:
void MainWindow::on_convertButton_clicked() {
QString ffmpeg = "C:/FFmpeg/bin/ffmpeg";
QStringList arguments;
fileName = ui->filePath->text();
if (fileName != "") {
QFileInfo fileInfo = QFile(fileName);
outputFileName = fileInfo.path() + "/" + fileInfo.completeBaseName();

  1. Check for the file's format; specifically, whether it's .avi, .mp4, or .mov:
        if (QFile::exists(fileName)) {
int format = ui->fileFormat->currentIndex();
if (format == 0) {
outputFileName += ".avi"; // AVI
}
else if (format == 1) {
outputFileName += ".mp4"; // MP4
}
else if (format == 2) {
outputFileName += ".mov"; // MOV
}
  1. Start the conversion using the following code:
            qDebug() << outputFileName << format;
arguments << "-i" << fileName << outputFileName;
qDebug() << arguments;
process->setProcessChannelMode(QProcess::MergedChannels);
process->start(ffmpeg, arguments);
}
  1. Display the message boxes:
        else {
QMessageBox::warning(this, "Failed", "Failed to open video file.");
}
}
else {
QMessageBox::warning(this, "Failed", "No file is selected.");
}
}
  1. Tell the program what to do when the conversion process has started:
void MainWindow::processStarted() {
qDebug() << "Process started.";
ui->browseButton->setEnabled(false);
ui->fileFormat->setEditable(false);
ui->convertButton->setEnabled(false);
}

  1. Write the slot function that gets called during the conversion process whenever FFmpeg returns an output to the program:
void MainWindow::readyReadStandardOutput() {
outputText += process->readAllStandardOutput();
ui->outputDisplay->setText(outputText);
ui->outputDisplay->verticalScrollBar()->setSliderPosition(ui->outputDisplay->verticalScrollBar()->maximum());
}
  1. Define the slot function that gets called when the entire conversion process has been completed:
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);
}
  1. Build and run the project, and you should get a simple, yet workable, video converter:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset