Video conversion

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.

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. Then, open up Qt Creator and create a new Qt Widgets Application project by going to File | New File or Project….
  3. After that, open up 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:
    How to do it…
  4. 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:
    How to do it…
  5. After that, 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. Then, repeat this step for the other push button as well.
  6. After that, open up mainwindow.h and add the following headers to the top:
    #include <QMainWindow>
    #include <QFileDialog>
    #include <QProcess>
    #include <QMessageBox>
    #include <QScrollBar>
    #include <QDebug>
  7. Then, add the following pointers under the public keyword:
    public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
    
      QProcess* process;
      QString outputText;
      QString fileName;
      QString outputFileName;
    
  8. Besides that, we also need to add three extra slot functions under the two functions that Qt created for us previously:
    private slots:
      void on_browseButton_clicked();
      void on_convertButton_clicked();
    
      void processStarted();
      void readyReadStandardOutput();
      void processFinished();
    
  9. Next, open up 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()));
    }
  10. After that, we define what will happen when the Browse button is clicked, which in this case will open up the file dialog 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);
    }
  11. Then, we also define what will happen if the Convert button is clicked. What we are doing here is passing the filenames and arguments to FFmpeg and then the conversion process will be handled externally by FFmpeg:
    How to do it…
  12. Once we are done with that, we will then tell our 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);
    }
  13. Next, we will 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());
    }
  14. Lastly, we 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);
    }
  15. Build and run the project now and you should get a simple yet workable video converter:
    How to do it…

How it works...

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's more…

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.

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

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