]> git.sur5r.net Git - minitube/blobdiff - src/ytvideo.cpp
New upstream version 3.4
[minitube] / src / ytvideo.cpp
index e84f7e16bc4540b4efac4c710c9092c616549a18..fca388a3e28cedef97df7b15e93cbf04bcab911b 100644 (file)
@@ -6,6 +6,7 @@
 #include "jsfunctions.h"
 #include "temporary.h"
 #include "videodefinition.h"
+#include "yt3.h"
 
 #include <QJSEngine>
 #include <QJSValue>
@@ -27,8 +28,10 @@ void YTVideo::loadStreamUrl() {
     loadingStreamUrl = true;
     elIndex = 0;
     ageGate = false;
+    webPageLoaded = false;
 
-    getVideoInfo();
+    // getVideoInfo();
+    loadWebPage();
 }
 
 void YTVideo::getVideoInfo() {
@@ -36,7 +39,7 @@ void YTVideo::getVideoInfo() {
 
     QUrl url;
     if (elIndex == elTypes.size()) {
-        // qDebug() << "Trying special embedded el param";
+        qDebug() << "Trying special embedded el param";
         url = QUrl("https://www.youtube.com/get_video_info");
         QUrlQuery q;
         q.addQueryItem("video_id", videoId);
@@ -48,9 +51,13 @@ 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;
@@ -59,9 +66,9 @@ void YTVideo::getVideoInfo() {
                            .arg(videoId, elTypes.at(elIndex)));
     }
 
-    QObject *reply = HttpUtils::yt().get(url);
+    QObject *reply = HttpUtils::stealthAndNotCached().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...
 }
@@ -70,10 +77,55 @@ 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" << 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();
@@ -81,7 +133,6 @@ void YTVideo::gotVideoInfo(const QByteArray &bytes) {
     }
 
     QString videoToken = videoTokeRE.cap(1);
-    qDebug() << "got token" << videoToken;
     while (videoToken.contains('%'))
         videoToken = QByteArray::fromPercentEncoding(videoToken.toLatin1());
     qDebug() << "videoToken" << videoToken;
@@ -96,22 +147,29 @@ void YTVideo::gotVideoInfo(const QByteArray &bytes) {
         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, bool fromWebPage) {
-    const QString definitionName = QSettings().value("definition", "360p").toString();
-    const VideoDefinition &definition = VideoDefinition::forName(definitionName);
+void YTVideo::parseFmtUrlMap(const QString &fmtUrlMap) {
+    int videoFormat = 0;
+    const VideoDefinition &definition = YT3::instance().maxVideoDefinition();
 
-    qDebug() << "fmtUrlMap" << fmtUrlMap;
+    // qDebug() << "fmtUrlMap" << fmtUrlMap;
     const QVector<QStringRef> formatUrls = fmtUrlMap.splitRef(',', QString::SkipEmptyParts);
-    QMap<int, QString> urlMap;
+
     for (const QStringRef &formatUrl : formatUrls) {
         // qDebug() << "formatUrl" << formatUrl;
         const QVector<QStringRef> urlParams = formatUrl.split('&', QString::SkipEmptyParts);
@@ -120,8 +178,13 @@ void YTVideo::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) {
         int format = -1;
         QString url;
         QString sig;
+        QStringRef sp;
         for (const QStringRef &urlParam : urlParams) {
-            // qWarning() << urlParam;
+            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();
@@ -132,55 +195,62 @@ void YTVideo::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) {
                 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());
-                    if (ageGate)
-                        sig = JsFunctions::instance()->decryptAgeSignature(sig);
-                    else {
-                        sig = decryptSignature(sig);
-                        if (sig.isEmpty()) sig = JsFunctions::instance()->decryptSignature(sig);
-                    }
+                    sig = decryptSignature(sig);
+                    if (sig.isEmpty()) sig = JsFunctions::instance()->decryptSignature(sig);
+                    if (sig.isEmpty()) qWarning() << "Empty signature";
                 } else {
-                    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");
-                    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(errorVideoInfo(QString)));
-                    // see you in scrapWebPage(QByteArray)
+                    loadWebPage();
                     return;
                 }
             }
         }
         if (format == -1 || url.isNull()) continue;
 
-        url += QLatin1String("&signature=") + sig;
+        if (!sig.isEmpty()) {
+            if (sp.isEmpty())
+                url += QLatin1String("&signature=") + sig;
+            else
+                url += '&' + sp + '=' + sig;
+        }
 
         if (!url.contains(QLatin1String("ratebypass"))) url += QLatin1String("&ratebypass=yes");
 
-        qDebug() << url;
-
+        qDebug() << format;
         if (format == definition.getCode()) {
-            qDebug() << "Found format" << definitionCode;
-            saveDefinitionForUrl(url, definition);
-            return;
+            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) - 1, 0);
     for (; previousIndex >= 0; previousIndex--) {
         const VideoDefinition &previousDefinition = definitions.at(previousIndex);
+        qDebug() << "Testing format" << previousDefinition.getCode();
         if (urlMap.contains(previousDefinition.getCode())) {
-            // qDebug() << "Found format" << definitionCode;
+            qDebug() << "Found format" << previousDefinition.getCode();
             saveDefinitionForUrl(urlMap.value(previousDefinition.getCode()), previousDefinition);
             return;
         }
@@ -189,56 +259,95 @@ void YTVideo::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) {
     emit errorStreamUrl(tr("Cannot get video stream for %1").arg(videoId));
 }
 
-void YTVideo::errorVideoInfo(const QString &message) {
-    loadingStreamUrl = false;
+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) {
-        // qDebug() << "Found ageGate";
+    if (ageGateRE.indexIn(html) != -1 || html.contains("desktopLegacyAgeGateReason")) {
+        qDebug() << "Found ageGate";
         ageGate = true;
-        elIndex = 4;
-        getVideoInfo();
+        // elIndex = 4;
+        // getVideoInfo();
+        loadEmbedPage();
         return;
     }
 
+    // "\"url_encoded_fmt_stream_map\":\s*\"([^\"]+)\""
     static const QRegExp fmtMapRE(JsFunctions::instance()->webPageFmtMapRE());
-    if (fmtMapRE.indexIn(html) == -1) {
-        qWarning() << "Error parsing video page";
-        // emit errorStreamUrl("Error parsing video page");
-        // loadingStreamUrl = false;
-        elIndex++;
-        getVideoInfo();
-        return;
+    if (fmtMapRE.indexIn(html) != -1) {
+        fmtUrlMap = fmtMapRE.cap(1);
+        fmtUrlMap.replace("\\u0026", "&");
     }
-    fmtUrlMap = fmtMapRE.cap(1);
-    fmtUrlMap.replace("\\u0026", "&");
-// parseFmtUrlMap(fmtUrlMap, true);
-
-#ifdef APP_DASH
-    QSettings settings;
-    QString definitionName = settings.value("definition", "360p").toString();
-    if (definitionName == QLatin1String("1080p")) {
-        QRegExp dashManifestRe("\"dashmpd\":\\s*\"([^\"]+)\"");
-        if (dashManifestRe.indexIn(html) != -1) {
-            dashManifestUrl = dashManifestRe.cap(1);
-            dashManifestUrl.remove('\\');
-            qDebug() << "dashManifestUrl" << dashManifestUrl;
-        } else {
-            qWarning() << "DASH manifest not found in webpage";
-            if (dashManifestRe.indexIn(fmtUrlMap) != -1) {
-                dashManifestUrl = dashManifestRe.cap(1);
-                dashManifestUrl.remove('\\');
-                qDebug() << "dashManifestUrl" << dashManifestUrl;
-            } else
-                qWarning() << "DASH manifest not found in fmtUrlMap" << fmtUrlMap;
-        }
+
+    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;
     }
-#endif
 
     static const QRegExp jsPlayerRe(JsFunctions::instance()->jsPlayerRE());
     if (jsPlayerRe.indexIn(html) != -1) {
@@ -255,9 +364,11 @@ void YTVideo::scrapeWebPage(const QByteArray &bytes) {
                     jsPlayerIdRe.indexIn(jsPlayerUrl);
                     QString jsPlayerId = jsPlayerRe.cap(1);
                     */
-        QObject *reply = HttpUtils::yt().get(jsPlayerUrl);
+        QObject *reply = HttpUtils::stealthAndNotCached().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)));
+    } else {
+        qDebug() << "Cannot find jsPlayer";
     }
 }
 
@@ -266,58 +377,33 @@ void YTVideo::parseJsPlayer(const QByteArray &bytes) {
     // qDebug() << "jsPlayer" << jsPlayer;
 
     // QRegExp funcNameRe("[\"']signature[\"']\\s*,\\s*([" + jsNameChars + "]+)\\(");
-    static const QRegExp funcNameRe(
-            JsFunctions::instance()->signatureFunctionNameRE().arg(jsNameChars));
-
-    if (funcNameRe.indexIn(jsPlayer) == -1) {
-        qWarning() << "Cannot capture signature function name" << jsPlayer;
-    } else {
-        sigFuncName = funcNameRe.cap(1);
-        captureFunction(sigFuncName, jsPlayer);
-        // qWarning() << sigFunctions << sigObjects;
-    }
-
-#ifdef APP_DASH
-    if (!dashManifestUrl.isEmpty()) {
-        QRegExp sigRe("/s/([\\w\\.]+)");
-        if (sigRe.indexIn(dashManifestUrl) != -1) {
-            qDebug() << "Decrypting signature for dash manifest";
-            QString sig = sigRe.cap(1);
-            sig = decryptSignature(sig);
-            dashManifestUrl.replace(sigRe, "/signature/" + sig);
-            qWarning() << "dash manifest" << dashManifestUrl;
-
-            if (true) {
-                // let phonon play the manifest
-                m_streamUrl = dashManifestUrl;
-                this->definitionCode = 37;
-                emit gotStreamUrl(m_streamUrl);
-                loadingStreamUrl = false;
-            } else {
-                // download the manifest
-                QObject *reply = HttpUtils::yt().get(QUrl::fromEncoded(dashManifestUrl.toUtf8()));
-                connect(reply, SIGNAL(data(QByteArray)), SLOT(parseDashManifest(QByteArray)));
-                connect(reply, SIGNAL(error(QString)), SLOT(errorVideoInfo(QString)));
+    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;
             }
-
-            return;
+            captureFunction(sigFuncName, jsPlayer);
+            qDebug() << sigFunctions << sigObjects;
+            break;
         }
     }
-#endif
-
-    parseFmtUrlMap(fmtUrlMap, true);
-}
-
-void YTVideo::parseDashManifest(const QByteArray &bytes) {
-    QFile file(Temporary::filename() + ".mpd");
-    if (!file.open(QIODevice::WriteOnly)) qWarning() << file.errorString() << file.fileName();
-    QDataStream stream(&file);
-    stream.writeRawData(bytes.constData(), bytes.size());
+    if (sigFuncName.isEmpty()) qDebug() << "Empty signature function name" << jsPlayer;
 
-    m_streamUrl = "file://" + file.fileName();
-    this->definitionCode = 37;
-    emit gotStreamUrl(m_streamUrl);
-    loadingStreamUrl = false;
+    // parseFmtUrlMap(fmtUrlMap, true);
+    getVideoInfo();
 }
 
 void YTVideo::captureFunction(const QString &name, const QString &js) {
@@ -423,8 +509,26 @@ QString YTVideo::decryptSignature(const QString &s) {
 }
 
 void YTVideo::saveDefinitionForUrl(const QString &url, const VideoDefinition &definition) {
-    m_streamUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
+    qDebug() << "Selected video format" << definition.getCode() << definition.getName()
+             << definition.hasAudio();
+    m_streamUrl = url;
     definitionCode = definition.getCode();
-    emit gotStreamUrl(m_streamUrl);
+
+    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);
 }