]> git.sur5r.net Git - minitube/blob - src/video.cpp
Imported Upstream version 2.1.5
[minitube] / src / video.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "video.h"
22 #include "networkaccess.h"
23 #include <QtNetwork>
24 #include "videodefinition.h"
25 #include "jsfunctions.h"
26
27 namespace The {
28 NetworkAccess* http();
29 }
30
31 Video::Video() : m_duration(0),
32     m_viewCount(-1),
33     definitionCode(0),
34     elIndex(0),
35     ageGate(false),
36     m_license(LicenseYouTube),
37     loadingStreamUrl(false),
38     loadingThumbnail(false)
39 { }
40
41 Video* Video::clone() {
42     Video* cloneVideo = new Video();
43     cloneVideo->m_title = m_title;
44     cloneVideo->m_description = m_description;
45     cloneVideo->m_author = m_author;
46     cloneVideo->m_userId = m_userId;
47     cloneVideo->m_webpage = m_webpage;
48     cloneVideo->m_streamUrl = m_streamUrl;
49     cloneVideo->m_thumbnail = m_thumbnail;
50     cloneVideo->m_thumbnailUrl = m_thumbnailUrl;
51     cloneVideo->m_mediumThumbnailUrl = m_mediumThumbnailUrl;
52     cloneVideo->m_duration = m_duration;
53     cloneVideo->m_published = m_published;
54     cloneVideo->m_viewCount = m_viewCount;
55     cloneVideo->videoId = videoId;
56     cloneVideo->videoToken = videoToken;
57     cloneVideo->definitionCode = definitionCode;
58     return cloneVideo;
59 }
60
61 void Video::setWebpage(QUrl webpage) {
62     m_webpage = webpage;
63
64     // Get Video ID
65     // youtube-dl line 428
66     // QRegExp re("^((?:http://)?(?:\\w+\\.)?youtube\\.com/(?:(?:v/)|(?:(?:watch(?:\\.php)?)?\\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$");
67     QRegExp re("^https?://www\\.youtube\\.com/watch\\?v=([0-9A-Za-z_-]+).*");
68     bool match = re.exactMatch(m_webpage.toString());
69     if (!match || re.numCaptures() < 1) {
70         qWarning() << QString("Cannot get video id for %1").arg(m_webpage.toString());
71         // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
72         // loadingStreamUrl = false;
73         return;
74     }
75     videoId = re.cap(1);
76 }
77
78 void Video::loadThumbnail() {
79     if (m_thumbnailUrl.isEmpty() || loadingThumbnail) return;
80     loadingThumbnail = true;
81     QObject *reply = The::http()->get(m_thumbnailUrl);
82     connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
83 }
84
85 void Video::setThumbnail(QByteArray bytes) {
86     loadingThumbnail = false;
87     m_thumbnail.loadFromData(bytes);
88     if (m_thumbnail.width() > 160)
89         m_thumbnail = m_thumbnail.scaledToWidth(160, Qt::SmoothTransformation);
90     emit gotThumbnail();
91 }
92
93 void Video::loadMediumThumbnail() {
94     if (m_mediumThumbnailUrl.isEmpty()) return;
95     QObject *reply = The::http()->get(m_mediumThumbnailUrl);
96     connect(reply, SIGNAL(data(QByteArray)), SIGNAL(gotMediumThumbnail(QByteArray)));
97 }
98
99 void Video::loadStreamUrl() {
100     if (loadingStreamUrl) {
101         qDebug() << "Already loading stream URL for" << this->title();
102         return;
103     }
104     loadingStreamUrl = true;
105     elIndex = 0;
106     ageGate = false;
107
108     getVideoInfo();
109 }
110
111 void  Video::getVideoInfo() {
112     static const QStringList elTypes = QStringList() << "&el=embedded" << "&el=detailpage" << "&el=vevo" << "";
113
114     QUrl videoInfoUrl;
115
116     if (elIndex == elTypes.size()) {
117         // qDebug() << "Trying special embedded el param";
118         videoInfoUrl = QUrl("http://www.youtube.com/get_video_info");
119         videoInfoUrl.addQueryItem("video_id", videoId);
120         videoInfoUrl.addQueryItem("el", "embedded");
121         videoInfoUrl.addQueryItem("gl", "US");
122         videoInfoUrl.addQueryItem("hl", "en");
123         videoInfoUrl.addQueryItem("eurl", "https://youtube.googleapis.com/v/" + videoId);
124         videoInfoUrl.addQueryItem("asv", "3");
125         videoInfoUrl.addQueryItem("sts", "1588");
126     } else if (elIndex > elTypes.size() - 1) {
127         qWarning() << "Cannot get video info";
128         loadingStreamUrl = false;
129         emit errorStreamUrl("Cannot get video info");
130         return;
131     } else {
132         // qDebug() << "Trying el param:" << elTypes.at(elIndex) << elIndex;
133         videoInfoUrl = QUrl(QString(
134                                 "http://www.youtube.com/get_video_info?video_id=%1%2&ps=default&eurl=&gl=US&hl=en"
135                                 ).arg(videoId, elTypes.at(elIndex)));
136     }
137
138     QObject *reply = The::http()->get(videoInfoUrl);
139     connect(reply, SIGNAL(data(QByteArray)), SLOT(gotVideoInfo(QByteArray)));
140     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
141
142     // see you in gotVideoInfo...
143 }
144
145 void  Video::gotVideoInfo(QByteArray data) {
146     QString videoInfo = QString::fromUtf8(data);
147     // qDebug() << "videoInfo" << videoInfo;
148
149     // get video token
150     QRegExp re = QRegExp("^.*&token=([^&]+).*$");
151     bool match = re.exactMatch(videoInfo);
152     // handle regexp failure
153     if (!match || re.numCaptures() < 1) {
154         // qDebug() << "Cannot get token. Trying next el param";
155         // Don't panic! We're gonna try another magic "el" param
156         elIndex++;
157         getVideoInfo();
158         return;
159     }
160
161     QString videoToken = re.cap(1);
162     while (videoToken.contains('%'))
163         videoToken = QByteArray::fromPercentEncoding(videoToken.toAscii());
164     // qDebug() << "videoToken" << videoToken;
165     this->videoToken = videoToken;
166
167     // get fmt_url_map
168     re = QRegExp("^.*&url_encoded_fmt_stream_map=([^&]+).*$");
169     match = re.exactMatch(videoInfo);
170     // handle regexp failure
171     if (!match || re.numCaptures() < 1) {
172         // qDebug() << "Cannot get urlMap. Trying next el param";
173         // Don't panic! We're gonna try another magic "el" param
174         elIndex++;
175         getVideoInfo();
176         return;
177     }
178
179     // qDebug() << "Got token and urlMap" << elIndex;
180
181     QString fmtUrlMap = re.cap(1);
182     fmtUrlMap = QByteArray::fromPercentEncoding(fmtUrlMap.toUtf8());
183     parseFmtUrlMap(fmtUrlMap);
184 }
185
186 void Video::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) {
187     QSettings settings;
188     QString definitionName = settings.value("definition", "360p").toString();
189     int definitionCode = VideoDefinition::getDefinitionCode(definitionName);
190
191     // qDebug() << "fmtUrlMap" << fmtUrlMap;
192     QStringList formatUrls = fmtUrlMap.split(',', QString::SkipEmptyParts);
193     QHash<int, QString> urlMap;
194     foreach(QString formatUrl, formatUrls) {
195         // qDebug() << "formatUrl" << formatUrl;
196         QStringList urlParams = formatUrl.split('&', QString::SkipEmptyParts);
197         // qDebug() << "urlParams" << urlParams;
198
199         int format = -1;
200         QString url;
201         QString sig;
202         foreach(QString urlParam, urlParams) {
203             // qWarning() << urlParam;
204             if (urlParam.startsWith("itag=")) {
205                 int separator = urlParam.indexOf("=");
206                 format = urlParam.mid(separator + 1).toInt();
207             } else if (urlParam.startsWith("url=")) {
208                 int separator = urlParam.indexOf("=");
209                 url = urlParam.mid(separator + 1);
210                 url = QByteArray::fromPercentEncoding(url.toUtf8());
211             } else if (urlParam.startsWith("sig=")) {
212                 int separator = urlParam.indexOf("=");
213                 sig = urlParam.mid(separator + 1);
214                 sig = QByteArray::fromPercentEncoding(sig.toUtf8());
215             } else if (urlParam.startsWith("s=")) {
216                 if (fromWebPage || ageGate) {
217                     int separator = urlParam.indexOf("=");
218                     sig = urlParam.mid(separator + 1);
219                     sig = QByteArray::fromPercentEncoding(sig.toUtf8());
220                     if (ageGate)
221                         sig = JsFunctions::instance()->decryptAgeSignature(sig);
222                     else {
223                         sig = decryptSignature(sig);
224                         if (sig.isEmpty())
225                             sig = JsFunctions::instance()->decryptSignature(sig);
226                     }
227                 } else {
228                     // qDebug() << "Loading webpage";
229                     QUrl url("http://www.youtube.com/watch");
230                     url.addQueryItem("v", videoId);
231                     url.addQueryItem("gl", "US");
232                     url.addQueryItem("hl", "en");
233                     url.addQueryItem("has_verified", "1");
234                     QObject *reply = The::http()->get(url);
235                     connect(reply, SIGNAL(data(QByteArray)), SLOT(scrapeWebPage(QByteArray)));
236                     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
237                     // see you in scrapWebPage(QByteArray)
238                     return;
239                 }
240             }
241         }
242         if (format == -1 || url.isNull()) continue;
243
244         url += "&signature=" + sig;
245
246         if (!url.contains("ratebypass"))
247             url += "&ratebypass=yes";
248
249         // qWarning() << url;
250
251         if (format == definitionCode) {
252             qDebug() << "Found format" << definitionCode;
253             QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
254             m_streamUrl = videoUrl;
255             this->definitionCode = definitionCode;
256             emit gotStreamUrl(videoUrl);
257             loadingStreamUrl = false;
258             return;
259         }
260
261         urlMap.insert(format, url);
262     }
263
264     QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
265     int currentIndex = definitionCodes.indexOf(definitionCode);
266     int previousIndex = 0;
267     while (currentIndex >= 0) {
268         previousIndex = currentIndex - 1;
269         if (previousIndex < 0) previousIndex = 0;
270         int definitionCode = definitionCodes.at(previousIndex);
271         if (urlMap.contains(definitionCode)) {
272             qDebug() << "Found format" << definitionCode;
273             QString url = urlMap.value(definitionCode);
274             QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
275             m_streamUrl = videoUrl;
276             this->definitionCode = definitionCode;
277             emit gotStreamUrl(videoUrl);
278             loadingStreamUrl = false;
279             return;
280         }
281         currentIndex--;
282     }
283
284     emit errorStreamUrl(tr("Cannot get video stream for %1").arg(m_webpage.toString()));
285 }
286
287 void Video::foundVideoUrl(QString videoToken, int definitionCode) {
288     // qDebug() << "foundVideoUrl" << videoToken << definitionCode;
289
290     QUrl videoUrl = QUrl(QString(
291                              "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
292                              ).arg(videoId, videoToken, QString::number(definitionCode)));
293
294     m_streamUrl = videoUrl;
295     loadingStreamUrl = false;
296     emit gotStreamUrl(videoUrl);
297 }
298
299 void Video::errorVideoInfo(QNetworkReply *reply) {
300     loadingStreamUrl = false;
301     emit errorStreamUrl(tr("Network error: %1 for %2").arg(reply->errorString(), reply->url().toString()));
302 }
303
304 void Video::scrapeWebPage(QByteArray data) {
305     QString html = QString::fromUtf8(data);
306     // qWarning() << html;
307
308     if (html.contains("player-age-gate-content\"")) {
309         // qDebug() << "Found ageGate";
310         ageGate = true;
311         elIndex = 4;
312         getVideoInfo();
313         return;
314     }
315
316     QRegExp re(".*\"url_encoded_fmt_stream_map\":\\s+\"([^\"]+)\".*");
317     bool match = re.exactMatch(html);
318     // on regexp failure, stop and report error
319     if (!match || re.numCaptures() < 1) {
320         qWarning() << "Error parsing video page";
321         // emit errorStreamUrl("Error parsing video page");
322         // loadingStreamUrl = false;
323         elIndex++;
324         getVideoInfo();
325         return;
326     }
327     fmtUrlMap = re.cap(1);
328     fmtUrlMap.replace("\\u0026", "&");
329     // parseFmtUrlMap(fmtUrlMap, true);
330
331     QRegExp jsPlayerRe("\"assets\":.+\"js\":\\s*\"([^\"]+)\"");
332     if (jsPlayerRe.indexIn(html) != -1) {
333         QString jsPlayerUrl = jsPlayerRe.cap(1);
334         jsPlayerUrl.remove('\\');
335         jsPlayerUrl = "http:" + jsPlayerUrl;
336         qWarning() << "jsPlayerUrl" << jsPlayerUrl;
337         /*
338         QRegExp jsPlayerIdRe("-(.+)\\.js");
339         jsPlayerIdRe.indexIn(jsPlayerUrl);
340         QString jsPlayerId = jsPlayerRe.cap(1);
341         */
342         QObject *reply = The::http()->get(jsPlayerUrl);
343         connect(reply, SIGNAL(data(QByteArray)), SLOT(parseJsPlayer(QByteArray)));
344         connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
345     }
346 }
347
348 void Video::gotHeadHeaders(QNetworkReply* reply) {
349     int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
350     // qDebug() << "gotHeaders" << statusCode;
351     if (statusCode == 200) {
352         foundVideoUrl(videoToken, definitionCode);
353     } else {
354
355         // try next (lower quality) definition
356         /*
357         QStringList definitionNames = VideoDefinition::getDefinitionNames();
358         int currentIndex = definitionNames.indexOf(currentDefinition);
359         int previousIndex = 0;
360         if (currentIndex > 0) {
361             previousIndex = currentIndex - 1;
362         }
363         if (previousIndex > 0) {
364             QString nextDefinitionName = definitionNames.at(previousIndex);
365             findVideoUrl(nextDefinitionName);
366         } else {
367             foundVideoUrl(videoToken, 18);
368         }*/
369
370
371         QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
372         int currentIndex = definitionCodes.indexOf(definitionCode);
373         int previousIndex = 0;
374         if (currentIndex > 0) {
375             previousIndex = currentIndex - 1;
376             int definitionCode = definitionCodes.at(previousIndex);
377             if (definitionCode == 18) {
378                 // This is assumed always available
379                 foundVideoUrl(videoToken, 18);
380             } else {
381                 findVideoUrl(definitionCode);
382             }
383
384         } else {
385             foundVideoUrl(videoToken, 18);
386         }
387
388     }
389 }
390
391 void Video::parseJsPlayer(QByteArray bytes) {
392     QString js = QString::fromUtf8(bytes);
393     QRegExp funcNameRe("signature=([a-zA-Z0-9]+)");
394     if (funcNameRe.indexIn(js) == -1) {
395         qWarning() << "Cannot capture signature function name";
396     } else {
397         sigFuncName = funcNameRe.cap(1);
398         captureFunction(sigFuncName, js);
399         // qDebug() << sigFunctions;
400     }
401     parseFmtUrlMap(fmtUrlMap, true);
402 }
403
404 void Video::captureFunction(const QString &name, const QString &js) {
405     QRegExp funcRe("function\\s+" + name + "\\s*\\([a-zA-Z0-9,\\s]*\\)\\s*\\{[^\\}]+\\}");
406     if (funcRe.indexIn(js) == -1) {
407         qWarning() << "Cannot capture function" << name;
408         return;
409     }
410     QString func = funcRe.cap(0);
411     sigFunctions.insert(name, func);
412
413     // capture inner functions
414     QRegExp invokedFuncRe("[\\s=;\\(]([a-zA-Z0-9]+)\\s*\\([a-zA-Z0-9, ]+\\)");
415     int pos = name.length() + 9;
416     while ((pos = invokedFuncRe.indexIn(func, pos)) != -1) {
417         QString funcName = invokedFuncRe.cap(1);
418         if (!sigFunctions.contains(funcName))
419             captureFunction(funcName, js);
420         pos += invokedFuncRe.matchedLength();
421     }
422 }
423
424 QString Video::decryptSignature(const QString &s) {
425     if (sigFuncName.isEmpty()) return QString();
426     QScriptEngine engine;
427     foreach (QString f, sigFunctions.values()) {
428         QScriptValue value = engine.evaluate(f);
429         if (value.isError())
430             qWarning() << "Error in" << f << value.toString();
431     }
432     QString js = sigFuncName + "('" + s + "');";
433     QScriptValue value = engine.evaluate(js);
434     if (value.isUndefined()) {
435         qWarning() << "Undefined result for" << js;
436         return QString();
437     }
438     if (value.isError()) {
439         qWarning() << "Error in" << js << value.toString();
440         return QString();
441     }
442     return value.toString();
443 }
444
445 void Video::findVideoUrl(int definitionCode) {
446     this->definitionCode = definitionCode;
447
448     QUrl videoUrl = QUrl(QString(
449                              "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
450                              ).arg(videoId, videoToken, QString::number(definitionCode)));
451
452     QObject *reply = The::http()->head(videoUrl);
453     connect(reply, SIGNAL(finished(QNetworkReply*)), SLOT(gotHeadHeaders(QNetworkReply*)));
454     // connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
455
456     // see you in gotHeadHeaders()
457 }
458
459 QString Video::formattedDuration() const {
460     QString format = m_duration > 3600 ? "h:mm:ss" : "m:ss";
461     return QTime().addSecs(m_duration).toString(format);
462 }