-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscreenshot-mosaic.js
888 lines (888 loc) · 33.6 KB
/
screenshot-mosaic.js
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
"use strict";
/**
* This script will take a screenshot of a video and create a mosaic of that said video.
* It will take a screenshot of determined number of rows and columns, and then create
* a montage of those screenshots.
*
* This .js file is a compiled version of the .ts file, you can compile it yourself by
* running `npm run compile:mosaic`.
*
* Created by: noaione
* License: MIT
* Version: 2024.07.08.1
*/
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var scriptName = mp.get_script_name();
var uoscMenuName = "".concat(scriptName, "_uosc_menu");
var mosaicOptions = {
// Number of rows for screenshot
rows: 3,
// Number of columns for screenshot
columns: 4,
// Padding between screenshots (pixels)
padding: 10,
// Output format
/**
* @type {"png" | "jpg" | "webp"}
*/
format: "png",
// Screenshot mode
/**
* @type {"video" | "subtitles" | "window"}
*/
mode: "video",
// Append the "magick" command to the command line.
// Sometimes on windows, you cannot really use any magick command without prefixing
// "magick", if the command failed, you can set this to `yes` in your config.
append_magick: "no",
// Resize the final montage into the video height.
// I recommend keeping this enabled since if you have a 4k video, you don't want to
// have a montage that is basically 4k * whatever the number of screenshots you have.
// It would be way too big, so this will resize it back to the video height.
resize: "yes",
// The quality of the final montage image.
quality: 90,
// The path to the ImageMagick executable folders.
executable_path: "",
// The overriden family of the font to use.
// This is passed to magick convert -family option.
// If not set, it will use the default font.
font_family: "",
};
/**
* Replace all instances of a substring in a string, using simple while loop.
* Since there is no String.replaceAll in mpv scripting, or RegEx replace.
*
* @param text String to be replaced
* @param search Substring to be searched
* @param replace Replacement string
* @returns Replaced string
*/
function replMut(text, search, replace) {
while (text.indexOf(search) !== -1) {
text = text.replace(search, replace);
}
return text;
}
/**
* @class Pathing
*/
var Pathing = /** @class */ (function () {
function Pathing() {
this._isUnix = null;
this._isMac = null;
this._pathSep = null;
}
Pathing.prototype.getCwd = function () {
var cwd = mp.utils.getcwd();
if (cwd)
return cwd;
return "";
};
Pathing.prototype._detectOs = function () {
var cwdPath = this.getCwd();
this._isUnix = cwdPath.charAt(0) === '/';
this._isMac = false; // Mac is also Unix, but we'll detect separately.
this._pathSep = this._isUnix ? '/' : '\\';
if (this._isUnix) {
var unameResult = mp.command_native_async({ name: "subprocess", capture_stdout: true, playback_only: false, args: ["uname", "-s"] });
if (typeof unameResult.stdout === "string" && unameResult.stdout.match(/^\s*Darwin\s*$/)) {
this._isMac = true;
}
}
};
Pathing.prototype.isUnix = function () {
if (this._isUnix === null) {
this._detectOs();
}
return this._isUnix;
};
Pathing.prototype.isMac = function () {
if (this._isMac === null) {
this._detectOs();
}
return this._isMac;
};
Pathing.prototype.pathSep = function () {
if (this._pathSep === null) {
this._detectOs();
}
return this._pathSep;
};
Pathing.prototype.getParentPath = function (path) {
if (this._isUnix === null || this._pathSep === null) {
this._detectOs();
}
var pathParts = path.split(this._pathSep);
var previousDir = null;
if (pathParts.length > 1) {
previousDir = pathParts.pop();
}
var newPath = pathParts.join(this._pathSep);
if (this._isUnix && !newPath.length) {
newPath = "/";
}
if (!newPath.length) {
newPath = path;
}
return {
path: path,
newPath: newPath,
previousDir: previousDir,
};
};
Pathing.prototype.createDirectory = function (path) {
if (this.isUnix()) {
mp.msg.info("Creating directory (Unix): " + path);
mp.command_native({ name: "subprocess", playback_only: false, args: ["mkdir", "-p", path] });
}
else {
mp.msg.info("Creating directory (Windows): " + path);
mp.command_native({ name: "subprocess", playback_only: false, args: ["cmd", "/C", "mkdir", path] });
}
};
Pathing.prototype.deleteFile = function (path) {
mp.msg.info("Deleting file: " + path);
if (this.isUnix()) {
mp.command_native({ name: "subprocess", playback_only: false, args: ["rm", path] });
}
else {
mp.command_native({ name: "subprocess", playback_only: false, args: ["cmd", "/C", "del", "/F", "/Q", path] });
}
};
Pathing.prototype.joinPath = function (basePath, path) {
if (this._pathSep === null) {
this._detectOs();
}
// Check if ends with \\ or /
var lastIndex = basePath.length - 1;
if (basePath.indexOf('\\') === lastIndex || basePath.indexOf('/') === lastIndex) {
basePath = basePath.slice(0, -1);
}
return basePath + this._pathSep + path;
};
Pathing.prototype.fixPath = function (path) {
if (!this.isUnix()) {
if (path.indexOf("/") !== -1) {
path = path.replace(/\//g, "\\");
}
}
if (path.indexOf("Program Files (x86)") !== -1) {
path = path.replace("Program Files (x86)", "PROGRA~2");
}
if (path.indexOf("Program Files") !== -1) {
path = path.replace("Program Files", "PROGRA~1");
}
return path;
};
return Pathing;
}());
mp.options.read_options(mosaicOptions, "screenshot-mosaic");
/**
* Test if a directory is valid.
* @param {string} path - The path to test.
* @returns {boolean} - Whether the path is valid.
*/
function testDirectory(path) {
var paths = new Pathing();
var target = mp.utils.join_path(path, "_mosaic_screenshot_test.bin");
try {
mp.utils.write_file("file://".concat(target), "THIS FILE IS CREATED BY MPV SCREENSHOT MOSAIC SCRIPT");
}
catch (e) {
mp.msg.error("Could not write to directory: " + path);
return false;
}
// delete
paths.deleteFile(paths.fixPath(target));
return true;
}
/**
* Get the output directory.
* @returns {string} - The output directory.
*/
function getOutputDir() {
var paths = new Pathing();
// Use screenshot directory
var screenDir = mp.get_property("screenshot-directory");
if (screenDir) {
var expandScreenDir = mp.command_native(["expand-path", screenDir]);
var lastError = mp.last_error();
if (!lastError && testDirectory(expandScreenDir)) {
mp.msg.info("Using screenshot directory: " + expandScreenDir);
return paths.fixPath(expandScreenDir);
}
}
// Use mpv home directory as fallback
var homeDir = mp.command_native(["expand-path", "~~home/"]);
mp.msg.error("Could not get screenshot directory, trying to use mpv home directory: ".concat(homeDir));
return paths.fixPath(homeDir);
}
/**
* Create magick prefix command.
* @param {string} [cmd] - The command to execute.
* @returns {string[]} - The prefixed command.
*/
function magick(cmd) {
var basePathing = [];
if (mosaicOptions.executable_path && mosaicOptions.executable_path.length > 0) {
var pathing = new Pathing();
if (mosaicOptions.append_magick.toLowerCase() === "yes") {
basePathing.push(pathing.fixPath(mp.utils.join_path(mosaicOptions.executable_path, "magick")));
cmd && basePathing.push(cmd);
}
else {
basePathing.push(pathing.fixPath(mp.utils.join_path(mosaicOptions.executable_path, cmd || "convert")));
}
}
else {
if (mosaicOptions.append_magick.toLowerCase() === "yes") {
basePathing.push("magick");
}
cmd && basePathing.push(cmd);
}
return basePathing;
}
/**
* Explicitly check if the execution of magick command was successful.
* Since sometimes a command would just return 0 even if it failed.
* @param {SubprocessResult} result - The result of the subprocess.
* @returns {boolean} - Whether the subprocess was successful.
*/
function isMagickSuccess(result) {
// mpv subprocess actually return success even if magick fails.
if (result.status !== 0) {
return [false, result.stderr || ""];
}
var stdout = result.stdout || "";
var stderr = result.stderr || "";
var errorMsg = (stdout || stderr).replace(/\r?\n/g, "\n");
return [stdout.indexOf("error/") === -1 && stderr.indexOf("error/") === -1, errorMsg];
}
/**
* Humanize bytes number to a human readable string.
* @param {number | undefined} bytes - The number of bytes.
* @returns {string} - The humanized bytes. e.g. "1.2 MiB" or "?? B" if bytes is undefined.
*/
function humanizeBytes(bytes) {
if (bytes === undefined)
return "?? B";
var thresh = 1024;
if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}
var units = ['kiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
var u = -1;
do {
bytes /= thresh;
++u;
} while (Math.abs(bytes) >= thresh && u < units.length - 1);
return bytes.toFixed(1) + ' ' + units[u];
}
/**
* Floor a number.
* Since there is no Math.floor in mpv scripting, use ~~ instead.
* @param {number} n - The number to floor.
* @returns {number} - The floored number.
*/
function floor(n) {
// no Math module
return ~~n;
}
/**
* Pad a number with leading zeros.
* @param {number} n - The number to pad.
* @returns {string}
*/
function padZero(n) {
return n < 10 ? "0" + n : "" + n;
}
/**
* Format a duration number into HH:MM:SS.
* @param {number | undefined} seconds - The seconds to format.
* @returns {string} - The formatted duration.
*/
function formatDurationToHHMMSS(seconds) {
// do not use Date
if (seconds === undefined)
return "??:??:??";
var hours = floor(seconds / 3600);
var minutes = floor((seconds - (hours * 3600)) / 60);
var seconds2 = floor(seconds - (hours * 3600) - (minutes * 60));
// pad numbers with leading zeros
var hoursString = padZero(hours);
var minutesString = padZero(minutes);
var secondsString = padZero(seconds2);
return hoursString + ":" + minutesString + ":" + secondsString;
}
/**
* Format an output filename with some constraints and extra info.
* @param {string} fileName - The original filename
* @param {MinimalMosaicOptions} options - The options to use
* @returns {string} - The new filename
*/
function createOutputName(fileName, options) {
var finalName = fileName.replace(" ", "_");
var ColRow = "".concat(options.columns, "x").concat(options.rows);
var mosaicName = ".mosaic".concat(ColRow);
// Max count is 256 characters, with safety margin to 224
var testCount = finalName.length + mosaicName.length;
if (testCount > 224) {
// Butcher only the finalName that doesn't include the mosaicName yet
var cutCount = testCount - 224;
finalName = finalName.slice(0, -cutCount);
}
return finalName + mosaicName;
}
/**
* Run the resize command, will be skipped if resize is disabled.
* @param {string} imgOutput - The image output path.
* @param {number} videoHeight - The video height.
* @param {MosaicOptions} options - The options to use.
* @param {CallbackChain} callback - The callback chain that will be called.
* @returns {void} - Nothing
*/
function runResize(imgOutput, videoHeight, options, callback) {
var resizeCmdsBase = [];
if (options.append_magick.toLowerCase() === "yes") {
resizeCmdsBase.push("magick");
}
var resizeCmds = __spreadArray(__spreadArray([], magick("convert"), true), [
"".concat(imgOutput, ".montage.png"),
"-resize",
"x".concat(videoHeight),
"".concat(imgOutput, ".montage.png"),
], false);
if (options.resize.toLowerCase() !== "yes") {
callback(true, undefined);
return;
}
mp.msg.info("Resizing image to x".concat(videoHeight, ": ").concat(imgOutput, ".montage.png"));
dump(resizeCmds);
mp.command_native_async({
name: "subprocess",
playback_only: false,
args: resizeCmds,
capture_stderr: true,
capture_stdout: true,
}, function (_, result, __) {
var _a = isMagickSuccess(result), success = _a[0], errorMsg = _a[1];
mp.msg.info("Resize status: ".concat(success, " || err? ").concat(errorMsg));
callback(success, errorMsg);
});
}
/**
* Run the annotation command.
* @param {string} fileName - The video filename.
* @param {number} videoWidth - The video width.
* @param {number} videoHeight - The video height.
* @param {string} duration - The video duration (pre-formatted).
* @param {string} imgOutput - The image output path.
* @param {MosaicOptions} options - The options to use.
* @param {CallbackChain} callback - The callback chain that will be called.
* @returns {void} - Nothing
*/
function runAnnotation(fileName, videoWidth, videoHeight, duration, imgOutput, options, callback) {
// check font family
var fontFamilies = [];
if (options.font_family && options.font_family.trim().length > 0) {
fontFamilies.push("-family", options.font_family.trim());
}
// annotate text
var annotateCmds = __spreadArray(__spreadArray(__spreadArray(__spreadArray([], magick("convert"), true), [
"-background",
"white",
"-pointsize",
"40"
], false), fontFamilies, true), [
"-gravity",
"northwest",
"label:mpv Media Player",
// Add top margin
"-splice",
"0x10",
"-pointsize",
"16",
"-gravity",
"northwest",
"label:File Name: " + fileName + "",
"label:File Size: " + humanizeBytes(mp.get_property_number("file-size")) + "",
"label:Resolution: " + videoWidth + "x" + videoHeight + "",
"label:Duration: " + duration + "",
// Add left margin
"-splice",
"10x0",
"".concat(imgOutput, ".montage.png"),
"-append",
"-quality",
"".concat(options.quality, "%"),
imgOutput,
], false);
mp.msg.info("Annotating image: ".concat(imgOutput));
dump(annotateCmds);
mp.command_native_async({
name: "subprocess",
playback_only: false,
args: annotateCmds,
capture_stderr: true,
capture_stdout: true,
}, function (_, result, __) {
var _a = isMagickSuccess(result), success = _a[0], errorMsg = _a[1];
mp.msg.info("Annotate status: ".concat(success, " || err? ").concat(errorMsg));
callback(success, errorMsg);
});
}
/**
* Run the montagge, resize, and annotate command.
* @param {string[]} screenshots - The list of screenshots to montage.
* @param {number} videoWidth - The video width.
* @param {number} videoHeight - The video height.
* @param {string} fileName - The video filename.
* @param {string} duration - The video duration (pre-formatted).
* @param {string} outputFile - The image output path.
* @param {MosaicOptions} options - The options to use.
* @param {CallbackChain} callback - The callback chain that will be called.
* @returns {void} - Nothing
*/
function createMosaic(screenshots, videoWidth, videoHeight, fileName, duration, outputFile, options, callback) {
var imageMagickArgs = __spreadArray(__spreadArray([], magick("montage"), true), [
"-geometry",
"".concat(videoWidth, "x").concat(videoHeight, "+").concat(options.padding, "+").concat(options.padding),
], false);
for (var i = 0; i < screenshots.length; i++) {
imageMagickArgs.push(screenshots[i]);
}
imageMagickArgs.push("".concat(outputFile, ".montage.png"));
mp.msg.info("Creating image montage: ".concat(outputFile, ".montage.png"));
dump(imageMagickArgs);
mp.command_native_async({
name: "subprocess",
playback_only: false,
args: imageMagickArgs,
capture_stderr: true,
capture_stdout: true,
}, function (_, result, __) {
var _a = isMagickSuccess(result), success = _a[0], errorMsg = _a[1];
mp.msg.info("Montage status: ".concat(success, " || ").concat(errorMsg));
if (success) {
runResize(outputFile, videoHeight, options, function (result2, error2) {
if (!result2) {
callback(false, error2);
}
else {
runAnnotation(fileName, videoWidth, videoHeight, duration, outputFile, options, function (result3, error3) {
callback(result3, error3);
});
}
});
}
else {
callback(false, errorMsg);
}
});
}
/**
* Create a collection of screenshots from the video.
* @param {number} startTime - The start time of the video. (in seconds, relative to video duration)
* @param {number} timeStep - The time step in-between each screenshot. (in seconds)
* @param {string} screenshotDir - The temporary folder to be used to save the screenshot.
* @param {MosaicOptions} options - The options to use.
* @param {CallbackChainScreenshot} callback - The callback chain that will be called.
*/
function screenshotCycles(startTime, timeStep, screenshotDir, options, callback) {
var rows = options.rows, columns = options.columns;
var screenshots = [];
var totalImages = rows * columns;
// callback hell...
function callbackScreenshot(counter, screenshots) {
mp.command_native_async(["seek", (startTime + (timeStep * (counter - 1))), "absolute", "exact"], function (success, _, error) {
if (!success) {
callback(false, error, []);
return;
}
var imagePath = mp.utils.join_path(screenshotDir, "temp_screenshot-".concat(counter, ".png"));
mp.command_native_async(["screenshot-to-file", imagePath, options.mode], function (success, _, error) {
if (!success) {
callback(false, error, screenshots);
return;
}
// if counter is equal to totalImages, we are done
if (counter >= totalImages) {
callback(true, undefined, __spreadArray(__spreadArray([], screenshots, true), [imagePath], false));
return;
}
// if not, loop again.
callbackScreenshot(counter + 1, __spreadArray(__spreadArray([], screenshots, true), [imagePath], false));
});
});
}
callbackScreenshot(1, screenshots);
}
/**
* Check if the montage command is available. (also check Magick)
* @returns {boolean} - True if the montage command is available, false otherwise.
*/
function checkMagick() {
var montageCmd = __spreadArray(__spreadArray([], magick("montage"), true), [
"--version",
], false);
mp.msg.info("Checking ImageMagick availability...", JSON.stringify(montageCmd));
var res = mp.command_native({ name: "subprocess", playback_only: false, args: montageCmd });
return res.status === 0;
}
/**
* Check if the variables of an options are all valid.
* @param {MosaicOptions} options - The options to check.
* @returns {boolean} - True if all the options are valid, false otherwise.
*/
function verifyVariables(options) {
if (options.rows < 1) {
mp.osd_message("Mosaic rows must be greater than 0");
return false;
}
if (options.columns < 1) {
mp.osd_message("Mosaic columns must be greater than 0");
return false;
}
if (options.padding < 0) {
mp.osd_message("Mosaic padding must be greater than or equal to 0");
return false;
}
var mosaicMode = options.mode.toLowerCase();
if (mosaicMode !== "video" && mosaicMode !== "subtitles" && mosaicMode !== "window") {
mp.osd_message("Mosaic mode must be either 'video' or 'subtitles' or 'window'");
return false;
}
return true;
}
/**
* Send a formatted OSD message that support ASS tags.
* @param {string} message - The message to send
* @param {number} duration - The duration (in seconds, default to `2`)
*/
function sendOSD(message, duration) {
if (duration === void 0) { duration = 2; }
var prefix = mp.get_property("osd-ass-cc/0");
var postfix = mp.get_property("osd-ass-cc/1");
if (prefix && postfix) {
mp.osd_message("".concat(prefix).concat(message).concat(postfix), duration);
}
else {
mp.osd_message(message, duration);
}
}
/**
* The main execution function, which includes all the check and everything.
* This should be called immediatly after a macro is executed.
*
* @param {MosaicOptions} options - Options
* @returns {void} - Nothing
*/
function entrypoint(options) {
// create a mosaic of screenshots
var paths = new Pathing();
if (!verifyVariables(options)) {
return;
}
var magickExist = checkMagick();
if (!magickExist) {
var tf = paths.isUnix() ? "false" : "true";
mp.msg.info("ImageMagick cannot be found, please install it.\nOr you can set append_magick to ".concat(tf, " in the script options."));
mp.osd_message("ImageMagick cannot be found, please install it.\nOr you can set append_magick to ".concat(tf, " in the script options."), 5);
return;
}
var imageCount = options.rows * options.columns;
// get video length and divide by number of screenshots
var videoLength = mp.get_property_number("duration");
if (videoLength === undefined) {
mp.osd_message("Failed to get video length");
return;
}
// get video width
var videoWidth = mp.get_property_number("width");
if (videoWidth === undefined) {
mp.osd_message("Failed to get video width");
return;
}
// get video height
var videoHeight = mp.get_property_number("height");
if (videoHeight === undefined) {
mp.osd_message("Failed to get video height");
return;
}
// original time position
var originalTimePos = mp.get_property_number("time-pos");
if (originalTimePos === undefined) {
mp.osd_message("Failed to get time position");
return;
}
mp.msg.info(mp.module_paths);
mp.msg.info("Running Mosaic Tools with the following options:");
mp.msg.info(" Rows: " + options.rows);
mp.msg.info(" Columns: " + options.columns);
mp.msg.info(" Padding: " + options.padding);
mp.msg.info(" Format: " + options.format);
mp.msg.info(" Video Length: " + videoLength);
mp.msg.info(" Video Width: " + videoWidth);
mp.msg.info(" Video Height: " + videoHeight);
var outputDir = getOutputDir();
mp.msg.info(" Output Directory: " + outputDir);
var videoDuration = formatDurationToHHMMSS(videoLength);
// we want to start at 10% of the video length and end at 90%
var startTime = videoLength * 0.1;
var endTime = videoLength * 0.9;
var timeStep = (endTime - startTime) / (imageCount - 1);
mp.osd_message("Creating ".concat(options.columns, "x").concat(options.rows, " mosaic of ").concat(imageCount, " screenshots..."), 2);
mp.msg.info("Creating ".concat(options.columns, "x").concat(options.rows, " mosaic of ").concat(imageCount, " screenshots..."));
// pause video
var homeDir = mp.command_native(["expand-path", "~~home/"]);
var screenshotDir = paths.fixPath(mp.utils.join_path(homeDir, "screenshot-mosaic"));
paths.createDirectory(screenshotDir);
mp.set_property("pause", "yes");
// Take screenshot and put it in callback to createMosaic
screenshotCycles(startTime, timeStep, screenshotDir, options, function (success, error, screenshots) {
mp.set_property_number("time-pos", originalTimePos);
mp.set_property("pause", "no");
if (!success) {
mp.msg.error("Failed to create screenshots...");
mp.msg.error(error);
mp.osd_message("Failed to create screenshots...", 5);
return;
}
if (screenshots.length > 0) {
mp.msg.info("Creating mosaic for ".concat(options.columns, "x").concat(options.rows, " images..."));
mp.osd_message("Creating mosaic...", 2);
var fileName = mp.get_property("filename");
var imgOutput_1 = paths.fixPath(mp.utils.join_path(outputDir, "".concat(createOutputName(fileName, options), ".").concat(options.format)));
createMosaic(screenshots, videoWidth, videoHeight, fileName, videoDuration, imgOutput_1, options, function (success, error) {
if (success) {
mp.msg.info("Mosaic created for ".concat(options.columns, "x").concat(options.rows, " images at ").concat(imgOutput_1, "..."));
sendOSD("Mosaic created!\n{\\b1}".concat(imgOutput_1, "{\\b0}"), 5);
}
else {
mp.msg.error("Failed to create mosaic for ".concat(options.columns, "x").concat(options.rows, " images..."));
mp.msg.error(error);
mp.osd_message("Failed to create mosaic for ".concat(options.columns, "x").concat(options.rows, " images..."), 5);
}
// Cleanup
mp.msg.info("Cleaning up...");
screenshots.forEach(function (sspath) {
paths.deleteFile(paths.fixPath(sspath));
});
paths.deleteFile(paths.fixPath("".concat(imgOutput_1, ".montage.png")));
});
}
});
}
mp.add_key_binding("Ctrl+Alt+s", "screenshot", function () {
entrypoint(mosaicOptions);
});
/** UOSC Related Code */
var UOSCState = {
rows: 3,
columns: 4,
padding: 10,
format: "png",
mode: "video",
resize: "yes",
quality: 90,
};
function resetStateWithConfig() {
for (var key in UOSCState) {
// @ts-expect-error
UOSCState[key] = mosaicOptions[key];
}
}
function createUOSCMenu() {
return {
type: uoscMenuName,
title: "Screenshot Mosaic",
keep_open: true,
items: [
{
title: "Rows",
hint: UOSCState.rows.toString(),
items: [
{
title: "+1",
icon: "keyboard_arrow_up",
value: uoscUpdateDispatch("rows", UOSCState.rows + 1),
},
{
title: "-1",
icon: "keyboard_arrow_down",
value: uoscUpdateDispatch("rows", UOSCState.rows - 1 < 1 ? 1 : UOSCState.rows - 1),
},
]
},
{
title: "Columns",
hint: UOSCState.columns.toString(),
items: [
{
title: "+1",
icon: "keyboard_arrow_up",
value: uoscUpdateDispatch("columns", UOSCState.columns + 1),
},
{
title: "-1",
icon: "keyboard_arrow_down",
value: uoscUpdateDispatch("columns", UOSCState.columns - 1 < 1 ? 1 : UOSCState.columns - 1),
},
]
},
{
title: "Padding",
hint: UOSCState.padding.toString(),
items: [
{
title: "+5",
icon: "keyboard_double_arrow_up",
value: uoscUpdateDispatch("padding", UOSCState.padding + 5),
},
{
title: "+1",
icon: "keyboard_arrow_up",
value: uoscUpdateDispatch("padding", UOSCState.padding + 1),
},
{
title: "-1",
icon: "keyboard_arrow_down",
value: uoscUpdateDispatch("padding", UOSCState.padding - 1 < 1 ? 1 : UOSCState.padding - 1),
},
{
title: "-5",
icon: "keyboard_double_arrow_down",
value: uoscUpdateDispatch("padding", UOSCState.padding - 5 < 1 ? 1 : UOSCState.padding - 5),
},
]
},
{
title: "Format",
hint: UOSCState.format,
icon: "image",
items: [
{
title: "PNG",
icon: UOSCState.format === "png" ? "radio_button_checked" : "radio_button_unchecked",
value: uoscUpdateDispatch("format", "png"),
},
{
title: "JPEG",
icon: UOSCState.format === "jpg" ? "radio_button_checked" : "radio_button_unchecked",
value: uoscUpdateDispatch("format", "jpg"),
},
{
title: "WebP",
icon: UOSCState.format === "webp" ? "radio_button_checked" : "radio_button_unchecked",
value: uoscUpdateDispatch("format", "webp"),
},
]
},
{
title: "Screenshot Mode",
hint: UOSCState.mode,
icon: "burst_mode",
items: [
{
title: "Video only",
icon: UOSCState.mode === "video" ? "radio_button_checked" : "radio_button_unchecked",
value: uoscUpdateDispatch("mode", "video"),
},
{
title: "Video + Subtitles",
icon: UOSCState.mode === "subtitles" ? "radio_button_checked" : "radio_button_unchecked",
value: uoscUpdateDispatch("mode", "subtitles"),
},
{
title: "Whole window",
icon: UOSCState.mode === "window" ? "radio_button_checked" : "radio_button_unchecked",
value: uoscUpdateDispatch("mode", "window"),
},
]
},
{
title: "Resize",
icon: UOSCState.resize === "yes" ? "check_box" : "check_box_outline_blank",
value: uoscUpdateDispatch("resize", UOSCState.resize === "yes" ? "no" : "yes"),
},
{
title: "Reset",
icon: "restart_alt",
value: "script-message-to ".concat(scriptName, " uosc-menu-reset"),
},
{
title: "Create Mosaic Screenshot",
icon: "screenshot_monitor",
value: "script-message-to ".concat(scriptName, " uosc-menu-execute"),
keep_open: false,
}
]
};
}
function uoscUpdateDispatch(key, value) {
var jsonData = JSON.stringify({
key: key,
value: String(value),
format: typeof value,
});
return "script-message-to ".concat(scriptName, " uosc-menu-update ").concat(jsonData);
}
function uoscUpdateConsume(rawData) {
if (typeof rawData !== "string")
return;
var _a = JSON.parse(rawData), key = _a.key, value = _a.value, format = _a.format;
if (key in UOSCState) {
if (format === "number") {
var parsed = parseInt(value);
if (!isNaN(parsed)) {
// @ts-expect-error
UOSCState[key] = parsed;
}
}
else if (format === "string") {
// @ts-expect-error
UOSCState[key] = value;
}
else if (format === "boolean") {
// @ts-expect-error
UOSCState[key] = value === "true" ? "yes" : "no";
}
}
var menu = createUOSCMenu();
mp.commandv("script-message-to", "uosc", "update-menu", JSON.stringify(menu));
}
mp.add_key_binding(null, "uosc-menu", function () {
var menu = createUOSCMenu();
mp.commandv("script-message-to", "uosc", "open-menu", JSON.stringify(menu));
});
mp.register_script_message("uosc-menu-update", uoscUpdateConsume);
mp.register_script_message("uosc-menu-reset", function () {
resetStateWithConfig();
var menu = createUOSCMenu();
mp.commandv("script-message-to", "uosc", "update-menu", JSON.stringify(menu));
});
mp.register_script_message("uosc-menu-execute", function () {
var mergedConfig = __assign(__assign({}, mosaicOptions), UOSCState);
resetStateWithConfig();
entrypoint(mergedConfig);
});