loadingStreamUrl = true;
elIndex = 0;
ageGate = false;
+ webPageLoaded = false;
- getVideoInfo();
+ // getVideoInfo();
+ loadWebPage();
}
void YTVideo::getVideoInfo() {
q.addQueryItem("sts", "1588");
url.setQuery(q);
} else if (elIndex > elTypes.size() - 1) {
- qWarning() << "Cannot get video info";
- loadingStreamUrl = false;
- emit errorStreamUrl("Cannot get video info");
+ 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;
QObject *reply = HttpUtils::yt().get(url);
connect(reply, SIGNAL(data(QByteArray)), SLOT(gotVideoInfo(QByteArray)));
- connect(reply, SIGNAL(error(QString)), SLOT(errorVideoInfo(QString)));
+ connect(reply, SIGNAL(error(QString)), SLOT(emitError(QString)));
// see you in gotVideoInfo...
}
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();
+ 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" << videoInfo << videoTokeRE.pattern();
+ qDebug() << "Cannot get token. Trying next el param" << videoTokeRE.pattern() << videoInfo;
// Don't panic! We're gonna try another magic "el" param
elIndex++;
getVideoInfo();
getVideoInfo();
return;
}
-
QString fmtUrlMap = fmtMapRE.cap(1);
// qDebug() << "got fmtUrlMap" << fmtUrlMap;
fmtUrlMap = QByteArray::fromPercentEncoding(fmtUrlMap.toUtf8());
+*/
+
+ if (urlMap.isEmpty()) {
+ elIndex++;
+ getVideoInfo();
+ return;
+ }
qDebug() << "Got token and urlMap" << elIndex << videoToken << fmtUrlMap;
parseFmtUrlMap(fmtUrlMap);
}
-void YTVideo::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) {
+void YTVideo::parseFmtUrlMap(const QString &fmtUrlMap) {
int videoFormat = 0;
const VideoDefinition &definition = YT3::instance().maxVideoDefinition();
int separator = urlParam.indexOf('=');
sig = QByteArray::fromPercentEncoding(urlParam.mid(separator + 1).toUtf8());
} else if (urlParam.startsWith(QLatin1String("s="))) {
- if (fromWebPage || ageGate) {
+ if (webPageLoaded || ageGate) {
int separator = urlParam.indexOf('=');
sig = QByteArray::fromPercentEncoding(urlParam.mid(separator + 1).toUtf8());
sig = decryptSignature(sig);
urlMap.insert(format, url);
}
- if (!fromWebPage && !ageGate) {
+ if (!webPageLoaded && !ageGate) {
loadWebPage();
return;
}
return;
}
+ qDebug() << "available formats" << urlMap.keys();
const QVector<VideoDefinition> &definitions = VideoDefinition::getDefinitions();
int previousIndex = std::max(definitions.indexOf(definition) - 1, 0);
for (; previousIndex >= 0; previousIndex--) {
q.addQueryItem("has_verified", "1");
q.addQueryItem("bpctr", "9999999999");
url.setQuery(q);
+
+ // QUrl url("https://www.youtube.com/embed/" + videoId);
+
qDebug() << "Loading webpage" << url;
QObject *reply = HttpUtils::yt().get(url);
connect(reply, SIGNAL(data(QByteArray)), SLOT(scrapeWebPage(QByteArray)));
- connect(reply, SIGNAL(error(QString)), SLOT(errorVideoInfo(QString)));
+ connect(reply, SIGNAL(error(QString)), SLOT(emitError(QString)));
// see you in scrapWebPage(QByteArray)
}
-void YTVideo::errorVideoInfo(const QString &message) {
- loadingStreamUrl = false;
+void YTVideo::emitError(const QString &message) {
+ qWarning() << message;
emit errorStreamUrl(message);
}
void YTVideo::scrapeWebPage(const QByteArray &bytes) {
- qDebug() << "scrapeWebPage";
+ webPageLoaded = true;
const QString html = QString::fromUtf8(bytes);
+ // qDebug() << "scrapeWebPage" << html;
static const QRegExp ageGateRE(JsFunctions::instance()->ageGateRE());
if (ageGateRE.indexIn(html) != -1) {
fmtUrlMap += adaptiveFormatsRE.cap(1).replace("\\u0026", "&");
}
- if (fmtUrlMap.isEmpty()) {
+ if (fmtUrlMap.isEmpty() && urlMap.isEmpty()) {
qWarning() << "Cannot get fmtUrlMap from video page. Trying next el";
- elIndex++;
- getVideoInfo();
- return;
+ // elIndex++;
+ // getVideoInfo();
+ // return;
}
static const QRegExp jsPlayerRe(JsFunctions::instance()->jsPlayerRE());
*/
QObject *reply = HttpUtils::yt().get(jsPlayerUrl);
connect(reply, SIGNAL(data(QByteArray)), SLOT(parseJsPlayer(QByteArray)));
- connect(reply, SIGNAL(error(QString)), SLOT(errorVideoInfo(QString)));
+ connect(reply, SIGNAL(error(QString)), SLOT(emitError(QString)));
}
}
}();
for (const QRegExp &funcNameRe : funcNameRes) {
if (funcNameRe.indexIn(jsPlayer) == -1) {
- qWarning() << "Cannot capture signature function name" << funcNameRe;
+ qDebug() << "Cannot capture signature function name" << funcNameRe;
continue;
} else {
sigFuncName = funcNameRe.cap(1);
}
if (sigFuncName.isEmpty()) qDebug() << "Empty signature function name";
- parseFmtUrlMap(fmtUrlMap, true);
+ // parseFmtUrlMap(fmtUrlMap, true);
+ getVideoInfo();
}
void YTVideo::captureFunction(const QString &name, const QString &js) {