This repository has been archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ytvideo.cpp
534 lines (484 loc) · 20 KB
/
ytvideo.cpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include "ytvideo.h"
#include "datautils.h"
#include "http.h"
#include "httputils.h"
#include "jsfunctions.h"
#include "temporary.h"
#include "videodefinition.h"
#include "yt3.h"
#include <QJSEngine>
#include <QJSValue>
#include <QtNetwork>
namespace {
static const QString jsNameChars = "a-zA-Z0-9\\$_";
}
YTVideo::YTVideo(const QString &videoId, QObject *parent)
: QObject(parent), videoId(videoId), definitionCode(0), elIndex(0), ageGate(false),
loadingStreamUrl(false) {}
void YTVideo::loadStreamUrl() {
if (loadingStreamUrl) {
qDebug() << "Already loading stream URL for" << videoId;
return;
}
loadingStreamUrl = true;
elIndex = 0;
ageGate = false;
webPageLoaded = false;
// getVideoInfo();
loadWebPage();
}
void YTVideo::getVideoInfo() {
static const QStringList elTypes = {"&el=embedded", "&el=detailpage", "&el=vevo", ""};
QUrl url;
if (elIndex == elTypes.size()) {
qDebug() << "Trying special embedded el param";
url = QUrl("https://www.youtube.com/get_video_info");
QUrlQuery q;
q.addQueryItem("video_id", videoId);
q.addQueryItem("el", "embedded");
q.addQueryItem("gl", "US");
q.addQueryItem("hl", "en");
q.addQueryItem("eurl", "https://youtube.googleapis.com/v/" + videoId);
q.addQueryItem("asv", "3");
q.addQueryItem("sts", "1588");
url.setQuery(q);
} else if (elIndex > elTypes.size() - 1) {
qDebug() << "Cannot get video info";
if (!webPageLoaded) {
// no video info file, but we can try loading the "urlmap" from the web page
loadWebPage();
} else {
emitError("Cannot get video info");
}
return;
} else {
// qDebug() << "Trying el param:" << elTypes.at(elIndex) << elIndex;
url = QUrl(QString("https://www.youtube.com/"
"get_video_info?video_id=%1%2&ps=default&eurl=&gl=US&hl=en")
.arg(videoId, elTypes.at(elIndex)));
}
QObject *reply = HttpUtils::stealthAndNotCached().get(url);
connect(reply, SIGNAL(data(QByteArray)), SLOT(gotVideoInfo(QByteArray)));
connect(reply, SIGNAL(error(QString)), SLOT(emitError(QString)));
// see you in gotVideoInfo...
}
void YTVideo::gotVideoInfo(const QByteArray &bytes) {
QString videoInfo = QString::fromUtf8(bytes);
// qDebug() << "videoInfo" << videoInfo;
// get player_response
static const QRegExp playerResponseRE("&player_response=([^&]+)");
if (playerResponseRE.indexIn(videoInfo) != -1) {
QString playerResponse = playerResponseRE.cap(1);
QByteArray playerResponseUtf8 = QByteArray::fromPercentEncoding(playerResponse.toUtf8());
// qDebug() << "player_response" << playerResponseUtf8;
QJsonDocument doc = QJsonDocument::fromJson(playerResponseUtf8);
QJsonObject obj = doc.object();
if (obj.contains("streamingData")) {
auto parseFormats = [this](const QJsonArray &formats) {
for (const QJsonValue &format : formats) {
QJsonObject formatObj = format.toObject();
int itag = formatObj["itag"].toInt();
QString url = formatObj["url"].toString();
if (url.isEmpty()) {
QString cipher = formatObj["cipher"].toString();
if (cipher.isEmpty()) cipher = formatObj["signatureCipher"].toString();
QUrlQuery q(cipher);
qDebug() << "Cipher is " << q.toString();
url = q.queryItemValue("url").trimmed();
// while (url.contains('%'))
url = QByteArray::fromPercentEncoding(url.toUtf8());
if (q.hasQueryItem("s")) {
QString s = q.queryItemValue("s");
qDebug() << "s is" << s;
s = decryptSignature(s);
if (!s.isEmpty()) {
qDebug() << "Added signature" << s;
url += "&sig=";
url += s;
}
}
}
// qDebug() << "player_response format" << itag << url;
if (!url.isEmpty()) urlMap.insert(itag, url);
}
};
QJsonObject streamingDataObj = obj["streamingData"].toObject();
// qDebug() << "Found streamingData" << streamingDataObj;
parseFormats(streamingDataObj["formats"].toArray());
parseFormats(streamingDataObj["adaptiveFormats"].toArray());
}
}
/*
// get video token
static const QRegExp videoTokeRE(JsFunctions::instance()->videoTokenRE());
if (videoTokeRE.indexIn(videoInfo) == -1) {
qDebug() << "Cannot get token. Trying next el param" << videoTokeRE.pattern() << videoInfo;
// Don't panic! We're gonna try another magic "el" param
elIndex++;
getVideoInfo();
return;
}
QString videoToken = videoTokeRE.cap(1);
while (videoToken.contains('%'))
videoToken = QByteArray::fromPercentEncoding(videoToken.toLatin1());
qDebug() << "videoToken" << videoToken;
this->videoToken = videoToken;
// get fmt_url_map
static const QRegExp fmtMapRE(JsFunctions::instance()->videoInfoFmtMapRE());
if (fmtMapRE.indexIn(videoInfo) == -1) {
qDebug() << "Cannot get urlMap. Trying next el param";
// Don't panic! We're gonna try another magic "el" param
elIndex++;
getVideoInfo();
return;
}
QString fmtUrlMap = fmtMapRE.cap(1);
// qDebug() << "got fmtUrlMap" << fmtUrlMap;
fmtUrlMap = QByteArray::fromPercentEncoding(fmtUrlMap.toUtf8());
*/
if (urlMap.isEmpty()) {
qDebug() << "empty urlMap, trying next el";
elIndex++;
getVideoInfo();
return;
}
qDebug() << "Got token and urlMap" << elIndex << videoToken << fmtUrlMap;
parseFmtUrlMap(fmtUrlMap);
}
void YTVideo::parseFmtUrlMap(const QString &fmtUrlMap) {
int videoFormat = 0;
const VideoDefinition &definition = YT3::instance().maxVideoDefinition();
// qDebug() << "fmtUrlMap" << fmtUrlMap;
const QVector<QStringRef> formatUrls = fmtUrlMap.splitRef(',', QString::SkipEmptyParts);
for (const QStringRef &formatUrl : formatUrls) {
// qDebug() << "formatUrl" << formatUrl;
const QVector<QStringRef> urlParams = formatUrl.split('&', QString::SkipEmptyParts);
// qDebug() << "urlParams" << urlParams;
int format = -1;
QString url;
QString sig;
QStringRef sp;
for (const QStringRef &urlParam : urlParams) {
qDebug() << "urlParam" << urlParam;
if (sp.isNull() && urlParam.startsWith(QLatin1String("sp"))) {
int separator = urlParam.indexOf('=');
sp = urlParam.mid(separator + 1);
}
if (urlParam.startsWith(QLatin1String("itag="))) {
int separator = urlParam.indexOf('=');
format = urlParam.mid(separator + 1).toInt();
} else if (urlParam.startsWith(QLatin1String("url="))) {
int separator = urlParam.indexOf('=');
url = QByteArray::fromPercentEncoding(urlParam.mid(separator + 1).toUtf8());
} else if (urlParam.startsWith(QLatin1String("sig="))) {
int separator = urlParam.indexOf('=');
sig = QByteArray::fromPercentEncoding(urlParam.mid(separator + 1).toUtf8());
} else if (urlParam.startsWith(QLatin1String("s="))) {
if (webPageLoaded || ageGate) {
int separator = urlParam.indexOf('=');
sig = QByteArray::fromPercentEncoding(urlParam.mid(separator + 1).toUtf8());
sig = decryptSignature(sig);
if (sig.isEmpty()) sig = JsFunctions::instance()->decryptSignature(sig);
if (sig.isEmpty()) qWarning() << "Empty signature";
} else {
loadWebPage();
return;
}
}
}
if (format == -1 || url.isNull()) continue;
if (!sig.isEmpty()) {
if (sp.isEmpty())
url += QLatin1String("&signature=") + sig;
else
url += QLatin1Char('&') + sp.toString() + QLatin1Char('=') + sig;
}
if (!url.contains(QLatin1String("ratebypass"))) url += QLatin1String("&ratebypass=yes");
qDebug() << format;
if (format == definition.getCode()) {
qDebug() << "Found format" << format;
if (definition.hasAudio()) {
// we found the exact match with an audio/video stream
saveDefinitionForUrl(url, definition);
return;
}
videoFormat = format;
}
urlMap.insert(format, url);
}
if (!webPageLoaded && !ageGate) {
loadWebPage();
return;
}
if (videoFormat != 0) {
// exact match with video stream was found
const VideoDefinition &definition = VideoDefinition::forCode(videoFormat);
saveDefinitionForUrl(urlMap.value(videoFormat), definition);
return;
}
qDebug() << "available formats" << urlMap.keys();
const QVector<VideoDefinition> &definitions = VideoDefinition::getDefinitions();
int previousIndex = std::max(definitions.indexOf(definition), 0);
for (; previousIndex >= 0; previousIndex--) {
const VideoDefinition &previousDefinition = definitions.at(previousIndex);
qDebug() << "Testing format" << previousDefinition.getCode();
if (urlMap.contains(previousDefinition.getCode())) {
qDebug() << "Found format" << previousDefinition.getCode();
saveDefinitionForUrl(urlMap.value(previousDefinition.getCode()), previousDefinition);
return;
}
}
emit errorStreamUrl(tr("Cannot get video stream for %1").arg(videoId));
}
void YTVideo::loadWebPage() {
QUrl url("https://www.youtube.com/watch");
QUrlQuery q;
q.addQueryItem("v", videoId);
q.addQueryItem("gl", "US");
q.addQueryItem("hl", "en");
q.addQueryItem("has_verified", "1");
q.addQueryItem("bpctr", "9999999999");
url.setQuery(q);
qDebug() << "Loading webpage" << url;
QObject *reply = HttpUtils::yt().get(url);
connect(reply, SIGNAL(data(QByteArray)), SLOT(scrapeWebPage(QByteArray)));
connect(reply, SIGNAL(error(QString)), SLOT(emitError(QString)));
// see you in scrapWebPage(QByteArray)
}
void YTVideo::loadEmbedPage() {
QUrl url("https://www.youtube.com/embed/" + videoId);
auto reply = HttpUtils::yt().get(url);
connect(reply, &HttpReply::finished, this, [this](const HttpReply &reply) {
if (!reply.isSuccessful()) {
getVideoInfo();
return;
}
static const QRegExp embedRE("\"sts\"\\s*:\\s*(\\d+)");
QString sts;
if (embedRE.indexIn(reply.body()) == -1) {
// qDebug() << "Cannot get sts" << reply.body();
} else {
sts = embedRE.cap(1);
qDebug() << "sts" << sts;
}
QUrlQuery q;
q.addQueryItem("video_id", videoId);
q.addQueryItem("eurl", "https://youtube.googleapis.com/v/" + videoId);
q.addQueryItem("sts", sts);
QUrl url = QUrl("https://www.youtube.com/get_video_info");
url.setQuery(q);
HttpReply *r = HttpUtils::stealthAndNotCached().get(url);
connect(r, &HttpReply::data, this, [this](const QByteArray &bytes) {
QByteArray decodedBytes = QByteArray::fromPercentEncoding(bytes);
gotVideoInfo(decodedBytes);
});
connect(r, &HttpReply::error, this, &YTVideo::emitError);
});
}
void YTVideo::emitError(const QString &message) {
qWarning() << message;
emit errorStreamUrl(message);
}
void YTVideo::scrapeWebPage(const QByteArray &bytes) {
webPageLoaded = true;
const QString html = QString::fromUtf8(bytes);
// qDebug() << "scrapeWebPage" << html;
static const QRegExp ageGateRE(JsFunctions::instance()->ageGateRE());
if (ageGateRE.indexIn(html) != -1 || html.contains("desktopLegacyAgeGateReason")) {
qDebug() << "Found ageGate";
ageGate = true;
// elIndex = 4;
// getVideoInfo();
loadEmbedPage();
return;
}
// "\"url_encoded_fmt_stream_map\":\s*\"([^\"]+)\""
static const QRegExp fmtMapRE(JsFunctions::instance()->webPageFmtMapRE());
if (fmtMapRE.indexIn(html) != -1) {
fmtUrlMap = fmtMapRE.cap(1);
fmtUrlMap.replace("\\u0026", "&");
}
QRegExp adaptiveFormatsRE("\"adaptive_fmts\":\\s*\"([^\"]+)\"");
if (adaptiveFormatsRE.indexIn(html) != -1) {
qDebug() << "Found adaptive_fmts";
if (!fmtUrlMap.isEmpty()) fmtUrlMap += ',';
fmtUrlMap += adaptiveFormatsRE.cap(1).replace("\\u0026", "&");
}
if (fmtUrlMap.isEmpty() && urlMap.isEmpty()) {
qDebug() << "Cannot get fmtUrlMap from video page. Trying next el";
// elIndex++;
// getVideoInfo();
// return;
}
static const QRegExp jsPlayerRe(JsFunctions::instance()->jsPlayerRE());
if (jsPlayerRe.indexIn(html) != -1) {
QString jsPlayerUrl = jsPlayerRe.cap(1);
jsPlayerUrl.remove('\\');
if (jsPlayerUrl.startsWith(QLatin1String("//"))) {
jsPlayerUrl = QLatin1String("https:") + jsPlayerUrl;
} else if (jsPlayerUrl.startsWith("/")) {
jsPlayerUrl = QLatin1String("https://youtube.com") + jsPlayerUrl;
}
// qDebug() << "jsPlayerUrl" << jsPlayerUrl;
/*
QRegExp jsPlayerIdRe("-(.+)\\.js");
jsPlayerIdRe.indexIn(jsPlayerUrl);
QString jsPlayerId = jsPlayerRe.cap(1);
*/
QObject *reply = HttpUtils::stealthAndNotCached().get(jsPlayerUrl);
connect(reply, SIGNAL(data(QByteArray)), SLOT(parseJsPlayer(QByteArray)));
connect(reply, SIGNAL(error(QString)), SLOT(emitError(QString)));
} else {
qDebug() << "Cannot find jsPlayer";
}
}
void YTVideo::parseJsPlayer(const QByteArray &bytes) {
jsPlayer = QString::fromUtf8(bytes);
// qDebug() << "jsPlayer" << jsPlayer;
// QRegExp funcNameRe("[\"']signature[\"']\\s*,\\s*([" + jsNameChars + "]+)\\(");
static const QVector<QRegExp> funcNameRes = [] {
QVector<QRegExp> res;
for (const QString &s : JsFunctions::instance()->signatureFunctionNameREs()) {
res << QRegExp(s.arg(jsNameChars));
}
return res;
}();
for (const QRegExp &funcNameRe : funcNameRes) {
if (funcNameRe.indexIn(jsPlayer) == -1) {
qDebug() << "Cannot capture signature function name" << funcNameRe;
continue;
} else {
sigFuncName = funcNameRe.cap(1);
qDebug() << "Captures" << funcNameRe.captureCount() << funcNameRe.capturedTexts();
if (sigFuncName.isEmpty()) {
qDebug() << "Empty capture for" << funcNameRe;
continue;
}
captureFunction(sigFuncName, jsPlayer);
qDebug() << sigFunctions << sigObjects;
break;
}
}
if (sigFuncName.isEmpty()) qDebug() << "Empty signature function name" << jsPlayer;
// parseFmtUrlMap(fmtUrlMap, true);
getVideoInfo();
}
void YTVideo::captureFunction(const QString &name, const QString &js) {
qDebug() << __PRETTY_FUNCTION__ << name;
const QString argsAndBody =
QLatin1String("\\s*\\([") + jsNameChars + QLatin1String(",\\s]*\\)\\s*\\{[^\\}]+\\}");
QString func;
QRegExp funcRe(QLatin1String("function\\s+") + QRegExp::escape(name) + argsAndBody);
if (funcRe.indexIn(js) != -1) {
func = funcRe.cap(0);
} else {
// try var foo = function(bar) { };
funcRe = QRegExp(QLatin1String("var\\s+") + QRegExp::escape(name) +
QLatin1String("\\s*=\\s*function") + argsAndBody);
if (funcRe.indexIn(js) != -1) {
func = funcRe.cap(0);
} else {
// try ,gr= function(bar) { };
funcRe = QRegExp(QLatin1String("[,\\s;}\\.\\)](") + QRegExp::escape(name) +
QLatin1String("\\s*=\\s*function") + argsAndBody + ")");
if (funcRe.indexIn(js) != -1) {
func = funcRe.cap(1);
} else {
qWarning() << "Cannot capture function" << name;
return;
}
}
}
sigFunctions.insert(name, func);
// capture inner functions
static const QRegExp invokedFuncRe(QLatin1String("[\\s=;\\(]([") + jsNameChars +
QLatin1String("]+)\\s*\\([") + jsNameChars +
QLatin1String(",\\s]+\\)"));
int pos = name.length() + 9;
while ((pos = invokedFuncRe.indexIn(func, pos)) != -1) {
QString funcName = invokedFuncRe.cap(1);
if (!sigFunctions.contains(funcName)) captureFunction(funcName, js);
pos += invokedFuncRe.matchedLength();
}
// capture referenced objects
static const QRegExp objRe(QLatin1String("[\\s=;\\(]([") + jsNameChars +
QLatin1String("]+)\\.[") + jsNameChars + QLatin1String("]+"));
pos = name.length() + 9;
while ((pos = objRe.indexIn(func, pos)) != -1) {
QString objName = objRe.cap(1);
if (!sigObjects.contains(objName)) captureObject(objName, js);
pos += objRe.matchedLength();
}
}
void YTVideo::captureObject(const QString &name, const QString &js) {
QRegExp re(QLatin1String("var\\s+") + QRegExp::escape(name) +
QLatin1String("\\s*=\\s*\\{.*\\}\\s*;"));
re.setMinimal(true);
if (re.indexIn(js) == -1) {
qWarning() << "Cannot capture object" << name;
return;
}
QString obj = re.cap(0);
sigObjects.insert(name, obj);
}
QString YTVideo::decryptSignature(const QString &s) {
qDebug() << "decryptSignature" << sigFuncName << sigFunctions << sigObjects;
if (sigFuncName.isEmpty()) return QString();
QJSEngine engine;
for (const QString &f : sigObjects) {
QJSValue value = engine.evaluate(f);
if (value.isError()) qWarning() << "Error in" << f << value.toString();
}
for (const QString &f : sigFunctions) {
QJSValue value = engine.evaluate(f);
if (value.isError()) qWarning() << "Error in" << f << value.toString();
}
QString js = sigFuncName + "('" + s + "');";
QJSValue value = engine.evaluate(js);
bool error = false;
if (value.isUndefined()) {
qWarning() << "Undefined result for" << js;
error = true;
}
if (value.isError()) {
qWarning() << "Error in" << js << value.toString();
error = true;
}
if (error) {
QJSEngine engine2;
engine2.evaluate(jsPlayer);
value = engine2.evaluate(js);
if (value.isUndefined()) {
qWarning() << "Undefined result for" << js;
error = true;
}
if (value.isError()) {
qWarning() << "Error in" << js << value.toString();
error = true;
}
}
if (error) return QString();
return value.toString();
}
void YTVideo::saveDefinitionForUrl(const QString &url, const VideoDefinition &definition) {
qDebug() << "Selected video format" << definition.getCode() << definition.getName()
<< definition.hasAudio();
m_streamUrl = url;
definitionCode = definition.getCode();
QString audioUrl;
if (!definition.hasAudio()) {
qDebug() << "Finding audio format";
static const QVector<int> audioFormats({251, 171, 140});
for (int audioFormat : audioFormats) {
qDebug() << "Trying audio format" << audioFormat;
auto i = urlMap.constFind(audioFormat);
if (i != urlMap.constEnd()) {
qDebug() << "Found audio format" << i.value();
audioUrl = i.value();
break;
}
}
}
loadingStreamUrl = false;
emit gotStreamUrl(url, audioUrl);
}