-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
502 lines (414 loc) · 14.7 KB
/
index.html
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
<!DOCTYPE html>
<html>
<link>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
<link href="public/main.css" rel="stylesheet">
</head>
<body>
<div id="version"></div>
<script>
const { version } = require('./package.json');
const versionHolder = document.getElementById('version');
versionHolder.innerText = `v${version}`;
versionHolder.addEventListener('click', async () => {
await open(`https://github.com/Aurora-Hunters/timelapse-maker/releases/tag/v${version}`);
});
</script>
<script>
const { hawkToken } = require('./env.json');
window.tlOptions = {};
if (hawkToken) {
const machineIdSync = require('node-machine-id').machineIdSync;
const HawkCatcher = require('@hawk.so/javascript');
const hawk = new HawkCatcher({
token: hawkToken,
release: `v${version}`,
user: {
id: machineIdSync({original: true})
},
beforeSend: (event) => {
console.log(event);
event.context = {
options: window.tlOptions
}
return event;
}
});
}
</script>
<!-- <div id="help"></div>-->
<div class="table">
<div class="row">
<div class="cell">
<div class="header">Time Lapse Maker</div>
</div>
</div>
<div class="row mb-x">
<div class="cell cell--centered">
<div class="button select-directory" id="select-directory-button">Select Folder</div>
</div>
<div id="selected-directory" class="cell cell--centered cell--column hidden">
<div id="selected-directory-name" class="dirdata"></div>
<div id="selected-directory-images"></div>
</div>
</div>
<div id="tl-options" class="hidden">
<div class="row">
<div class="cell">
Frames per Second
</div>
<div class="cell cell--stretched">
<input type="text" id="inputFPS" value="24" class="short-number">
</div>
</div>
<!-- <div class="row">-->
<!-- <div class="cell">-->
<!-- <h2>Output video params</h2>-->
<!-- </div>-->
<!-- </div>-->
<!-- <div class="row">-->
<!-- <div class="cell">-->
<!-- Output video FPS. How much images you want to see per second (outputFPS)-->
<!-- </div>-->
<!-- <div class="cell cell--stretched">-->
<!-- <input type="text" id="outputFPS" value="30" class="short-number">-->
<!-- </div>-->
<!-- </div>-->
<div class="row">
<div class="cell">
Video resolution
<br>
<span class="video-resolution" onclick="updateResolution('', '')" title="The highest available resolution">Source</span>
<span class="video-resolution" onclick="updateResolution('', '720')" title="Small video for social networks">HD</span>
<span class="video-resolution" onclick="updateResolution('', '1080')" title="The most common quality video size">Full HD</span>
<span class="video-resolution" onclick="updateResolution('', '2160')" title="High quality video for Youtube">4K</span>
<!-- <span class="video-resolution" onclick="updateResolution('', '4320')">8K</span>-->
</div>
<div class="cell cell--stretched">
<input type="text" id="videoWidth" value="" placeholder="auto" class="short-number">
<span class="cross-separator">×</span>
<input type="text" id="videoHeight" value="1080" placeholder="auto" class="short-number">
</div>
</div>
<div class="row">
<div class="cell">
<span>Video filename</span>
</div>
<div class="cell cell--stretched">
<input type="text" id="timelapseName" value="timelapse">
<span class="ext-ending">.mp4</span>
</div>
</div>
<div class="row">
<div class="cell cell--centered mt-2x">
<div class="button" id="createTlButton">Create video</div>
<div class="button button--warning hidden" id="stopTlButton">Stop video creation</div>
</div>
</div>
<div class="row">
<div class="cell">
<div id="progressbar" class="progressbar hidden">
<div id='progressbar__progress' class="progressbar__progress"></div>
</div>
<!-- <div id="previewImage" class="preview-image"></div>-->
</div>
</div>
</div>
</div>
<script>
const path = require('path');
const fs = require('fs');
const open = require('open');
const FileToObject = require('./app/utils/file-to-object');
const sizeOf = require('image-size');
/**
* Global variable
* List of images
*/
let images = [];
/**
* Global variable for ffmpeg command
*/
let command;
const sourceResolution = {
width: 0,
height: 0
}
// const helpHolder = document.getElementById('help');
// helpHolder.innerText = `Help`;
// helpHolder.addEventListener('click', async () => {
// await open(`https://github.com/Aurora-Hunters/timelapse-maker/blob/master/docs/help.md`);
// });
const selectDirectoryButton = document.getElementById('select-directory-button');
const selectedDirectory = document.getElementById('selected-directory');
const selectedDirectoryName = document.getElementById('selected-directory-name');
const selectedDirectoryImages = document.getElementById('selected-directory-images');
const inputFPS = document.getElementById('inputFPS');
const outputFPS = document.getElementById('outputFPS');
const videoWidth = document.getElementById('videoWidth');
const videoHeight = document.getElementById('videoHeight');
const timelapseName = document.getElementById('timelapseName');
const createTlButton = document.getElementById('createTlButton');
const stopTlButton = document.getElementById('stopTlButton');
const tlOptions = document.getElementById('tl-options');
const progressBar = document.getElementById('progressbar');
const progressBarProgress = document.getElementById('progressbar__progress');
// const previewImage = document.getElementById('previewImage');
const progressHit = function (percentage) {
const normalizedPercentage = percentage > 0 ? (percentage < 100 ? percentage : 100) : 0;
progressBarProgress.style.width = `${normalizedPercentage}%`;
};
/**
* Cached input element for preventing memory leak
* @type {HTMLInputElement}
*/
let inputElement = null;
selectDirectoryButton.addEventListener('click', (event) => {
/**
* Check cached INPUT element
* If inputElement is null, create INPUT element cache
*/
if (inputElement === null) {
/**
* Create a new INPUT element
* @type {HTMLElement}
*/
inputElement = document.createElement('INPUT');
/**
* Set a 'FILE' type for this input element
* @type {string}
*/
inputElement.type = 'file';
inputElement.webkitdirectory = true;
/**
* Do not show element
*/
inputElement.style.display = 'none';
/**
* Append element to the body
* Fix using module on mobile devices
*/
document.body.appendChild(inputElement);
/**
* Create event handler for resolving files
* @param {Event} event
*/
const resolveFiles = async (event) => {
/**
* Get files from input field
*/
const files = event.target.files;
/**
* Process selected files
*/
await directorySelected([...files]);
/**
* Remove event handler for preventing memory leak
*/
inputElement.removeEventListener('change', resolveFiles);
/**
* Remove element from a DOM
*/
document.body.removeChild(inputElement);
/**
* Remove input element
*/
inputElement = null;
};
/**
* Add onchange listener for «choose file» pop-up
*/
inputElement.addEventListener('change', resolveFiles, false);
}
/**
* Fire click event on «input file» field
*/
inputElement.click();
})
createTlButton.addEventListener('click', async (event) => {
checkSelectedImages();
createMovie({
images: images,
filename: timelapseName.value,
inputFPS: inputFPS.value,
resolution: {
width: videoWidth.value,
height: videoHeight.value
}
});
});
stopTlButton.addEventListener('click', async (event) => {
try {
command.kill();
} catch (e) {}
});
const updateResolution = (width = '?', height = '?') => {
if (!width && !height) {
width = sourceResolution.width;
height = sourceResolution.height;
}
videoWidth.value = width;
videoHeight.value = height;
}
const directorySelected = async (chosenFiles) => {
let filesList = [];
console.log('chosenFiles', chosenFiles);
chosenFiles = chosenFiles.map((file, index) => {
return FileToObject(file);
});
let shortestDirname = false;
filesList = chosenFiles
.filter(file => {
return (file.type).startsWith('image');
})
.sort(function (a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
})
.map(function (file) {
const dirname = path.dirname(file.path);
if (!shortestDirname || dirname.length < shortestDirname.length) {
shortestDirname = dirname;
}
return file.path;
});
// .filter(filepath => {
// const dirname = path.dirname(filepath);
//
// return dirname === shortestDirname;
// });
images = filesList;
selectedDirectoryImages.innerText = `${images.length} images`;
selectedDirectoryImages.addEventListener('click', checkSelectedImages)
const dimensions = sizeOf(images[0]);
sourceResolution.width = dimensions.width;
sourceResolution.height = dimensions.height;
// updateResolution(sourceResolution.width, sourceResolution.height);
const dirname = path.basename(shortestDirname);
selectedDirectoryName.innerText = dirname;
selectedDirectory.classList.remove('hidden');
tlOptions.classList.remove('hidden');
};
const checkSelectedImages = () => {
images = images.filter(image => {
try {
if (fs.existsSync(image)) {
return true;
}
} catch(err) {
console.error(err)
}
return false;
})
selectedDirectoryImages.innerText = `${images.length} images`;
}
const createMovie = async function (options) {
const defaultOptions = {
images: [],
filename: `timelapse`,
format: 'mp4',
inputFPS: 24,
outputFPS: 30,
resolution: {
width: '',
height: ''
}
};
options = Object.assign(defaultOptions, options);
if (options.resolution.width === '') {
options.resolution.width = '?';
}
if (options.resolution.height === '') {
options.resolution.height = '?';
}
if (inputFPS > 30) {
options.outputFPS = inputFPS
}
console.log('options', options);
if (options.images.length === 0) {
return;
}
const TEMPDIR_PATH = path.dirname(options.images[0]);
const FILE_SHOW = path.join(TEMPDIR_PATH, `${options.filename}.${options.format}`);
const LISTFILENAME = path.join(TEMPDIR_PATH, 'list.txt');
let list = '';
options.images.forEach(file => {
if (!fs.existsSync(file)) return;
list += `file '${file}'\n`;
});
await fs.writeFileSync(LISTFILENAME, list);
const ffmpeg = require('fluent-ffmpeg')
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffprobePath = require('@ffprobe-installer/ffprobe').path;
ffmpeg.setFfmpegPath(ffmpegPath.replace('app.asar', 'app.asar.unpacked'));
ffmpeg.setFfprobePath(ffprobePath.replace('app.asar', 'app.asar.unpacked'));
progressHit(0);
window.tlOptions = options;
await new Promise((resolve, reject) => {
command = ffmpeg(LISTFILENAME)
.inputOptions([
'-f concat',
'-safe 0',
])
.size(`${options.resolution.width}x${options.resolution.height}`)
.videoFilter([
{
filter: 'deflicker',
options: {
size: 7
}
}
])
.outputOption([
'-y',
'-pix_fmt yuv420p',
])
.inputFPS(options.inputFPS)
.fps(options.outputFPS)
.videoCodec(options.format === 'mp4' ? 'libx264' : '')
.toFormat(options.format)
.on('start', function(commandLine) {
console.log('Spawned Ffmpeg with command: ' + commandLine);
progressBar.classList.remove('hidden');
stopTlButton.classList.remove('hidden');
createTlButton.classList.add('hidden');
})
.on('progress', function(progress) {
const percentage = Math.round(progress.frames / (options.images.length * options.outputFPS / options.inputFPS) * 99);
// const frameNumber = Math.round(progress.frames / (options.outputFPS / options.inputFPS));
// previewImage.style.backgroundImage = `url('${options.images[frameNumber]}')`;
progressHit(percentage);
console.log('Processing: ' + percentage + '% done');
})
.on('end', async function(stdout, stderr) {
progressBar.classList.add('hidden');
stopTlButton.classList.add('hidden');
createTlButton.classList.remove('hidden');
console.log('Transcoding succeeded !');
try {
fs.unlinkSync(LISTFILENAME);
} catch (e) {}
await open(FILE_SHOW);
resolve(FILE_SHOW);
})
.on('error', function(err, stdout, stderr) {
progressBar.classList.add('hidden');
stopTlButton.classList.add('hidden');
createTlButton.classList.remove('hidden');
console.log('Cannot process video: ' + err.message);
reject(err);
})
.save(`${FILE_SHOW}`);
});
console.log('FILE_SHOW', FILE_SHOW);
};
</script>
<script src="public/bundle.js"></script>
</body>
</html>