X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fvideo.cpp;h=05d9761b58eb52ea899706c959587dd49114d466;hb=434d88418722fd7717038e44bd74271ca1d92771;hp=1e65242ca389531707ec680a974530caa2c7acbc;hpb=b72bf3da6f5f05a1c161799c1e0b46892dcc18ba;p=minitube diff --git a/src/video.cpp b/src/video.cpp index 1e65242..05d9761 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -1,331 +1,145 @@ -#include "video.h" -#include "networkaccess.h" -#include -#include "videodefinition.h" +/* $BEGIN_LICENSE -namespace The { - NetworkAccess* http(); -} +This file is part of Minitube. +Copyright 2009, Flavio Tordini -Video::Video() : m_duration(0), -m_viewCount(-1), -definitionCode(0), -elIndex(0), -loadingStreamUrl(false) -{ } +Minitube is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. -Video* Video::clone() { - Video* cloneVideo = new Video(); - 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_thumbnailUrl = m_thumbnailUrl; - cloneVideo->m_mediumThumbnailUrl = m_mediumThumbnailUrl; - cloneVideo->m_duration = m_duration; - cloneVideo->m_published = m_published; - cloneVideo->m_viewCount = m_viewCount; - cloneVideo->videoId = videoId; - cloneVideo->videoToken = videoToken; - cloneVideo->definitionCode = definitionCode; - return cloneVideo; -} +Minitube is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. -void Video::setWebpage(QUrl webpage) { - m_webpage = webpage; +You should have received a copy of the GNU General Public License +along with Minitube. If not, see . - // 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); -} +$END_LICENSE */ -void Video::loadThumbnail() { - QObject *reply = The::http()->get(m_thumbnailUrl); - connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray))); -} +#include "video.h" +#include "datautils.h" +#include "http.h" +#include "httputils.h" +#include "jsfunctions.h" +#include "playlistitemdelegate.h" +#include "videodefinition.h" +#include "ytvideo.h" -void Video::setThumbnail(QByteArray bytes) { - m_thumbnail.loadFromData(bytes); - m_thumbnail = m_thumbnail.scaled(160, 90); - emit gotThumbnail(); -} +Video::Video() + : duration(0), viewCount(-1), license(LicenseYouTube), definitionCode(0), + loadingThumbnail(false), ytVideo(nullptr) {} -void Video::loadMediumThumbnail() { - if (m_mediumThumbnailUrl.isEmpty()) return; - QObject *reply = The::http()->get(m_mediumThumbnailUrl); - connect(reply, SIGNAL(data(QByteArray)), SIGNAL(gotMediumThumbnail(QByteArray))); +Video::~Video() { + qDebug() << "Deleting" << id; } -void Video::loadStreamUrl() { - if (loadingStreamUrl) { - qDebug() << "Already loading stream URL for" << this->title(); - return; - } - loadingStreamUrl = true; - - // https://develop.participatoryculture.org/trac/democracy/browser/trunk/tv/portable/flashscraper.py - getVideoInfo(); +Video *Video::clone() { + Video *clone = new Video(); + clone->title = title; + clone->description = description; + clone->channelTitle = channelTitle; + clone->channelId = channelId; + clone->webpage = webpage; + clone->streamUrl = streamUrl; + clone->thumbnail = thumbnail; + clone->thumbnailUrl = thumbnailUrl; + clone->mediumThumbnailUrl = mediumThumbnailUrl; + clone->duration = duration; + clone->formattedDuration = formattedDuration; + clone->published = published; + clone->formattedPublished = formattedPublished; + clone->viewCount = viewCount; + clone->formattedViewCount = formattedViewCount; + clone->id = id; + clone->definitionCode = definitionCode; + return clone; } -void Video::getVideoInfo() { - static const QStringList elTypes = QStringList() << "&el=embedded" << "&el=vevo" << "&el=detailpage" << ""; - - if (elIndex > elTypes.size() - 1) { - loadingStreamUrl = false; - emit errorStreamUrl("Cannot get video info"); - /* - // Don't panic! We have a plan B. - // get the youtube video webpage - qDebug() << "Scraping" << webpage().toString(); - QObject *reply = The::http()->get(webpage().toString()); - connect(reply, SIGNAL(data(QByteArray)), SLOT(scrapeWebPage(QByteArray))); - connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*))); - // see you in scrapWebPage(QByteArray) - */ - return; - } - - // Get Video Token - QUrl videoInfoUrl = QUrl(QString( - "http://www.youtube.com/get_video_info?video_id=%1%2&ps=default&eurl=&gl=US&hl=en" - ).arg(videoId, elTypes.at(elIndex))); - - QObject *reply = The::http()->get(videoInfoUrl); - connect(reply, SIGNAL(data(QByteArray)), SLOT(gotVideoInfo(QByteArray))); - connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*))); - - // see you in gotVideoInfo... - +const QString &Video::getWebpage() { + if (webpage.isEmpty() && !id.isEmpty()) + webpage.append("https://www.youtube.com/watch?v=").append(id); + return webpage; } -void Video::gotVideoInfo(QByteArray data) { - QString videoInfo = QString::fromUtf8(data); - - // qDebug() << "videoInfo" << videoInfo; - - // get video token - QRegExp re = QRegExp("^.*&token=([^&]+).*$"); - bool match = re.exactMatch(videoInfo); - // handle regexp failure - if (!match || re.numCaptures() < 1) { - // Don't panic! We're gonna try another magic "el" param - elIndex++; - getVideoInfo(); - return; - } - QString videoToken = re.cap(1); - while (videoToken.contains('%')) - videoToken = QByteArray::fromPercentEncoding(videoToken.toAscii()); - // qDebug() << "videoToken" << videoToken; - this->videoToken = videoToken; - - // get fmt_url_map - re = QRegExp("^.*&url_encoded_fmt_stream_map=([^&]+).*$"); - match = re.exactMatch(videoInfo); - // handle regexp failure - if (!match || re.numCaptures() < 1) { - // Don't panic! We're gonna try another magic "el" param - elIndex++; - getVideoInfo(); - return; - } - - QString fmtUrlMap = re.cap(1); - fmtUrlMap = QByteArray::fromPercentEncoding(fmtUrlMap.toUtf8()); - - QSettings settings; - QString definitionName = settings.value("definition").toString(); - int definitionCode = VideoDefinition::getDefinitionCode(definitionName); - - // qDebug() << "fmtUrlMap" << fmtUrlMap; - QStringList formatUrls = fmtUrlMap.split(",", QString::SkipEmptyParts); - QHash urlMap; - foreach(QString formatUrl, formatUrls) { - // 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; +void Video::setWebpage(const QString &value) { + webpage = value; - if (format == definitionCode) { - qDebug() << "Found format" << definitionCode; - QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode); - m_streamUrl = videoUrl; - this->definitionCode = definitionCode; - emit gotStreamUrl(videoUrl); - loadingStreamUrl = false; - return; - } - - urlMap.insert(format, url); - } - - QList definitionCodes = VideoDefinition::getDefinitionCodes(); - int currentIndex = definitionCodes.indexOf(definitionCode); - int previousIndex = 0; - while (currentIndex >= 0) { - previousIndex = currentIndex - 1; - if (previousIndex < 0) previousIndex = 0; - int definitionCode = definitionCodes.at(previousIndex); - if (urlMap.contains(definitionCode)) { - qDebug() << "Found format" << definitionCode; - QString url = urlMap.value(definitionCode); - QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode); - m_streamUrl = videoUrl; - this->definitionCode = definitionCode; - emit gotStreamUrl(videoUrl); - loadingStreamUrl = false; + // Get Video ID + if (id.isEmpty()) { + QRegExp re(JsFunctions::instance()->videoIdRE()); + if (re.indexIn(webpage) == -1) { + qWarning() << QString("Cannot get video id for %1").arg(webpage); + // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString())); + // loadingStreamUrl = false; return; } - currentIndex--; + id = re.cap(1); } - - emit errorStreamUrl(tr("Cannot get video stream for %1").arg(m_webpage.toString())); - } -void Video::foundVideoUrl(QString videoToken, int definitionCode) { - // qDebug() << "foundVideoUrl" << videoToken << definitionCode; +void Video::loadThumbnail() { + if (thumbnailUrl.isEmpty() || loadingThumbnail) return; + loadingThumbnail = true; + QObject *reply = HttpUtils::yt().get(thumbnailUrl); + connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray))); +} - QUrl videoUrl = QUrl(QString( - "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3" - ).arg(videoId, videoToken, QString::number(definitionCode))); +void Video::setDuration(int value) { + duration = value; + formattedDuration = DataUtils::formatDuration(duration); +} - m_streamUrl = videoUrl; - loadingStreamUrl = false; - emit gotStreamUrl(videoUrl); +void Video::setViewCount(int value) { + viewCount = value; + formattedViewCount = DataUtils::formatCount(viewCount); } -void Video::errorVideoInfo(QNetworkReply *reply) { - loadingStreamUrl = false; - emit errorStreamUrl(tr("Network error: %1 for %2").arg(reply->errorString(), reply->url().toString())); +void Video::setPublished(const QDateTime &value) { + published = value; + formattedPublished = DataUtils::formatDateTime(published); } -void Video::scrapeWebPage(QByteArray data) { +void Video::setThumbnail(const QByteArray &bytes) { + qreal ratio = qApp->devicePixelRatio(); + thumbnail.loadFromData(bytes); + thumbnail.setDevicePixelRatio(ratio); + const int thumbWidth = PlaylistItemDelegate::thumbWidth * ratio; + if (thumbnail.width() > thumbWidth) + thumbnail = thumbnail.scaledToWidth(thumbWidth, Qt::SmoothTransformation); + emit gotThumbnail(); + loadingThumbnail = false; +} - QString videoHTML = QString::fromUtf8(data); - QRegExp re(".*, \"t\": \"([^\"]+)\".*"); - bool match = re.exactMatch(videoHTML); +void Video::streamUrlLoaded(const QString &streamUrl, const QString &audioUrl) { + qDebug() << "Streams loaded"; + definitionCode = ytVideo->getDefinitionCode(); + this->streamUrl = streamUrl; + emit gotStreamUrl(streamUrl, audioUrl); + ytVideo->deleteLater(); + ytVideo = nullptr; +} - // on regexp failure, stop and report error - if (!match || re.numCaptures() < 1) { - emit errorStreamUrl("Error parsing video page"); - loadingStreamUrl = false; +void Video::loadStreamUrl() { + if (ytVideo) { + qDebug() << "Already loading" << id; return; } - - QString videoToken = re.cap(1); - // FIXME proper decode - videoToken = videoToken.replace("%3D", "="); - - // we'll need this in gotHeadHeaders() - this->videoToken = videoToken; - - // qDebug() << "token" << videoToken; - - QSettings settings; - QString definitionName = settings.value("definition").toString(); - int definitionCode = VideoDefinition::getDefinitionCode(definitionName); - if (definitionCode == 18) { - // This is assumed always available - foundVideoUrl(videoToken, 18); - } else { - findVideoUrl(definitionCode); - } - + ytVideo = new YTVideo(id, this); + connect(ytVideo, &YTVideo::gotStreamUrl, this, &Video::streamUrlLoaded); + connect(ytVideo, &YTVideo::errorStreamUrl, this, [this](const QString &msg) { + emit errorStreamUrl(msg); + ytVideo->deleteLater(); + ytVideo = nullptr; + }); + ytVideo->loadStreamUrl(); } -void Video::gotHeadHeaders(QNetworkReply* reply) { - int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - // qDebug() << "gotHeaders" << statusCode; - if (statusCode == 200) { - foundVideoUrl(videoToken, definitionCode); - } else { - - // try next (lower quality) definition - /* - QStringList definitionNames = VideoDefinition::getDefinitionNames(); - int currentIndex = definitionNames.indexOf(currentDefinition); - int previousIndex = 0; - if (currentIndex > 0) { - previousIndex = currentIndex - 1; - } - if (previousIndex > 0) { - QString nextDefinitionName = definitionNames.at(previousIndex); - findVideoUrl(nextDefinitionName); - } else { - foundVideoUrl(videoToken, 18); - }*/ - - - QList definitionCodes = VideoDefinition::getDefinitionCodes(); - int currentIndex = definitionCodes.indexOf(definitionCode); - int previousIndex = 0; - if (currentIndex > 0) { - previousIndex = currentIndex - 1; - int definitionCode = definitionCodes.at(previousIndex); - if (definitionCode == 18) { - // This is assumed always available - foundVideoUrl(videoToken, 18); - } else { - findVideoUrl(definitionCode); - } - - } else { - foundVideoUrl(videoToken, 18); - } - +void Video::abortLoadStreamUrl() { + if (ytVideo) { + ytVideo->disconnect(this); + ytVideo->deleteLater(); + ytVideo = nullptr; } } - -void Video::findVideoUrl(int definitionCode) { - this->definitionCode = definitionCode; - - QUrl videoUrl = QUrl(QString( - "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3" - ).arg(videoId, videoToken, QString::number(definitionCode))); - - QObject *reply = The::http()->head(videoUrl); - connect(reply, SIGNAL(finished(QNetworkReply*)), SLOT(gotHeadHeaders(QNetworkReply*))); - // connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*))); - - // 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); -}