/
youtubedl-helper.hpp
294 lines (287 loc) · 9.37 KB
/
youtubedl-helper.hpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#ifndef YOUTUBEDLHELPER_HPP
#define YOUTUBEDLHELPER_HPP
#include <QtCore/QObject>
#include <QString>
#include <QStandardPaths>
#include <QDebug>
#include <QProcess>
#include <QByteArray>
#include <QFile>
#include <QStandardPaths>
#include <QFileDevice>
#include <QFileInfo>
#include <QDateTime>
class ythelper : public QObject
{ Q_OBJECT
Q_PROPERTY(int searchResultNumber READ searchResultNumber WRITE setSearchResultNumber)
public:
QString reqUrl;
QString streamUrl;
QString streamTitle;
QString errorMsg;
QString parameter;
QProcess streamProcess;
QProcess titleProcess;
QProcess updateBinary;
QString data_dir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
QString music_dir = QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
int searchResultNumber() { return m_searchResultNumber; }
void setSearchResultNumber(int nr) { m_searchResultNumber = nr; }
// bool ffmpegAvailable = false;
private:
QString _oggAudio;
QString _opusAudio;
QString _fullHdVideo;
QString _fullHdAudio;
QString _format;
QProcess oggProcess;
QProcess opusProcess;
QProcess fullHdProcess;
QProcess searchProcess;
int m_searchResultNumber = 11;
signals:
void streamUrlChanged(QString changedUrl);
void sTitleChanged(QString sTitle);
void updateComplete();
void downloadComplete();
void error(QString message);
void oggAudioChanged();
void opusAudioChanged();
void fullHdChanged();
void ytSearchResultsChanged(QString ytSearchResultsJson);
public slots:
void setUrl(QString url)
{
reqUrl = url;
}
void setParameter(QString param)
{
parameter = param;
}
QString getReqUrl()
{
return reqUrl;
}
// bool getFfmpegAvailable()
// {
// return ffmpegAvailable;
// }
QString getOggAudioUrl()
{
return _oggAudio;
}
QString getOpusAudioUrl()
{
return _opusAudio;
}
QString getFullHdVideoUrl()
{
return _fullHdVideo;
}
QString getFullHdAudioUrl()
{
return _fullHdAudio;
}
void checkAndInstall()
{
QFile ytdlBin;
QFile ytdlBinOrig("/usr/share/harbour-videoPlayer/qml/pages/helper/youtube-dl");
ytdlBin.setFileName(data_dir + "/youtube-dl");
if (!ytdlBin.exists()) {
if (ytdlBinOrig.exists()) {
ytdlBinOrig.copy(data_dir + "/youtube-dl");
}
else {
QProcess *ytdlBinDownload;
ytdlBinDownload->start("curl -L https://yt-dl.org/downloads/latest/youtube-dl -o " + data_dir + "/youtube-dl");
ytdlBinDownload->waitForFinished();
}
ytdlBin.setFileName(data_dir + "/youtube-dl");
}
QFileInfo ytdlFileInfo; ytdlFileInfo.setFile(ytdlBin);
QFileInfo ytdlBinOrigInfo; ytdlBinOrigInfo.setFile(ytdlBinOrig);
QDateTime created = ytdlFileInfo.lastModified();
QDateTime origTime = ytdlBinOrigInfo.lastModified();
if (created < origTime) {
qDebug() << "Youtube-DL outdated. Updating in the background ...";
if (ytdlBin.exists()) {
ytdlBin.remove();
}
ytdlBinOrig.copy(data_dir + "/youtube-dl");
}
else {
qDebug() << "Youtube-DL does not need an update yet.";
}
ytdlBin.setPermissions(QFileDevice::ExeUser|QFileDevice::ExeGroup|QFileDevice::ExeOther|QFileDevice::ReadUser|QFileDevice::ReadGroup|QFileDevice::ReadOther|QFileDevice::WriteUser|QFileDevice::WriteGroup|QFileDevice::WriteOther);
// // Detect ffmpeg binary from Encode App
// QFile ffmpegBin;
// ffmpegBin.setFileName("/usr/share/harbour-encode/ffmpeg_static");
// if (ffmpegBin.exists()) {
// //qDebug() << "ffmpegbin exists";
// ffmpegAvailable = true;
// QFile ffmpegHelperBin;
// ffmpegHelperBin.setFileName(data_dir + "/ffmpeg");
// if (!ffmpegHelperBin.exists()) {
// ffmpegBin.link(data_dir + "/ffmpeg");
// }
// }
// else {
// //qDebug() << "ffmpegbin does not exist";
// ffmpegAvailable = false;
// }
}
void updateYtdl()
{
checkAndInstall();
updateBinary.start(data_dir + "/youtube-dl -U");
connect(&updateBinary, SIGNAL(finished(int)), this, SLOT(getUpdateStatus(int)));
}
void getStreamUrl()
{
//qDebug() << "Starting process with url:" << reqUrl;
checkAndInstall();
streamProcess.start(data_dir + "/youtube-dl " + parameter + " -g " + reqUrl);
connect(&streamProcess, SIGNAL(finished(int)), this, SLOT(getStreamUrlOutput(int)));
}
void getStreamTitle()
{
//qDebug() << "Starting process with url:" << reqUrl;
checkAndInstall();
titleProcess.start(data_dir + "/youtube-dl -e " + reqUrl);
connect(&titleProcess, SIGNAL(finished(int)), this, SLOT(getTitleOutput(int)));
}
void getMusicUrls()
{
getVorbisUrl();
getOpusUrl();
}
void getDashUrls()
{
getFullHdUrls();
}
void getFullHdUrls()
{
checkAndInstall();
parameter = " ";
parameter += "-f 137,140";
//parameter += "-f 248,251";
fullHdProcess.start(data_dir + "/youtube-dl " + parameter + " -g " + reqUrl);
connect(&fullHdProcess, SIGNAL(finished(int)), this, SLOT(getFullHdUrlOutput(int)));
}
void getVorbisUrl()
{
checkAndInstall();
parameter = " ";
parameter += "-f 171";
oggProcess.start(data_dir + "/youtube-dl " + parameter + " -g " + reqUrl);
connect(&oggProcess, SIGNAL(finished(int)), this, SLOT(getOggUrlOutput(int)));
}
void getOpusUrl()
{
checkAndInstall();
parameter = " ";
parameter += "-f 251";
opusProcess.start(data_dir + "/youtube-dl " + parameter + " -g " + reqUrl);
connect(&opusProcess, SIGNAL(finished(int)), this, SLOT(getOpusUrlOutput(int)));
}
void getYtSearchResults(QString searchTerm) {
checkAndInstall();
parameter = " ";
parameter += "-J \"ytsearch" + QString::number(m_searchResultNumber) + ":" + searchTerm.toUtf8() + "\"";
searchProcess.start(data_dir + "/youtube-dl " + parameter);
connect(&searchProcess, SIGNAL(finished(int)), this, SLOT(getYtSearchResultsOutput(int)));
}
void getFullHdUrlOutput(int exitCode)
{
if (exitCode == 0) {
QByteArray out = fullHdProcess.readAllStandardOutput();
QList<QByteArray> outputList = out.split('\n');
qDebug() << "Called the C++ slot and got following url:" << outputList[0];
_fullHdVideo = outputList[0];
_fullHdAudio = outputList[1];
fullHdChanged();
}
else {
printError(&oggProcess);
}
}
void getOggUrlOutput(int exitCode)
{
if (exitCode == 0) {
QByteArray out = oggProcess.readAllStandardOutput();
QList<QByteArray> outputList = out.split('\n');
qDebug() << "Called the C++ slot and got following url:" << outputList[0];
_oggAudio = outputList[0];
oggAudioChanged();
}
else {
printError(&oggProcess);
}
}
void getOpusUrlOutput(int exitCode)
{
if (exitCode == 0) {
QByteArray out = opusProcess.readAllStandardOutput();
QList<QByteArray> outputList = out.split('\n');
qDebug() << "Called the C++ slot and got following url:" << outputList[0];
_opusAudio = outputList[0];
opusAudioChanged();
}
else {
printError(&opusProcess);
}
}
void getStreamUrlOutput(int exitCode)
{
if (exitCode == 0) {
QByteArray out = streamProcess.readAllStandardOutput();
QList<QByteArray> outputList = out.split('\n');
qDebug() << "Called the C++ slot and got following url:" << outputList[0];
streamUrl = outputList[0];
streamUrlChanged(streamUrl);
}
else {
printError(&streamProcess);
}
}
void printError(QProcess *pProcess)
{
QByteArray errorOut = pProcess->readAllStandardError();
qDebug() << "Called the C++ slot and got following error:" << errorOut.simplified();
errorMsg = errorOut.simplified();
error(errorMsg);
}
void getTitleOutput(int exitCode)
{
if (exitCode == 0) {
QByteArray out = titleProcess.readAllStandardOutput();
QList<QByteArray> outputList = out.split('\n');
qDebug() << "Called the C++ slot and got following url:" << outputList[0];
streamTitle = outputList[0];
sTitleChanged(streamTitle);
}
}
void getUpdateStatus(int exitCode)
{
if (exitCode == 0) {
updateComplete();
}
else {
printError(&updateBinary);
}
}
void getYtSearchResultsOutput(int exitCode)
{
if (exitCode == 0) {
QByteArray out = searchProcess.readAllStandardOutput();
emit ytSearchResultsChanged(QString(out));
}
else {
printError(&searchProcess);
}
}
void killYtSearch() {
searchProcess.close();
}
};
#endif // YOUTUBEDLHELPER_HPP