forked from mltframework/shotcut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
621 lines (580 loc) · 21 KB
/
util.cpp
File metadata and controls
621 lines (580 loc) · 21 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*
* Copyright (c) 2014-2022 Meltytech, LLC
*
* This program 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.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "util.h"
#include <QFileInfo>
#include <QWidget>
#include <QStringList>
#include <QFileInfo>
#include <QDir>
#include <QProcess>
#include <QUrl>
#include <QDesktopServices>
#include <QMessageBox>
#include <QMap>
#include <QDoubleSpinBox>
#include <QTemporaryFile>
#include <QApplication>
#include <QCryptographicHash>
#include <QtGlobal>
#include <QCameraInfo>
#include <MltChain.h>
#include <MltProducer.h>
#include <Logger.h>
#include "shotcut_mlt_properties.h"
#include "qmltypes/qmlapplication.h"
#include "proxymanager.h"
#include <memory>
#ifdef Q_OS_WIN
#include <windows.h>
#endif
#ifdef Q_OS_MAC
static const unsigned int kLowMemoryThresholdPercent = 10U;
#else
static const unsigned int kLowMemoryThresholdKB = 256U * 1024U;
#endif
QString Util::baseName(const QString &filePath, bool trimQuery)
{
QString s = filePath;
// Only if absolute path and not a URI.
if (s.startsWith('/') || s.midRef(1, 2) == ":/" || s.midRef(1, 2) == ":\\")
s = QFileInfo(s).fileName();
if (trimQuery) {
return removeQueryString(s);
}
return s;
}
void Util::setColorsToHighlight(QWidget *widget, QPalette::ColorRole role)
{
if (role == QPalette::Base) {
widget->setStyleSheet(
"QLineEdit {"
"font-weight: bold;"
"background-color: palette(highlight);"
"color: palette(highlighted-text);"
"selection-background-color: palette(alternate-base);"
"selection-color: palette(text);"
"}"
"QLineEdit:hover {"
"border: 2px solid palette(button-text);"
"}"
);
} else {
QPalette palette = QApplication::palette();
palette.setColor(role, palette.color(palette.Highlight));
palette.setColor(role == QPalette::Button ? QPalette::ButtonText : QPalette::WindowText,
palette.color(palette.HighlightedText));
widget->setPalette(palette);
widget->setAutoFillBackground(true);
}
}
void Util::showInFolder(const QString &path)
{
QFileInfo info(removeQueryString(path));
#if defined(Q_OS_WIN)
QStringList args;
if (!info.isDir())
args << "/select,";
args << QDir::toNativeSeparators(path);
if (QProcess::startDetached("explorer", args))
return;
#elif defined(Q_OS_MAC)
QStringList args;
args << "-e";
args << "tell application \"Finder\"";
args << "-e";
args << "activate";
args << "-e";
args << "select POSIX file \"" + path + "\"";
args << "-e";
args << "end tell";
#if !defined(QT_DEBUG)
args << "-e";
args << "return";
#endif
if (!QProcess::execute("/usr/bin/osascript", args))
return;
#endif
QDesktopServices::openUrl(QUrl::fromLocalFile(info.isDir() ? path : info.path()));
}
bool Util::warnIfNotWritable(const QString &filePath, QWidget *parent, const QString &caption)
{
// Returns true if not writable.
if (!filePath.isEmpty() && !filePath.contains("://")) {
QFileInfo info(filePath);
if (!info.isDir()) {
info = QFileInfo(info.dir().path());
}
if (!info.isWritable()) {
info = QFileInfo(filePath);
QMessageBox::warning(parent, caption,
QObject::tr("Unable to write file %1\n"
"Perhaps you do not have permission.\n"
"Try again with a different folder.")
.arg(info.fileName()));
return true;
}
}
return false;
}
QString Util::producerTitle(const Mlt::Producer &producer)
{
QString result;
Mlt::Producer &p = const_cast<Mlt::Producer &>(producer);
if (!p.is_valid() || p.is_blank()) return result;
if (p.get(kShotcutTransitionProperty))
return QObject::tr("Transition");
if (p.get(kTrackNameProperty))
return QObject::tr("Track: %1").arg(QString::fromUtf8(p.get(kTrackNameProperty)));
if (mlt_service_tractor_type == p.type())
return QObject::tr("Output");
if (p.get(kShotcutCaptionProperty))
return QString::fromUtf8(p.get(kShotcutCaptionProperty));
return Util::baseName(ProxyManager::resource(p));
}
QString Util::removeFileScheme(QUrl &url)
{
QString path = url.url();
if (url.scheme() == "file")
path = url.toString(QUrl::PreferLocalFile);
return QUrl::fromPercentEncoding(path.toUtf8());
}
static inline bool isValidGoProFirstFilePrefix(const QFileInfo &info)
{
QStringList list {"GOPR", "GH01", "GL01", "GM01", "GS01", "GX01"};
return list.contains(info.baseName().left(4).toUpper());
}
static inline bool isValidGoProPrefix(const QFileInfo &info)
{
QStringList list {"GP", "GH", "GL", "GM", "GS", "GX"};
return list.contains(info.baseName().left(2).toUpper());
}
static inline bool isValidGoProSuffix(const QFileInfo &info)
{
QStringList list {"MP4", "LRV", "360", "WAV"};
return list.contains(info.suffix().toUpper());
}
const QStringList Util::sortedFileList(const QList<QUrl> &urls)
{
QStringList result;
QMap<QString, QStringList> goproFiles;
// First look for GoPro main files.
foreach (QUrl url, urls) {
QFileInfo fi(removeFileScheme(url));
if (fi.baseName().size() == 8 && isValidGoProSuffix(fi) && isValidGoProFirstFilePrefix(fi)) {
goproFiles[fi.baseName().mid(4)] << fi.filePath();
}
}
// Then, look for GoPro split files.
foreach (QUrl url, urls) {
QFileInfo fi(removeFileScheme(url));
if (fi.baseName().size() == 8 && isValidGoProSuffix(fi) && isValidGoProPrefix(fi)
&& !isValidGoProFirstFilePrefix(fi)) {
QString goproNumber = fi.baseName().mid(4);
// Only if there is a matching main GoPro file.
if (goproFiles.contains(goproNumber) && goproFiles[goproNumber].size()) {
goproFiles[goproNumber] << fi.filePath();
}
}
}
// Next, sort the GoPro files.
auto keys = goproFiles.keys();
for (auto &goproNumber : keys) {
goproFiles[goproNumber].sort(Qt::CaseSensitive);
}
// Finally, build the list of all files.
// Add all the GoPro files first.
for (auto &paths : goproFiles) {
result << paths;
}
// Add all the non-GoPro files.
for (auto url : urls) {
QFileInfo fi(removeFileScheme(url));
if (fi.baseName().size() == 8 && isValidGoProSuffix(fi) &&
(isValidGoProFirstFilePrefix(fi) || isValidGoProPrefix(fi))) {
QString goproNumber = fi.baseName().mid(4);
if (goproFiles.contains(goproNumber) && goproFiles[goproNumber].contains(fi.filePath()))
continue;
}
result << fi.filePath();
}
return result;
}
int Util::coerceMultiple(int value, int multiple)
{
return (value + multiple - 1) / multiple * multiple;
}
QList<QUrl> Util::expandDirectories(const QList<QUrl> &urls)
{
QList<QUrl> result;
foreach (QUrl url, urls) {
QString path = Util::removeFileScheme(url);
QFileInfo fi(path);
if (fi.isDir()) {
QDir dir(path);
foreach (QFileInfo fi, dir.entryInfoList(QDir::Files | QDir::Readable, QDir::Name))
result << fi.filePath();
} else {
result << url;
}
}
return result;
}
bool Util::isDecimalPoint(QChar ch)
{
// See https://en.wikipedia.org/wiki/Decimal_separator#Unicode_characters
return ch == '.' || ch == ',' || ch == '\'' || ch == ' '
|| ch == QChar(0x00B7) || ch == QChar(0x2009) || ch == QChar(0x202F)
|| ch == QChar(0x02D9) || ch == QChar(0x066B) || ch == QChar(0x066C)
|| ch == QChar(0x2396);
}
bool Util::isNumeric(QString &str)
{
for (int i = 0; i < str.size(); ++i) {
QCharRef ch = str[i];
if (ch != '+' && ch != '-' && ch.toLower() != 'e'
&& !isDecimalPoint(ch) && !ch.isDigit())
return false;
}
return true;
}
bool Util::convertNumericString(QString &str, QChar decimalPoint)
{
// Returns true if the string was changed.
bool result = false;
if (isNumeric(str)) {
for (int i = 0; i < str.size(); ++i) {
QCharRef ch = str[i];
if (ch != decimalPoint && isDecimalPoint(ch)) {
ch = decimalPoint;
result = true;
}
}
}
return result;
}
bool Util::convertDecimalPoints(QString &str, QChar decimalPoint)
{
// Returns true if the string was changed.
bool result = false;
if (!str.contains(decimalPoint)) {
for (int i = 0; i < str.size(); ++i) {
QCharRef ch = str[i];
// Space is used as a delimiter for rect fields and possibly elsewhere.
if (ch != decimalPoint && ch != ' ' && isDecimalPoint(ch)) {
ch = decimalPoint;
result = true;
}
}
}
return result;
}
void Util::showFrameRateDialog(const QString &caption, int numerator, QDoubleSpinBox *spinner,
QWidget *parent)
{
double fps = numerator / 1001.0;
QMessageBox dialog(QMessageBox::Question, caption,
QObject::tr("The value you entered is very similar to the common,\n"
"more standard %1 = %2/1001.\n\n"
"Do you want to use %1 = %2/1001 instead?")
.arg(fps, 0, 'f', 6).arg(numerator),
QMessageBox::No | QMessageBox::Yes,
parent);
dialog.setDefaultButton(QMessageBox::Yes);
dialog.setEscapeButton(QMessageBox::No);
dialog.setWindowModality(QmlApplication::dialogModality());
if (dialog.exec() == QMessageBox::Yes) {
spinner->setValue(fps);
}
}
QTemporaryFile *Util::writableTemporaryFile(const QString &filePath, const QString &templateName)
{
// filePath should already be checked writable.
QFileInfo info(filePath);
QString templateFileName = templateName.isEmpty() ?
QString("%1.XXXXXX").arg(QCoreApplication::applicationName()) : templateName;
// First, try the system temp dir.
QString templateFilePath = QDir(QDir::tempPath()).filePath(templateFileName);
QScopedPointer<QTemporaryFile> tmp(new QTemporaryFile(templateFilePath));
if (!tmp->open() || tmp->write("") < 0) {
// Otherwise, use the directory provided.
return new QTemporaryFile(info.dir().filePath(templateFileName));
} else {
return tmp.take();
}
}
void Util::applyCustomProperties(Mlt::Producer &destination, Mlt::Producer &source, int in, int out)
{
Mlt::Properties p(destination);
p.clear("force_progressive");
p.clear("force_tff");
p.clear("force_aspect_ratio");
p.clear("video_delay");
p.clear("color_range");
p.clear("speed");
p.clear("warp_speed");
p.clear("warp_pitch");
p.clear("rotate");
p.clear(kAspectRatioNumerator);
p.clear(kAspectRatioDenominator);
p.clear(kCommentProperty);
p.clear(kShotcutProducerProperty);
p.clear(kDefaultAudioIndexProperty);
p.clear(kOriginalInProperty);
p.clear(kOriginalOutProperty);
if (!p.get_int(kIsProxyProperty))
p.clear(kOriginalResourceProperty);
destination.pass_list(source, "mlt_service, audio_index, video_index, force_progressive, force_tff,"
"force_aspect_ratio, video_delay, color_range, warp_speed, warp_pitch, rotate,"
kAspectRatioNumerator ","
kAspectRatioDenominator ","
kCommentProperty ","
kShotcutProducerProperty ","
kDefaultAudioIndexProperty ","
kOriginalInProperty ","
kOriginalOutProperty ","
kOriginalResourceProperty ","
kDisableProxyProperty);
if (!destination.get("_shotcut:resource")) {
destination.set("_shotcut:resource", destination.get("resource"));
destination.set("_shotcut:length", destination.get("length"));
}
QString resource = ProxyManager::resource(destination);
if (!qstrcmp("timewarp", source.get("mlt_service"))) {
auto speed = qAbs(source.get_double("warp_speed"));
auto caption = QString("%1 (%2x)").arg(Util::baseName(resource, true)).arg(speed);
destination.set(kShotcutCaptionProperty, caption.toUtf8().constData());
resource = destination.get("_shotcut:resource");
destination.set("warp_resource", resource.toUtf8().constData());
resource = QString("%1:%2:%3").arg("timewarp", source.get("warp_speed"), resource);
destination.set("resource", resource.toUtf8().constData());
double speedRatio = 1.0 / speed;
int length = qRound(destination.get_length() * speedRatio);
destination.set("length", destination.frames_to_time(length, mlt_time_clock));
} else {
auto caption = Util::baseName(resource, true);
destination.set(kShotcutCaptionProperty, caption.toUtf8().constData());
p.clear("warp_resource");
destination.set("resource", destination.get("_shotcut:resource"));
destination.set("length", destination.get("_shotcut:length"));
}
destination.set_in_and_out(in, out);
}
QString Util::getFileHash(const QString &path)
{
// This routine is intentionally copied from Kdenlive.
QFile file(removeQueryString(path));
if (file.open(QIODevice::ReadOnly)) {
QByteArray fileData;
// 1 MB = 1 second per 450 files (or faster)
// 10 MB = 9 seconds per 450 files (or faster)
if (file.size() > 1000000 * 2) {
fileData = file.read(1000000);
if (file.seek(file.size() - 1000000))
fileData.append(file.readAll());
} else {
fileData = file.readAll();
}
file.close();
return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
}
return QString();
}
QString Util::getHash(Mlt::Properties &properties)
{
QString hash = properties.get(kShotcutHashProperty);
if (hash.isEmpty()) {
QString service = properties.get("mlt_service");
QString resource = QString::fromUtf8(properties.get("resource"));
if (properties.get_int(kIsProxyProperty) && properties.get(kOriginalResourceProperty))
resource = QString::fromUtf8(properties.get(kOriginalResourceProperty));
else if (service == "timewarp")
resource = QString::fromUtf8(properties.get("warp_resource"));
else if (service == "vidstab")
resource = QString::fromUtf8(properties.get("filename"));
hash = getFileHash(resource);
if (!hash.isEmpty())
properties.set(kShotcutHashProperty, hash.toLatin1().constData());
}
return hash;
}
bool Util::hasDriveLetter(const QString &path)
{
auto driveSeparators = path.midRef(1, 2);
return driveSeparators == ":/" || driveSeparators == ":\\";
}
QFileDialog::Options Util::getFileDialogOptions()
{
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
if (qEnvironmentVariableIsSet("SNAP")) {
return QFileDialog::DontUseNativeDialog;
}
#endif
return QFileDialog::Options();
}
bool Util::isMemoryLow()
{
#if defined(Q_OS_WIN)
unsigned int availableKB = UINT_MAX;
MEMORYSTATUSEX memory_status;
ZeroMemory(&memory_status, sizeof(MEMORYSTATUSEX));
memory_status.dwLength = sizeof(MEMORYSTATUSEX);
if (GlobalMemoryStatusEx(&memory_status)) {
availableKB = memory_status.ullAvailPhys / 1024UL;
}
LOG_INFO() << "available RAM = " << availableKB << "KB";
return availableKB < kLowMemoryThresholdKB;
#elif defined(Q_OS_MAC)
QProcess p;
p.start("memory_pressure", QStringList());
p.waitForFinished();
auto lines = p.readAllStandardOutput();
p.close();
for (auto &line : lines.split('\n')) {
if (line.startsWith("System-wide memory free")) {
const auto fields = line.split(':');
for (auto s : fields) {
bool ok = false;
auto percentage = s.replace('%', "").toUInt(&ok);
if (ok) {
LOG_INFO() << percentage << '%';
return percentage <= kLowMemoryThresholdPercent;
}
}
}
}
return false;
#elif defined(__FreeBSD__)
QProcess p;
p.start("sysctl -n hw.usermem");
p.waitForFinished();
auto lines = p.readAllStandardOutput();
p.close();
bool ok = false;
auto availableKB = lines.toUInt(&ok);
if (ok) {
return availableKB < kLowMemoryThresholdKB;
}
return false;
#elif defined(Q_OS_LINUX)
unsigned int availableKB = UINT_MAX;
QFile meminfo("/proc/meminfo");
if (meminfo.open(QIODevice::ReadOnly)) {
for (auto line = meminfo.readLine(1024); availableKB == UINT_MAX
&& !line.isEmpty(); line = meminfo.readLine(1024)) {
if (line.startsWith("MemAvailable")) {
const auto &fields = line.split(' ');
for (const auto &s : fields) {
bool ok = false;
auto kB = s.toUInt(&ok);
if (ok) {
availableKB = kB;
break;
}
}
}
}
}
meminfo.close();
LOG_INFO() << "available RAM = " << availableKB << "KB";
return availableKB < kLowMemoryThresholdKB;
#endif
}
QString Util::removeQueryString(const QString &s)
{
auto i = s.lastIndexOf("\\?");
if (i < 0) {
i = s.lastIndexOf("%5C?");
}
if (i > 0 ) {
return s.left(i);
}
return s;
}
int Util::greatestCommonDivisor(int m, int n)
{
int gcd, remainder;
while (n) {
remainder = m % n;
m = n;
n = remainder;
}
gcd = m;
return gcd;
}
void Util::normalizeFrameRate(double fps, int &numerator, int &denominator)
{
// Convert some common non-integer frame rates to fractions.
if (qRound(fps * 1000000.0) == 23976024) {
numerator = 24000;
denominator = 1001;
} else if (qRound(fps * 100000.0) == 2997003) {
numerator = 30000;
denominator = 1001;
} else if (qRound(fps * 1000000.0) == 47952048) {
numerator = 48000;
denominator = 1001;
} else if (qRound(fps * 100000.0) == 5994006) {
numerator = 60000;
denominator = 1001;
} else {
// Workaround storing QDoubleSpinBox::value() loses precision.
numerator = qRound(fps * 1000000.0);
denominator = 1000000;
}
}
QString Util::textColor(const QColor &color)
{
return (color.value() < 150) ? "white" : "black";
}
void Util::cameraFrameRateSize(const QByteArray &deviceName, qreal &frameRate, QSize &size)
{
std::unique_ptr<QCamera> camera(new QCamera(deviceName));
if (camera) {
camera->load();
QCameraViewfinderSettings viewfinderSettings;
auto resolutions = camera->supportedViewfinderResolutions(viewfinderSettings);
if (resolutions.size() > 0) {
LOG_INFO() << "resolutions:" << resolutions;
// Get the highest resolution
viewfinderSettings.setResolution(resolutions.first());
for (auto &resolution : resolutions) {
if (resolution.width() > viewfinderSettings.resolution().width()
&& resolution.height() > viewfinderSettings.resolution().height()) {
viewfinderSettings.setResolution(resolution);
}
}
auto frameRates = camera->supportedViewfinderFrameRateRanges(viewfinderSettings);
if (frameRates.size() > 0) {
// Get the highest frame rate for the chosen resolution
viewfinderSettings.setMaximumFrameRate(frameRates.first().maximumFrameRate);
for (auto &frameRate : frameRates) {
LOG_INFO() << "frame rate:" << frameRate.maximumFrameRate;
if (frameRate.maximumFrameRate > viewfinderSettings.maximumFrameRate()) {
viewfinderSettings.setMaximumFrameRate(frameRate.maximumFrameRate);
}
}
}
}
camera->unload();
if (viewfinderSettings.maximumFrameRate() > 0) {
frameRate = viewfinderSettings.maximumFrameRate();
}
if (viewfinderSettings.resolution().width() > 0) {
size = viewfinderSettings.resolution();
}
}
}