Search code examples
c++qtyoutubeyoutube-dlqprogressbar

How to link a QProgressBar to a Terminal process percentage?


So I designed a very simple dialog using Qt which is meant to download videos from youtube and convert them to either mp4 or mp3, everything using youtube-dl command (I use system() to call youtube-dl). Yes, I'm a Linux user. It's working fine, but I would like to show the progress of the download in my UI, like the one showing in the Terminal when I call youtube-dl directly from there. Any constructive critique about the whole code would be very appreciated.

//ytdialog.h
#ifndef YTDIALOG_H
#define YTDIALOG_H
#include <iostream>
using namespace std;
#include <QDialog>

class QPushButton;
class QLineEdit;
class QLabel;

class ytDialog : public QDialog
{
  Q_OBJECT
  public:
    ytDialog(QWidget *parent = 0);
  private slots:
    void videoOutput(); //download the video that corresponds to the link in   link(QLineEdit)
    void audioOutput(); //download the video and converts it to mp3
    void enableButtons(); //enable the buttons when link is not empty
  private:
    QLabel *linkLabel; //Just a Label
    QPushButton *video; //Button to Download video
    QPushButton *audio; //Button to Download video and convert it to mp3
    QLineEdit *link; //Input link
};

#endif

//ytdialog.cpp **Just In case You need it**
#include <QtWidgets>

#include "ytdialog.h"

ytDialog::ytDialog(QWidget *parent)
      : QDialog(parent)
{
  linkLabel = new QLabel("Link: ");
  link = new QLineEdit;
  video = new QPushButton("Video Output");
  audio = new QPushButton("Audio Output");

  link->setMinimumWidth(400);
  video->setEnabled(false);
  audio->setEnabled(false);

  connect(link, SIGNAL(textChanged(const QString &)),
      this, SLOT(enableButtons()));
  connect(video, SIGNAL(clicked()),
      this, SLOT(videoOutput()));
  connect(audio, SIGNAL(clicked()),
      this, SLOT(audioOutput()));

  QHBoxLayout *linkLayout = new QHBoxLayout;
  linkLayout -> addWidget(linkLabel);
  linkLayout -> addWidget(link);

  QHBoxLayout *buttonsLayout = new QHBoxLayout;
  buttonsLayout -> addWidget(video);
  buttonsLayout -> addWidget(audio);

  QVBoxLayout *main = new QVBoxLayout;
  main -> addLayout(linkLayout);
  main -> addLayout(buttonsLayout);

  setLayout(main);
  setWindowTitle("Download Youtube");
}
void ytDialog::enableButtons()
{
   if (!link->text().isEmpty())
   {
     video->setEnabled(true);
     audio->setEnabled(true);
   }
   else
   {
     video->setEnabled(false);
     audio->setEnabled(false);
   }
}
void ytDialog::videoOutput()
{
  string cmd = "youtube-dl -o '/home/rodrigo/Videos/%(title)s.%(ext)s' " + link-    >text().toStdString();
  system(cmd.c_str());
}

void ytDialog::audioOutput()
{
  string cmd = "youtube-dl -x --audio-format mp3 -o '/home/rodrigo/Music/%(title)s.%    (ext)s' " + link->text().toStdString();
  system(cmd.c_str());
}

Solution

  • You can use QProcess to execute the command. Then you can use readyReadStandardOutput signal to read the command output messages in a slot and process the text to extract the progress percentage :

    In your constructor :

    process = new QProcess();
    
    connect (process, SIGNAL(readyReadStandardOutput()), this, SLOT(readyRead()));
    connect (process, SIGNAL(finished (int, QProcess::ExitStatus)), this, SLOT(finished()));
    

    videoOutput slot :

    void ytDialog::videoOutput()
    {
      string cmd = "youtube-dl -o '/home/rodrigo/Videos/%(title)s.%(ext)s' " + link-    >text().toStdString();
      process.start("/bin/sh", QStringList()<< "-c" << cmd);
    }
    

    readyRead slot :

    ytDialog::readyRead()
    {
      if (!process)
        return;
    
      QString str = process->readAllStandardOutput();
    
      //Extract the progress percentage and emit a signal to update the progress bar
      ...
    }