]> git.sur5r.net Git - minitube/blobdiff - src/video.cpp
Thumb scaling optimization
[minitube] / src / video.cpp
index 545c29827d5078ba0fcbae1009a624cbcd35b1eb..1e65242ca389531707ec680a974530caa2c7acbc 100644 (file)
@@ -19,10 +19,12 @@ Video* Video::clone() {
     cloneVideo->m_title = m_title;
     cloneVideo->m_description = m_description;
     cloneVideo->m_author = m_author;
+    cloneVideo->m_authorUri = m_authorUri;
     cloneVideo->m_webpage = m_webpage;
     cloneVideo->m_streamUrl = m_streamUrl;
     cloneVideo->m_thumbnail = m_thumbnail;
-    cloneVideo->m_thumbnailUrls = m_thumbnailUrls;
+    cloneVideo->m_thumbnailUrl = m_thumbnailUrl;
+    cloneVideo->m_mediumThumbnailUrl = m_mediumThumbnailUrl;
     cloneVideo->m_duration = m_duration;
     cloneVideo->m_published = m_published;
     cloneVideo->m_viewCount = m_viewCount;
@@ -32,19 +34,38 @@ Video* Video::clone() {
     return cloneVideo;
 }
 
-void Video::preloadThumbnail() {
-    if (m_thumbnailUrls.isEmpty()) return;
-    QObject *reply = The::http()->get(m_thumbnailUrls.first());
+void Video::setWebpage(QUrl webpage) {
+    m_webpage = webpage;
+
+    // Get Video ID
+    // youtube-dl line 428
+    // QRegExp re("^((?:http://)?(?:\\w+\\.)?youtube\\.com/(?:(?:v/)|(?:(?:watch(?:\\.php)?)?\\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$");
+    QRegExp re("^https?://www\\.youtube\\.com/watch\\?v=([0-9A-Za-z_-]+).*");
+    bool match = re.exactMatch(m_webpage.toString());
+    if (!match || re.numCaptures() < 1) {
+        qDebug() << QString("Cannot get video id for %1").arg(m_webpage.toString());
+        // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
+        // loadingStreamUrl = false;
+        return;
+    }
+    videoId = re.cap(1);
+}
+
+void Video::loadThumbnail() {
+    QObject *reply = The::http()->get(m_thumbnailUrl);
     connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
 }
 
 void Video::setThumbnail(QByteArray bytes) {
-    m_thumbnail = QImage::fromData(bytes);
+    m_thumbnail.loadFromData(bytes);
+    m_thumbnail = m_thumbnail.scaled(160, 90);
     emit gotThumbnail();
 }
 
-const QImage Video::thumbnail() const {
-    return m_thumbnail;
+void Video::loadMediumThumbnail() {
+    if (m_mediumThumbnailUrl.isEmpty()) return;
+    QObject *reply = The::http()->get(m_mediumThumbnailUrl);
+    connect(reply, SIGNAL(data(QByteArray)), SIGNAL(gotMediumThumbnail(QByteArray)));
 }
 
 void Video::loadStreamUrl() {
@@ -55,21 +76,7 @@ void Video::loadStreamUrl() {
     loadingStreamUrl = true;
 
     // https://develop.participatoryculture.org/trac/democracy/browser/trunk/tv/portable/flashscraper.py
-
-    // Get Video ID
-    // youtube-dl line 428
-    // QRegExp re("^((?:http://)?(?:\\w+\\.)?youtube\\.com/(?:(?:v/)|(?:(?:watch(?:\\.php)?)?\\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$");
-    QRegExp re("^http://www\\.youtube\\.com/watch\\?v=([0-9A-Za-z_-]+).*");
-    bool match = re.exactMatch(m_webpage.toString());
-    if (!match || re.numCaptures() < 1) {
-        emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
-        loadingStreamUrl = false;
-        return;
-    }
-    videoId = re.cap(1);
-
     getVideoInfo();
-
 }
 
 void  Video::getVideoInfo() {
@@ -125,7 +132,7 @@ void  Video::gotVideoInfo(QByteArray data) {
     this->videoToken = videoToken;
 
     // get fmt_url_map
-    re = QRegExp("^.*&fmt_url_map=([^&]+).*$");
+    re = QRegExp("^.*&url_encoded_fmt_stream_map=([^&]+).*$");
     match = re.exactMatch(videoInfo);
     // handle regexp failure
     if (!match || re.numCaptures() < 1) {
@@ -146,10 +153,30 @@ void  Video::gotVideoInfo(QByteArray data) {
     QStringList formatUrls = fmtUrlMap.split(",", QString::SkipEmptyParts);
     QHash<int, QString> urlMap;
     foreach(QString formatUrl, formatUrls) {
-        int separator = formatUrl.indexOf("|");
-        if (separator == -1) continue;
-        int format = formatUrl.left(separator).toInt();
-        QString url = formatUrl.mid(separator + 1);
+        // qDebug() << "formatUrl" << formatUrl;
+        QStringList urlParams = formatUrl.split("&", QString::SkipEmptyParts);
+        // qDebug() << "urlParams" << urlParams;
+
+        int format = -1;
+        QString url;
+        QString sig;
+        foreach(QString urlParam, urlParams) {
+            if (urlParam.startsWith("itag=")) {
+                int separator = urlParam.indexOf("=");
+                format = urlParam.mid(separator + 1).toInt();
+            } else if (urlParam.startsWith("url=")) {
+                int separator = urlParam.indexOf("=");
+                url = urlParam.mid(separator + 1);
+                url = QByteArray::fromPercentEncoding(url.toUtf8());
+            } else if (urlParam.startsWith("sig=")) {
+                int separator = urlParam.indexOf("=");
+                sig = urlParam.mid(separator + 1);
+                sig = QByteArray::fromPercentEncoding(sig.toUtf8());
+            }
+        }
+        if (format == -1 || url.isNull()) continue;
+
+        url += "&signature=" + sig;
 
         if (format == definitionCode) {
             qDebug() << "Found format" << definitionCode;
@@ -296,3 +323,9 @@ void Video::findVideoUrl(int definitionCode) {
     // see you in gotHeadHeaders()
 
 }
+
+
+QString Video::formattedDuration() const {
+    QString format = m_duration > 3600 ? "h:mm:ss" : "m:ss";
+    return QTime().addSecs(m_duration).toString(format);
+}