3

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());
}
Nejat
  • 31,784
  • 12
  • 106
  • 138
Michael
  • 1,018
  • 4
  • 14
  • 30

1 Answers1

3

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
  ...
}
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Thank you that will help me a lot. I understand that /bin/sh calls,in some way ,the Terminal (correct me if I'm wrong) and next argument of function QProcess::start() receives the commands executed by the Terminal in a QStringList. But what is -c supposed to do? and why is process returning a bool? (!process) – Michael Jan 03 '15 at 19:36
  • @Michael `/bin/sh`, accept the `-c` argument with a string. That means you can pass a string to be executed just after the `-c` argument. Although you can also have a shell script and pass the file as the argument. There are other ways to execute the command. You can also have `process.start(cmd);` to execute the command if it's not too complex. In case it's a bit complex you do like in the answer.There are other ways to work with `QProcess` like this one : http://stackoverflow.com/questions/26884734/how-to-run-print-command-lpr-p-programmatically-throgh-root-privilage-in-qt/26885247#26885247 – Nejat Jan 04 '15 at 04:12
  • Thank you very much, I finished my project. – Michael Jan 04 '15 at 23:21