1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#include "youtubedownloader.h"
#include <QStringList>
#include <QFileInfo>
#include <QVariant>
YouTubeDownloader::YouTubeDownloader(QObject* root, Songs* songs, QObject *parent) :
QObject(parent)
, downloadProcess(parent)
, convertProcess(parent)
, cclive("cclive")
, ffmpeg("ffmpeg")
{
this->songs = songs;
this->root = root;
this->create_download_dir();
// Connect signals
QObject::connect(&(this->downloadProcess), SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(start_conversion()));
QObject::connect(&(this->convertProcess), SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(add_file_converted()));
}
void YouTubeDownloader::start_download(QString url)
{
if(this->downloadProcess.state() == QProcess::NotRunning){
QStringList arguments;
arguments << "-W" << "-f" << "best" << "--filename-format" << "%t";
arguments << "--output-dir" << this->downloadDir->absolutePath() << url;
this->downloadProcess.start(this->cclive, arguments);
}else{
this->queued << url;
}
}
void YouTubeDownloader::start_conversion()
{
QStringList files = this->downloadDir->entryList(QStringList("*"), QDir::Files, QDir::Time);
if(files.size() > 0){
QFileInfo info(files[0]);
if(info.suffix() != "mp3"){
QString filename = this->downloadDir->filePath(files[0]);
QString newfile = filename + ".mp3";
QStringList arguments;
arguments << "-y" << "-i" << filename << "-vn" << "-acodec" << "libmp3lame" << "-ac";
arguments << "2" << "-ab" << "160k" << "-ar" << "48000" << newfile;
videofile = filename;
newsong = newfile;
this->convertProcess.start(this->ffmpeg, arguments);
}else{
this->add_file_converted();
}
}
}
void YouTubeDownloader::add_file_converted()
{
if(this->videofile != "" && this->newsong != ""){
this->downloadDir->remove(this->videofile);
this->songs->append_song(this->newsong);
this->videofile = "";
this->newsong = "";
QMetaObject::invokeMethod(root, "show_notification", Q_ARG(QVariant, "Song Downloaded"));
}
if(!queued.isEmpty()){
this->start_download(this->queued.takeFirst());
}
}
void YouTubeDownloader::create_download_dir(){
QString tempPath = QDir::tempPath();
this->downloadDir = new QDir(QDir(tempPath).filePath("umedia/download"));
if(!this->downloadDir->exists()){
this->downloadDir->mkpath(this->downloadDir->absolutePath());
}
}
|