-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrenderer.js
executable file
·1190 lines (1110 loc) · 41.2 KB
/
renderer.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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const {
desktopCapturer,
screen
} = require('electron');
var appRootDir = require('app-root-dir').get();
var remote = require('electron').remote;
var pid = remote.process.pid;
var dialog = remote.dialog;
var $ = require('jQuery');
var path = require('path');
require('shelljs/global');
const osTmpdir = require('os-tmpdir');
var ostemp = osTmpdir();
const Store = require('electron-store');
const store = new Store();
var kill = require('tree-kill');
const {
shell
} = require('electron');
// var ffmpegsrc = appRootDir + '/ffmpeg/';
var os = require("os");
if (os.platform() == "darwin") {
var ismac = 1;
} else {
var ismac = 0;
}
if (ismac) {
var ffmpegpath = appRootDir + '/bin/osx/ffmpeg';
var ffprobepath = appRootDir + '/bin/osx/ffprobe';
var appswitchpath = appRootDir + '/bin/osx/appswitch';
var workdir = ostemp + '/' + maketemp();
var trf = workdir + '/transforms.trf';
} else {
var winoriginal;
var ffmpegpath = appRootDir + '\\bin\\win\\ffmpeg.exe';
var ffprobepath = appRootDir + '\\bin\\win\\ffprobe.exe';
var sendkeysbatpath = appRootDir + '\\bin\\win\\sendKeys.bat';
var temporiginal = workdir + '\\temp.mp4';
var workdir = ostemp + '\\' + maketemp();
var unixify = require('unixify');
var trf = unixify(workdir + '\\transforms.trf');
}
console.log(ffmpegpath);
var filelist = [];
var widtharr = [];
var heightarr = [];
var croppixelarr = [];
var aspect;
remote.getGlobal('workdirObj').prop1 = workdir;
console.log('tempdir: ' + remote.getGlobal('workdirObj').prop1);
var giffile;
var tempcrop = workdir + '/crop.mp4';
var previewfile = workdir + '/preview.png';
var previewindex = 0;
var lastperc = 0;
var lastpercUL = 0;
var fs = require('fs');
//var path = require('path');
var croppedfilelist = [];
var title, folder, finallink;
var ispreviewclip = 1;
var maxsize = 15;
var gifsize, size, selectedgifmb;
var megapixels, selectedwidth, selectedheight, calcgifheight, calcgifwidth, giflength, videowidth, videoheight, id;
var inprogress = false;
var gifrender = false;
var helpviewing = false;
var newhelp, adjustVidHeight;
var helppending = false;
window.croppixelperc = 0.09;
const spawn = require('child_process').spawn;
const spawnsync = require('child_process').spawnSync;
//const spawnsync = require('child_process').spawnSync;
function maketemp() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 10; i++) text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function run_cmd(cmd, args, callBack) {
var spawn = require('child_process').spawn;
var child = spawn(cmd, args);
var resp = "";
child.stdout.on('data', function(buffer) {
resp += buffer.toString()
});
child.stdout.on('end', function() {
callBack(resp)
});
} // ()
var isdicom = 0;
function isclip(filename, filewithpath) {
var clipext = ['mp4', 'm4v', 'avi', 'wmv', 'mov', 'flv', 'mpg', 'mpeg', 'gif'];
for (var i = 0; i < clipext.length; i++) {
if (filename.toLowerCase().split('.').pop().indexOf(clipext[i]) >= 0) {
return (1);
}
}
var filetype = spawnsync('file', ['-Ib', filewithpath, ]); // '| grep -i dicom']);
var filetyperesult = filetype.stdout.toString().toLowerCase();
var dicomsearch = filetyperesult.indexOf('dicom');
if (dicomsearch > -1) {
isdicom = 1;
return (1);
}
return (0);
}
function isstill(filename) {
var stillext = ['jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif'];
for (var i = 0; i < stillext.length; i++) {
if (filename.toLowerCase().split('.').pop().indexOf(stillext[i]) >= 0) {
return (1);
}
}
return (0);
}
function search(startPath) {
var list = [];
if (!fs.existsSync(startPath)) {
return;
}
var files = fs.readdirSync(startPath);
for (var i = 0; i < files.length; i++) {
var filename = path.join(startPath, files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
var list_temp = [];
list_temp = search(filename); //recurse
for (var m = 0; m < list_temp.length; m++) {
list.push(list_temp[m]);
}
} else if (isclip(filename, files[i].path) > 0) {
list.push(filename);
}
}
return (list);
}
//allow drop on dahsed area
$("#filelistwrap").on('dragenter', function(event) {
event.stopPropagation();
event.preventDefault();
});
$("#filelistwrap").on('dragover', function(event) {
event.stopPropagation();
event.preventDefault();
});
function focusThisApp() {
if (ismac) {
spawn(appswitchpath, ['-p', pid]);
} else {
spawn('cmd.exe', ['/c', 'call', '"' + sendkeysbatpath + '"', '"vidSmooth"', '""'], {
windowsVerbatimArguments: true
});
}
}
function focusFinder() {
if (ismac) {
spawn(appswitchpath, ['-a', 'Finder']);
} else {
spawn('cmd.exe', ['/c', 'call', '"' + sendkeysbatpath + '"', '"vidSmooth"', '""'], {
windowsVerbatimArguments: true
});
}
}
$("#filelistwrap").on('drop', function(event) {
event.preventDefault();
filelist = [];
var files = event.originalEvent.dataTransfer.files;
focusThisApp();
for (var i = 0; i < files.length; i++) {
var name = files[i].name;
var pathn = files[i].path;
if (fs.lstatSync(pathn).isDirectory()) {
var temp_list = [];
temp_list = search(pathn);
for (var k = 0; k < temp_list.length; k++) {
if (filelist.indexOf(temp_list[k]) == -1) {
filelist.push(temp_list[k]);
index = filelist.length;
//$('#filelist').append(index + ': ' + temp_list[k] + '<br />');
}
}
} else if (isclip(name, files[i].path) > 0) {
if (filelist.indexOf(pathn) == -1) {
filelist.push(pathn);
index = filelist.length;
//$('#filelist').append(index + ': ' + path + '<br />');
}
}
}
if (filelist.length == '0') {
$('#drag').html('ugh, no clip found, try again');
} else {
$('#sidebar').show();
$('#drag').css('visibility', 'hidden');
$("#filelistwrap").hide();
$('#maintitle').hide();
//$('#loading-container').show();
//preview();
showoptions();
}
});
$("#openmodalwrap").on('dragenter', function(event) {
event.stopPropagation();
event.preventDefault();
});
$("#openmodalwrap").on('dragover', function(event) {
event.stopPropagation();
event.preventDefault();
});
$("#openmodalwrap").on('drop', function(event) {
event.preventDefault();
filelist = [];
var files = event.originalEvent.dataTransfer.files;
focusThisApp();
for (var i = 0; i < files.length; i++) {
var name = files[i].name;
var pathn = files[i].path;
if (fs.lstatSync(pathn).isDirectory()) {
var temp_list = [];
temp_list = search(pathn);
for (var k = 0; k < temp_list.length; k++) {
if (filelist.indexOf(temp_list[k]) == -1) {
filelist.push(temp_list[k]);
index = filelist.length;
//$('#filelist').append(index + ': ' + temp_list[k] + '<br />');
}
}
} else if (isclip(name, files[i].path) > 0) {
if (filelist.indexOf(pathn) == -1) {
filelist.push(pathn);
index = filelist.length;
//$('#filelist').append(index + ': ' + path + '<br />');
}
}
}
if (filelist.length == '0') {
$('#drag').html('ugh, no clip found, try again');
} else {
$('#sidebar').show();
$('#drag').css('visibility', 'hidden');
$("#filelistwrap").hide();
$('#maintitle').hide();
//$('#loading-container').show();
//preview();
showoptions();
}
});
//prevent ‘drop’ event on document.
$(document).on('dragenter', function(e) {
e.stopPropagation();
e.preventDefault();
});
$(document).on('dragover', function(e) {
e.stopPropagation();
e.preventDefault();
});
$(document).on('drop', function(e) {
e.stopPropagation();
e.preventDefault();
});
function queue(tasks) {
let index = 0;
const runTask = (arg) => {
if (index >= tasks.length) {
return Promise.resolve(arg);
}
return new Promise((resolve, reject) => {
tasks[index++](arg).then(arg => resolve(runTask(arg))).catch(reject);
});
}
return runTask();
}
function timeString2ms(a, b, c) { // time(HH:MM:SS.mss)
return c = 0,
a = a.split('.'), !a[1] || (c += a[1] * 1),
a = a[0].split(':'), b = a.length,
c += (b == 3 ? a[0] * 3600 + a[1] * 60 + a[2] * 1 : b == 2 ? a[0] * 60 + a[1] * 1 : s = a[0] * 1) * 1e3,
c
}
var child;
function customSpawn(command, args) {
return () => new Promise((resolve, reject) => {
console.log(command + ' ' + args.join(' '));
if (!ismac) {
var newargs = args.slice(0);
newargs.unshift('"' + command + '"');
newargs.unshift('/c');
console.log(newargs);
child = spawn('cmd.exe', newargs, {
windowsVerbatimArguments: true
});
} else {
child = spawn(command, args, {
windowsVerbatimArguments: true
});
}
var length = null;
var currenttime = 0;
var regex = /Duration:(.*), start:/;
var regex2 = /time=(.*) bitrate/;
child.stderr.on('data', (data) => {
//console.log(command + args + `stderr: ${data}`);
var buff = new Buffer(data);
var str = buff.toString('utf8')
//console.log(str);
if (fullviding || previewing) {
var Duration_matches = str.match(regex);
var Current_matches = str.match(regex2);
if (Duration_matches) {
length = timeString2ms(Duration_matches[1]);
}
if (Current_matches) {
currenttime = timeString2ms(Current_matches[1]);
}
for (i = 0; i < args.length; i++) {
//console.log(args[i]);
if (args[i].indexOf('vidstabdetect') > -1) {
var pass = 1;
}
}
if (fullviding) {
// window.t1 = performance.now();
// //var left=(window.t1-window.t0)*stillcount/(i*1000);
// var duration = (window.t1-window.t0)/1000;
// var left=Math.round(duration*stillcount/i-duration);
// if(left>59){
// var secs=left % 60;
// var min=Math.floor(left/60);
// var lefttext=min+' min '+secs+' sec left';
// } else {
// var lefttext=left+' secs left';
// }
if (pass == 1) {
var offset = 0;
var mult = 0.5;
} else {
var offset = 0.5;
var mult = 0.5;
}
var percdone = offset + mult * currenttime / length;
if (!isNaN(percdone)) {
if(percdone>1){
percdone = 1;
}
var perctext = '[' + (percdone * 100).toFixed(2) + '% done]';
$('#message').html('smoothing your video<br>' + perctext);
}
} else {
var offset = 0;
var mult = 1;
}
if (length) {
var percdone = offset + mult * currenttime / length;
var myqueue = [];
myqueue.push(progress(percdone));
queue(myqueue).then(([cmd, args]) => {}).catch(TypeError, function(e) {}).catch(err => console.log(err));
}
}
});
child.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
child.on('close', code => {
if (code === 0) {
resolve();
} else {
reject();
}
});
});
}
function roundNumber(num, scale) {
if (!("" + num).includes("e")) {
return +(Math.round(num + "e+" + scale) + "e-" + scale);
} else {
var arr = ("" + num).split("e");
var sig = ""
if (+arr[1] + scale > 0) {
sig = "+";
}
return +(Math.round(+arr[0] + "e" + sig + (+arr[1] + scale)) + "e-" + scale);
}
}
function progress(i) {
return () => new Promise((resolve, reject) => {
var elem = document.getElementById("myBar");
elem.style.width = 100 * i + '%';
resolve(i);
});
}
$(document).ready(function() {
$('body').on('loadedmetadata', 'video', function() {
console.log('loaded video');
});
});
function adjustVid() {
var el = document.getElementsByTagName('video')[0];
if (!el) {
return;
}
var viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var viewportHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
var el = document.getElementsByTagName('video')[0];
var imgWidth = el.videoWidth;
var imgHeight = el.videoHeight;
var gridratio = imgHeight / imgWidth;
if (imgHeight > viewportHeight - adjustVidHeight) {
//console.log('height:' +viewportHeight);
var newHeight = viewportHeight - adjustVidHeight;
var newWidth = newHeight / gridratio;
} else {
var newHeight = imgHeight;
var newWidth = imgWidth;
}
if (newWidth > viewportWidth - 400) {
//console.log('width:' + viewportWidth);
var newWidth = viewportWidth - 400;
var newHeight = newWidth * gridratio;
}
el.style.height = newHeight + "px";
el.style.width = newWidth + "px";
}
$(window).resize(function() {
adjustVid();
});
function setupselect() {
return () => new Promise((resolve, reject) => {
if (ismac) {
var ffprobe = spawnsync(ffprobepath, ['-select_streams', 'v:0', '-print_format', 'json', '-show_streams', '-i', filelist[0]]);
} else {
var ffprobe = spawnsync('cmd.exe', ['/c', '"' + ffprobepath + '"', '-select_streams', 'v:0', '-print_format', 'json', '-show_streams', '-i', winoriginal], {
windowsVerbatimArguments: true,
cwd: process.cwd(),
env: process.env,
stdio: 'pipe',
encoding: 'utf-8'
});
}
var ffprobeOb = JSON.parse(ffprobe.stdout);
window.width = ffprobeOb.streams[0].width;
window.height = ffprobeOb.streams[0].height;
aspect = window.width / window.height;
if (ffprobeOb.streams[0].tags) {
if (ffprobeOb.streams[0].tags.rotate == 90 || ffprobeOb.streams[0].tags.rotate == 270) {
aspect = 1 / aspect;
}
}
window.duration = Number(ffprobeOb.streams[0].duration);
window.fps = precisionRound(eval(ffprobeOb.streams[0].r_frame_rate), 4);
window.codec = ffprobeOb.streams[0].codec_name;
window.tpf = 1 / eval(window.fps);
var canvasheight = parseInt(window.height * 600 / window.width);
stillcount = ffprobeOb.streams[0].nb_frames;
resolve(1);
});
}
function createsample() {
$('#message').html('creating your sample');
fullviding = previewing = false;
sampling = true;
$('#helpmodal').html('<ul><li>be patient while I prepare your sample</li><li>If your clip is long or computer is slow, get a coffee</li></ul>');
fileonly = path.basename(filelist[0], path.extname(filelist[0]));
$('#myProgress').show();
$('button').hide();
$('#cancel').show();
var myqueue = [];
if (selectedMaxAngle == 360) {
selectedMaxAngle = -1;
} else {
selectedMaxAngle = selectedMaxAngle * Math.PI / 360;
}
if ($('#tripod').val() == '1') {
selectedsmooth = 0;
}
var samplelength = window.timeend - window.timestart;
var transformvf = ' vidstabdetect -f null - ';
var camera = $('#camera').val();
var crop = $('#crop').val();
var deshake = ',vidstabtransform=' + 'optalgo=' + camera + ':crop=' + crop + ':smoothing=' + selectedsmooth + ':input=' + trf + ':relative=1:maxshift=' + selectedmaxshift + ':maxangle=' + selectedMaxAngle + ',unsharp=5:5:0.8:3:3:0.4';
var vftext = 'setsar=1,scale=trunc(in_w/2)*2:trunc(in_h/2)*2';
var transformvf = vftext + ',vidstabdetect=' + 'result=' + trf + ':shakiness=' + selectedshakiness + ':accuracy=' + selectedaccuracy;
var samplefile = workdir + '/sample.mp4';
var mergedfile = workdir + '/merged.mp4';
var clippedOriginal = workdir + '/clipped.mp4';
if (aspect > 1) {
var stack = "vstack";
} else {
var stack = "hstack";
}
if (ismac) {
var infile = filelist[0];
} else {
var infile = winoriginal;
}
myqueue.push(customSpawn(ffmpegpath, ['-ss', window.timestart, '-t', samplelength, '-i', infile, '-y', '-vf', transformvf, '-f', 'null', '-']));
myqueue.push(progress(0.33));
myqueue.push(customSpawn(ffmpegpath, ['-ss', window.timestart, '-t', samplelength, '-i', infile, '-y', '-vf', vftext + deshake, samplefile]));
myqueue.push(progress(0.66));
myqueue.push(customSpawn(ffmpegpath, ['-ss', window.timestart, '-t', samplelength, '-i', infile, '-y', '-vf', vftext, clippedOriginal]));
myqueue.push(customSpawn(ffmpegpath, ['-i', clippedOriginal, '-i', samplefile, '-y', '-filter_complex', stack, mergedfile]));
myqueue.push(progress(1));
myqueue.push(wait(400));
myqueue.push(sampledump(1));
myqueue.push(progress(0));
queue(myqueue).then(([cmd, args]) => {
console.log(cmd + ' finished - all finished');
}).catch(TypeError, function(e) {}).catch(err => console.log(err));
}
function sampledump() {
return () => new Promise((resolve, reject) => {
newhelp = '<ul><li>Check to see if you like the amount of smoothing</li><li>Pay special attention to artifacts on the edges of the video</li><li>If you don\'t like the smoothing, click Change Settings.</li><li>You can save this comparison sample by clicking Save Sample </li></ul>';
if (!helpviewing) {
$('#helpmodal').html(newhelp);
} else {
helppending = true;
}
var seconds = new Date().getTime() / 1000;
var videoheight = parseInt(2 * window.height * 600 / window.width) + 'px';
//var cliphtml = '<video class=sample loop height=' + videoheight + ' width="600px" autoplay loop muted controls><source src="' + workdir + '/merged.mp4?v' + seconds + '" type=video/mp4></video>';
$('#myProgress,#cancel').hide();
$('#message').hide();
$('video').remove();
$('.slidecontainer').hide();
//$('#selecttrim').prepend(cliphtml);
$('video').remove();
var video = document.createElement("video");
var sourceClip = document.createElement("source");
video.class = "sample";
video.controls = "true";
video.muted = "true";
video.autoplay = "true";
video.loop = "true";
sourceClip.src = workdir + '/merged.mp4?v' + seconds;
video.appendChild(sourceClip);
video.addEventListener('loadedmetadata', (event) => {
adjustVidHeight = 120;
adjustVid();
});
var el = document.getElementById("selecttrim");
el.prepend(video);
$('#selecttrim,#restart,#changesettings,#fullvid,#savesample').show();
$(document).keydown(function(e) {
if (e.which == 32) { //up
e.preventDefault();
if (vid.paused) {
$('#previewclip').html('stop');
vid.currentTime = window.timestart;
vid.play();
} else {
vid.pause();
$('#previewclip').html('preview');
}
}
});
setupclip();
resolve(i);
});
}
function preptemp() {
return () => new Promise((resolve, reject) => {
$('#message').html('preparing your preview');
$('#message').show();
resolve(1);
});
}
var fileonly, previewing, fullviding, sampling;
function preview() {
//console.log('preview');
var ext = path.extname(filelist[0]).replace('.', '');
const video = document.createElement('video');
var canplay = video.canPlayType('video/' + ext);
fullviding = sampling = false;
previewing = true;
var outfile = workdir + '/temp.mp4'
$('#helpmodal').html('<ul><li>be patient while I prepare your preview</li><li>If your clip is long or computer slow, get a coffee</li></ul>');
fileonly = path.basename(filelist[0], path.extname(filelist[0]));
//giffile = workdir + '/' + fileonly + '.gif';
//$('#loading-container').show();
$('#myProgress').show();
$('button').hide();
if (!fs.existsSync(workdir)) {
fs.mkdirSync(workdir);
}
var myqueue = [];
var vftext = 'scale=iw*min(1\\,min(600/iw\\,480/ih)):-1,setsar=1,scale=trunc(in_w/2)*2:trunc(in_h/2)*2';
myqueue.push(setupselect());
if (!fs.existsSync(outfile)) {
if (canplay == "") {
if (ismac) {
var infile = filelist[0];
} else {
var infile = winoriginal;
}
myqueue.push(preptemp());
myqueue.push(customSpawn(ffmpegpath, ['-i', infile, '-an', '-y', '-vf', vftext, outfile]));
myqueue.push(progress(1));
myqueue.push(wait(400));
myqueue.push(previewdump(1, outfile));
myqueue.push(progress(0));
} else {
myqueue.push(previewdump(1, filelist[0]));
}
} else {
myqueue.push(previewdump(1, outfile));
}
queue(myqueue).then(([cmd, args]) => {
console.log(cmd + ' finished - all finished');
}).catch(TypeError, function(e) {}).catch(err => console.log(err));
}
function wait(ms) {
return () => new Promise((resolve, reject) => {
setTimeout(function() {
resolve(1);
}, ms);
});
}
function previewdump(i, file) {
return () => new Promise((resolve, reject) => {
newhelp = '<ul><li>Drag the start and stop sliders to trim your clip</li><li>Click the <span class=bordered>PREVIEW</span> button to preview your selection</li><li>You can also use the <span class=bordered>SPACEBAR</span> to start and pause your clip</li><li>You can also use your keyboard ARROW KEYS <ul id=arrowkeys><li>↑</li><li>←</li><li>↓</li><li>→</li></ul> to fine tune your selection</li></ul>';
if (!helpviewing) {
$('#helpmodal').html(newhelp);
} else {
helppending = true;
}
var seconds = new Date().getTime() / 1000;
//var videoheight = parseInt(window.height * 600 / window.width) + 'px';
//var cliphtml = '<video class=added id=clip loop muted height=' + videoheight + ' width="600px;" onloadeddata="javascript:adjustVid();"><source src="' + file + '?v' + seconds + '" type=video/mp4></video>';
$('#myProgress').hide();
$('video').remove();
var video = document.createElement("video");
var sourceClip = document.createElement("source");
video.class = "added";
video.id = "clip";
//video.controls="true";
video.muted = "true";
video.loop = "true";
sourceClip.src = file + '?v' + seconds;
video.appendChild(sourceClip);
video.addEventListener('loadedmetadata', (event) => {
adjustVidHeight = 200;
adjustVid();
});
var el = document.getElementById("selecttrim");
el.prepend(video);
//$('#selecttrim').prepend(cliphtml);
$('#slidecontainer').show();
$('#message').html('select the preview clip');
$('#selecttrim, #message,#restart,#trimok,.slidecontainer,#previewclip,#createsample').show();
$(document).keydown(function(e) {
if (e.which == 32) { //up
e.preventDefault();
if (vid.paused) {
$('#previewclip').html('stop');
vid.currentTime = window.timestart;
vid.play();
html5Slider.noUiSlider.destroy();
updatescrubber();
} else {
vid.pause();
$('#previewclip').html('preview');
html5Slider.noUiSlider.destroy();
loadscrubber();
}
}
});
setupclip();
resolve(i);
});
}
$('#restart').click(function() {
spawnsync('rm', ['-rf', workdir]);
$("#fadetoblack").show();
$("#fadetoblack").animate({
opacity: 1,
}, 1000, function() {
location.reload();
});
});
$('#cancel').click(function() {
spawnsync('rm', ['-rf', workdir]);
$("#fadetoblack").show();
kill(child.pid);
$("#fadetoblack").animate({
opacity: 1,
}, 1000, function() {
location.reload();
});
});
var fullvid = 0;
$('#fullvid').click(function() {
window.t0 = performance.now();
fullviding = 1;
$('#options,button,#gifmbwrap,#selecttrim,#canvaswrap,.btn').hide();
$('#helpmodal').html('<ul><li>Please wait while vidSmooth performs actual magic</li></ul>');
$('.dimentionsselectioncontainer').hide();
$('#myProgress,#cancel').show();
$('#message').html('smoothing your video').show();
var myqueue = [];
myqueue.push(progress(0));
if (!fs.existsSync(workdir)) {
fs.mkdirSync(workdir);
}
if (selectedMaxAngle == 360) {
selectedMaxAngle = -1;
} else {
selectedMaxAngle = selectedMaxAngle * Math.PI / 360;
}
if ($('#tripod').val() == '1') {
selectedsmooth = 0;
}
var samplelength = window.timeend - window.timestart;
var transformvf = ' vidstabdetect -f null - ';
var camera = $('#camera').val();
var crop = $('#crop').val();
var tripod = $('#tripod').val();
var deshake = 'vidstabtransform=' + 'optalgo=' + camera + ':crop=' + crop + ':smoothing=' + selectedsmooth + ':input=' + trf + ':relative=1:maxshift=' + selectedmaxshift + ':maxangle=' + selectedMaxAngle + ',unsharp=5:5:0.8:3:3:0.4';
var transformvf = 'vidstabdetect=' + 'result=' + trf + ':shakiness=' + selectedshakiness + ':accuracy=' + selectedaccuracy;
if (ismac) {
var finalFile = workdir + '/final.mp4';
var infile = filelist[0];
} else {
var finalFile = workdir + '\\final.mp4';
var infile = winoriginal;
}
myqueue.push(customSpawn(ffmpegpath, ['-i', infile, '-y', '-vf', transformvf, '-f', 'null', '-']));
myqueue.push(customSpawn(ffmpegpath, ['-i', infile, '-y', '-crf', '17', '-vf', deshake, finalFile]));
myqueue.push(progress(1));
myqueue.push(wait(300));
myqueue.push(showfinal(1));
queue(myqueue).then(([cmd, args]) => {
console.log(cmd + ' finished - all finished');
}).catch(TypeError, function(e) {}).catch(err => console.log(err));
});
function setfinalrender() {
return () => new Promise((resolve, reject) => {
gifrender = true;
resolve(i);
});
}
function precisionRound(number, precision) {
var factor = Math.pow(10, precision);
return Math.round(number * factor) / factor;
}
function getFilesizeInBytes(filename) {
const stats = fs.statSync(filename)
const fileSizeInBytes = stats.size
return fileSizeInBytes
}
function showfinal(i) {
return () => new Promise((resolve, reject) => {
var percdone = 1;
var perctext = '[' + (percdone * 100).toFixed(2) + '% done]';
$('#message').html('smoothing your video<br>' + perctext);
//var timeleft = parseInt($('#label').css('width')) * 30;
var timeleft = 0;
newhelp = '<ul><li>Your smoothing is done, now do a little dance!</li><li>If you don\'t like the final result, click Start Over</li><li>If you\'re happy, click Save and choose a name and location.</li></ul>';
if (!helpviewing) {
$('#helpmodal').html(newhelp);
} else {
helppending = true;
}
//$('#message').delay(timeleft).html('Done! Drag your gif to an app, location, or tweet');
$("#message").show();
$("#message").html('Done!'); // Drag your gif to an app, location, or tweet');
//$('#finalsize').html(size + 'MB/' + calcgifwidth + 'x' + calcgifheight + '/' + precisionRound(giflength, 2) + 's');
var seconds = new Date().getTime() / 1000;
//var cliphtml = '<video class=sample id=final oop height=auto width="100%" autoplay loop muted controls><source src="' + workdir + '/final.mp4?v' + seconds + '" type=video/mp4></video>';
$('video').remove();
var video = document.createElement("video");
var sourceClip = document.createElement("source");
video.id = "final";
video.controls = "true";
video.muted = "false";
video.autoplay = "true";
video.loop = "true";
sourceClip.src = workdir + '/final.mp4?v' + seconds;
video.appendChild(sourceClip);
video.addEventListener('loadedmetadata', (event) => {
adjustVidHeight = 120;
adjustVid();
});
var el = document.getElementById("result");
el.prepend(video);
$('#result').css('display', 'block');
//$('#result').append(cliphtml).css('display', 'block');
document.getElementById('final').ondragstart = (event) => {
event.preventDefault();
ipcRenderer.send('ondragstart', workdir + '/final.mp4')
}
$('#myProgress,#cancel').hide();
$('#finalsize').show();
$('#save').show();
$('#restart').show();
$('#changesettings').show();
//$('#openTwitter').show(0);
resolve(i);
});
}
$('#save').click(function() {
fileonly = path.basename(filelist[0], path.extname(filelist[0]));
filename = dialog.showSaveDialog({
title: 'Save your file',
defaultPath: '~/Desktop/' + fileonly + '_smoothed.mp4'
}).then(result => {
filename = result.filePath;
if (filename == "") {
console.log('cancel save');
return;
}
var filearr = filename.toLowerCase().split('.');
var ext = filearr.pop();
if (ext != 'mp4') {
filename = filename + '.mp4';
}
var finalfile = workdir + '/final.mp4';
fs.copyFile(finalfile, filename, (err) => {
if (err) throw err;
shell.openItem(path.dirname(filename));
focusFinder();
});
}).catch(err => {
console.log(err)
})
});
$('#savesample').click(function() {
fileonly = path.basename(filelist[0], path.extname(filelist[0]));
filename = dialog.showSaveDialog({
title: 'Save your sample',
defaultPath: '~/Desktop/' + fileonly + '_sample_smoothed.mp4'
}).then(result => {
filename = result.filePath;
if (filename == "") {
console.log('cancel save');
return;
}
var filearr = filename.toLowerCase().split('.');
var ext = filearr.pop();
if (ext != 'mp4') {
filename = filename + '.mp4';
}
var finalfile = workdir + '/merged.mp4';
fs.copyFile(finalfile, filename, (err) => {
if (err) throw err;
shell.openItem(path.dirname(filename));
focusFinder();
});
}).catch(err => {
console.log(err)
})
});
const {
ipcRenderer
} = require('electron');
ipcRenderer.send('resethtmlsize', '1');
$('#help').click(function() {
$('#helpmodalwrap').fadeIn();
helpviewing = true;
});
$('#helpmodalwrap').click(function() {
$(this).fadeOut(function() {
helpviewing = false;
if (helppending) {
$('#helpmodal').html(newhelp);
helppending = false;
}
});
});
var selectedsmooth, selectedmaxshift, selectedMaxAngle, selectedshakiness, selectedaccuracy;
function showoptions() {
if (!fs.existsSync(workdir)) {
fs.mkdirSync(workdir);
}
if (!ismac) {
winoriginal = workdir + '\\original' + path.extname(filelist[0]);
fs.copyFile(filelist[0], winoriginal, (err) => {
if (err) throw err;
});
}
$('#result,button,#selecttrim,#openmodalwrap,#message').hide();
$('#restart,#fullvid,#options,#sample').show();
//$('#helpmodal').html('<ul><li>Please wait while vidSmooth performs actual magic</li><li>If your endpoint was a specific file size, this might take a while</li></ul>');
$('#helpmodal').html('<p><ul><li>accuracy<br><ul><li>Set the accuracy of the detection process. It must be a value in the range 1-15. A value of 1 means low accuracy, a value of 15 means high accuracy. Default value is 15.</ul><li>shakiness<br><ul><li>Set the shakiness of input video or quickness of camera. It accepts a integer in the range 1-10, a value of 1 means little shakiness, a value of 10 means strong shakiness. Default value is 5.</ul><li>smoothing<br><ul><li>Set the number of frames (value*2 + 1), used for lowpass filtering the camera movements. Default value is 10.<br>For example, a number of 10 means that 21 frames are used (10 in the past and 10 in the future) to smoothen the motion in the video. A larger value leads to a smoother video, but limits the acceleration of the camera (pan/tilt movements). 0 is a special case where a static camera is simulated.</ul><li>maxshift<br><ul><li>Set maximal number of pixels to translate frames. </ul><li>maxangle<br><ul><li>Set maximal angle in radians (degree*PI/180) to rotate frames. </ul><li>crop method<br><ul><li>Specify how to deal with empty frame borders that may be shrinked-in due to movement compensation. Available values are:<br>keep: Keep image information from previous frame (default).<br>black: Fill the border-areas black.</ul><li>camera path<br><ul><li>Set the camera path optimization algorithm. Accepted values are:<br>gauss: Gaussian kernel low-pass filter on camera motion (default).<br>avg: Averaging on transformations.</ul><li>tripod<br><ul><li>Enables virtual tripod mode if set to "on", which is equivalent to smoothing=0. Default value is "off".</ul></ul>');
selectedsmooth = store.get('selectedsmooth') || 50;
var pipFormatssmooth = {
'0': '0',
'25': '25',
'50': '50',
'75': '75',
'100': 'max'
};
//window.tpf=2000/34329;
var selectsmooth = document.getElementById("smooth");
noUiSlider.create(selectsmooth, {
step: 1,
//limit:window.timeend,
tooltips: false, // [true, wNumb({ suffix: 's'}), true],
start: selectedsmooth,
connect: true,
range: {
'min': [0, 1],
'25%': [25, 1],
'50%': [50, 1],
'75%': [75, 1],
'max': [100, 1]
},
pips: {
mode: "range",
format: {
to: function(a) {
return pipFormatssmooth[a];
}
}
}
});
selectsmooth.noUiSlider.on("slide", function(values, handle) {
selectedsmooth = parseInt(values[handle]);
store.set('selectedsmooth', selectedsmooth);
});
selectedmaxshift = store.get('selectedmaxshift') || 150;
var pipFormatsmaxshift = {
'0': '0',
'50': '50',
'100': '100',
'150': '150',
'200': '200'
};
//window.tpf=2000/34329;
var selectmaxshift = document.getElementById("maxshift");
noUiSlider.create(selectmaxshift, {
step: 1,
//limit:window.timeend,
tooltips: false, // [true, wNumb({ suffix: 's'}), true],