-
Notifications
You must be signed in to change notification settings - Fork 4
/
video.cpp
165 lines (142 loc) · 4.06 KB
/
video.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "video.h"
#include <QtCore>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QMap>
QList<Meta> Video::getAvailaibleFormats() const
{
return availaibleFormats;
}
void Video::setAvailaibleFormats(const QList<Meta> &value)
{
availaibleFormats = value;
}
QString Video::getVideoId() const
{
return videoId;
}
void Video::setVideoId(const QString &value)
{
videoId = value;
}
QString Video::getTitle() const
{
return title;
}
void Video::setTitle(const QString &value)
{
title = value;
}
QString Video::getAuthor() const
{
return author;
}
void Video::setAuthor(const QString &value)
{
author = value;
}
QString Video::getThumbnail() const
{
return thumbnail;
}
void Video::setThumbnail(const QString &value)
{
thumbnail = value;
}
QString Video::getUrl() const
{
return url;
}
void Video::setUrl(const QString &value)
{
url = value;
}
QString Video::getLength() const
{
return length;
}
void Video::setLength(const QString &value)
{
length = value;
}
Video::Video()
{
}
Video::Video(QJsonObject &videoData)
{
this->videoId = videoData["args"].toObject()["video_id"].toString();
this->title = videoData["args"].toObject()["title"].toString();
this->author = videoData["args"].toObject()["author"].toString();
this->thumbnail = videoData["args"].toObject()["thumbnail_url"].toString();
this->url = videoData["args"].toObject()["loadedUrl"].toString();
this->length = videoData["args"].toObject()["length_seconds"].toString();
QString urlEncodedFmtStreamMap = videoData["args"].toObject()["url_encoded_fmt_stream_map"].toString();
QStringList availaibleStreams = urlEncodedFmtStreamMap.split(",");
Q_FOREACH(QString stream, availaibleStreams) {
QStringList parameters = stream.split("&");
QMap<QString, QString> map;
Q_FOREACH(QString parameter, parameters) {
QStringList keyValue = parameter.split("=");
map.insert(keyValue.at(0), QUrl::fromPercentEncoding(keyValue.at(1).toUtf8()));
qDebug() << keyValue.at(0) << " - " << QUrl::fromPercentEncoding(keyValue.at(1).toUtf8());
}
Meta meta;
meta.setItag(map["itag"].toInt());
meta.setUrl(map["url"]);
QString extension, resolution;
switch (meta.getItag()) {
case 5:
extension = "flv";
resolution = "400X240";
break;
case 17:
extension = "3gp";
resolution = "320X240";
break;
case 18:
extension = "mp4";
resolution = "640X360";
break;
case 22:
extension = "mp4";
resolution = "1280X720";
break;
case 36:
extension = "3gp";
resolution = "320X240";
break;
case 43:
extension = "webm";
resolution = "640X360";
break;
default:
extension = "mp4";
resolution = "";
break;
}
meta.setExtension(extension);
meta.setResolution(resolution);
meta.findVideoSize();
// this->availaibleFormats[meta.getItag()] = meta;
this->availaibleFormats.push_back(meta);
// qDebug() << "\n\n";
}
// TODO : Handle adaptive formats
// Converting length form seconds to hh:mm:ss
// TODO : move to utils
int ss = this->getLength().toInt();
int mm = ss/60; ss = ss%60;
int hh = mm/60; mm = mm%60;
this->setLength(QString("%1").arg(hh, 2, 10, QChar('0')) + ":" + QString("%1").arg(mm, 2, 10, QChar('0')) + ":" + QString("%1").arg(ss, 2, 10, QChar('0')));
}
void Video::printInfo()
{
qDebug() << "Title : " << this->title;
qDebug() << "Author : " << this->author;
qDebug() << "Length : " << this->length;
qDebug() << "Video Id : " << this->videoId;
Q_FOREACH(Meta meta, this->availaibleFormats) {
qDebug() << meta.getItag() << " " << meta.getHumanReadableSize();
}
}