forked from KikoPlayProject/KikoPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmediainfo.cpp
More file actions
225 lines (220 loc) · 7.48 KB
/
mediainfo.cpp
File metadata and controls
225 lines (220 loc) · 7.48 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "mediainfo.h"
#include <QTextEdit>
#include <QHBoxLayout>
#include "globalobjects.h"
#include "Play/Video/mpvplayer.h"
MediaInfo::MediaInfo(QWidget *parent) : CFramelessDialog(tr("Media Info"),parent)
{
QTextEdit *infoText=new QTextEdit(this);
infoText->setReadOnly(true);
infoText->setText(expandMediaInfo());
QHBoxLayout *infoHLayout=new QHBoxLayout(this);
infoHLayout->setContentsMargins(0,0,0,0);
infoText->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding);
infoHLayout->addWidget(infoText);
resize(520*logicalDpiX()/96,400*logicalDpiY()/96);
}
QString MediaInfo::expandMediaInfo()
{
static QString mediaInfoTemplate;
if(mediaInfoTemplate.isEmpty())
{
QFile infoTempFile(":/res/mediainfo");
infoTempFile.open(QFile::ReadOnly);
if(infoTempFile.isOpen())
{
mediaInfoTemplate = infoTempFile.readAll();
}
}
int state = 0;
QVector<TextBlock> textStack;
QStringList commandStack;
textStack.push_back(TextBlock());
for(QChar c : mediaInfoTemplate)
{
if(state == 0)
{
if(c=='$')
{
state = 1;
}
else textStack.back().text.append(c);
}
else if(state == 1)
{
if(c=='{')
{
state=2;
commandStack.push_back("");
}
else if(!commandStack.isEmpty())
{
state = 2;
commandStack.back().append(c);
}
else
{
state=0;
textStack.back().text.append("$");
textStack.back().text.append(c);
}
}
else if(state == 2)
{
if(c=='}')
{
evalCommand(commandStack, textStack);
if(commandStack.isEmpty())
state=0;
}
else if(c=='$')
{
state = 1;
}
else
{
commandStack.back().append(c);
}
}
}
return GlobalObjects::mpvplayer->expandMediaInfo(textStack.back().text);
}
void MediaInfo::evalCommand(QStringList &commandStack, QVector<TextBlock> &textStack)
{
static QHash<QString, QString> kikoCommand({
{"normalfont", GlobalObjects::normalFont},
{"tr:title", tr("Title")},
{"tr:file-size", tr("File Size")},
{"tr:date-created", tr("Date Created")},
{"tr:file", tr("File")},
{"tr:file-format", tr("File Format")},
{"tr:duration", tr("Duration")},
{"tr:video-output", tr("Video Output")},
{"tr:demuxer", tr("Demuxer")},
{"tr:bitrate", tr("Bitrate")},
{"tr:video", tr("Video")},
{"tr:audio-output", tr("Audio Output")},
{"tr:sample-rate", tr("Sample Rate")},
{"tr:channels", tr("Channels")},
{"tr:audio", tr("Audio")},
{"tr:meta-data", tr("Meta Data")},
{"tr:video-format", tr("Video Format")},
{"tr:video-size", tr("Video Size")},
{"tr:display-size", tr("Display Size")},
{"tr:audio-format", tr("Audio Format")},
{"tr:channel-count", tr("Channel Count")},
{"tr:chapters", tr("Chapters")},
{"tr:tracks", tr("Tracks")},
{"tr:language", tr("Language")},
{"tr:selected", tr("Selected")},
{"tr:editions", tr("Editions")}
});
QFileInfo fi(GlobalObjects::mpvplayer->getCurrentFile());
kikoCommand["date-created"] = fi.birthTime().toString();
kikoCommand["audio-trackcount"] = QString::number(GlobalObjects::mpvplayer->getTrackList(MPVPlayer::AudioTrack).size());
kikoCommand["dwidth"] = QString::number(GlobalObjects::mpvplayer->width());
kikoCommand["dheight"] = QString::number(GlobalObjects::mpvplayer->height());
QString command = commandStack.back();
commandStack.pop_back();
QString commandResult;
if(command.startsWith("kiko:") && kikoCommand.contains(command.mid(5))) {
commandResult = kikoCommand[command.mid(5)];
} else if(command.startsWith("mpv:")) {
int errCode = 0;
QString mpvProperty(GlobalObjects::mpvplayer->getMPVProperty(command.mid(4), errCode));
if(errCode >= 0) commandResult = mpvProperty;
} else if(command.startsWith("loop:")) {
QString loop = command.mid(5);
int varPos = loop.indexOf('=');
do
{
if(varPos <= 0) break;
QString loopVar = loop.left(varPos).trimmed();
if(loopVar.isEmpty()) break;
QStringList loopConds = loop.mid(varPos+1).split(',');
if(loopConds.size()<2) break;
bool ok;
int start = loopConds[0].toInt(&ok);
if(!ok) break;
int end = loopConds[1].toInt(&ok);
if(!ok) break;
int step = 1;
if(loopConds.size()>2)
{
step = loopConds[2].toInt(&ok);
if(!ok) break;
}
TextBlock block;
block.blockVar = loopVar;
block.start = start; block.end = end; block.step = step;
textStack.push_back(block);
}while(false);
return;
} else if(command.startsWith("endloop")) {
if(textStack.size()>1)
{
auto &block = textStack.back();
QString content;
QString var = QString("${var:%1}").arg(block.blockVar);
for(int i=block.start; block.start<=block.end?(i<block.end):(i>=block.end);i+=block.step)
{
content.append(QString(block.text).replace(var, QString::number(i)));
}
textStack.pop_back();
textStack.back().text.append(content);
}
return;
} else if(command.startsWith("if:")) {
QString ifcond = command.mid(3);
int lhsPos = ifcond.indexOf(QRegExp("([<>=])|(>=)|(<=)"));
do
{
if(lhsPos <= 0) break;
QChar op = ifcond[lhsPos];
QString lhs = ifcond.left(lhsPos).trimmed();
if(lhs.isEmpty()) break;
int rhsPos = lhsPos + 1;
if(rhsPos >= ifcond.length()) break;
bool hasEq = false;
if(rhsPos + 1<ifcond.length() && ifcond[rhsPos]=='=')
{
rhsPos += 1;
hasEq = true;
}
QString rhs = ifcond.mid(rhsPos).trimmed();
if(rhs.isEmpty()) break;
bool lNum = true, rNum = true;
double ilhs = lhs.toDouble(&lNum);
double irhs = rhs.toDouble(&rNum);
TextBlock block;
if(lNum && rNum)
{
block.cond = cp(ilhs, irhs, op.toLatin1(), hasEq);
}
else
{
block.cond = cp(lhs, rhs, op.toLatin1(), hasEq);
}
textStack.push_back(block);
}while(false);
return;
} else if(command.startsWith("endif")) {
if(textStack.size()>1)
{
QString content = textStack.back().text;
bool cond = textStack.back().cond;
textStack.pop_back();
if(cond)
{
textStack.back().text.append(content);
}
}
return;
} else {
commandResult = QString("${%1}").arg(command);
}
if(commandStack.empty())
textStack.back().text.append(commandResult);
else
commandStack.back().append(commandResult);
}