-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.js
394 lines (344 loc) · 14.1 KB
/
main.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
const { app, BrowserWindow, ipcMain, dialog, shell } = require('electron');
const { autoUpdater } = require('electron-updater');
const musicMetadata = require('music-metadata');
var path = require('path');
const sizeOf = require('image-size');
const getColors = require('get-image-colors');
const { resolve } = require('dns');
const { createServer } = require('http');
require('dotenv').config({ path: path.resolve(__dirname, '../.env') })
const execa = require('execa');
var ffmpegPath = '';
//const {render} = require('./src/js/index.js');
const port = 3030
const http = require("http");
const host = 'localhost';
var qs = require('querystring');
const requestListener = async function (req, res) {
try{
switch (req.url) {
//render api route hit
case "/render":
console.log('/render route')
let body = '';
let status = ''
//get body data
try{
req.on('data', async function (data) {
//try{
body += data;
// Too much POST data, kill the connection! 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e6) {
req.connection.destroy();
}
try {
//convert body to json
body = JSON.parse(body)
//console.log('body=',body)
//for each render
for(var x = 0; x < body.length; x++){
//convert render options to ffmpeg arr
let ffmpegArgs = await createFfmpegArgs(body[x]);
console.log('main ffmpegPath = ',ffmpegPath)
console.log('main ffmpegArgs = ',ffmpegArgs)
const process = execa(ffmpegPath, ffmpegArgs)
}
//create renderOptions{} object
let renderOptions = {}
//call render() function
/*
let ffmpegPath = 'node_modules/ffmpeg-ffprobe-static/ffmpeg.exe';
let ffmpegArgs = ["-loop", "1", "-framerate", "2", "-i", "C:\\Users\\marti\\Documents\\IMAGE.jpg", "-i", "C:\\Users\\marti\\Documents\\AUDIO.wav", "-c:a", "pcm_s32le", "-filter_complex", "concat=n=1:v=0:a=1", "-vcodec", "libx264", "-bufsize", "3M", "-filter:v", "scale=w=1920:h=1946,pad=ceil(iw/2)*2:ceil(ih/2)*2", "-crf", "18", "-pix_fmt", "yuv420p", "-shortest", "-tune", "stillimage", "-t", "297", "C:\\Users\\marti\\Documents\\VIDEO.mkv"];
const process = execa(ffmpegPath, ffmpegArgs)
*/
//console.log('process=',process)
finishRequest(res, 'no issue')
} catch (err) {
console.log('inside req.data, err=', err)
status='json err'
console.log('inside err status=',status)
finishRequest(res, status)
}
});
}catch(err){
console.log('req.on err=',err)
}
console.log('ending. status=',status)
//finishRequest(res, status)
}
}catch(err){
console.log('e1:',err)
}
};
async function createFfmpegArgs(renderOptions){
return new Promise(async function (resolve, reject) {
console.log('createFfmpegArgs() called. renderOptions=',renderOptions)
//get output format
let cmdArr = []
cmdArr.push('-loop')
cmdArr.push('1')
cmdArr.push('-framerate')
cmdArr.push('2')
//image input
cmdArr.push('-i')
cmdArr.push(`${renderOptions.image}`)
//audio input(s)
var totalLength = 0
for(var x=0; x < renderOptions.audio.length; x++){
cmdArr.push('-i')
cmdArr.push(`${renderOptions.audio[x]}`)
//get length in seconds of audio file
let length = await getAudioLength(renderOptions.audio[x])
console.log(`${x} length=`,length)
totalLength=totalLength+parseFloat(length)
}
totalLength=Math.ceil(totalLength)
console.log('totalLength=',totalLength)
//audio codec depending on output video format
if(renderOptions.outputFormat == 'mkv'){
cmdArr.push('-c:a')
cmdArr.push('pcm_s32le')
}else if(renderOptions.outputFormat == 'mp4'){
cmdArr.push('-c:a')
cmdArr.push('libmp3lame')
cmdArr.push('-b:a')
cmdArr.push('320k')
}else{
throw 'invalid output video format selected'
}
//filter to concatenate audio
cmdArr.push('-filter_complex')
cmdArr.push(`concat=n=${renderOptions.audio.length}:v=0:a=1`)
//video codec
cmdArr.push('-vcodec')
cmdArr.push('libx264')
//buffer size
cmdArr.push('-bufsize')
cmdArr.push('3M')
//filter to set resolution/padding
cmdArr.push('-filter:v')
//if user has no padding option selected, render vid to exact width/height resolution
if(renderOptions.padding.toLowerCase().trim() == 'none'){
cmdArr.push(`scale=w=${renderOptions.resolution.split('x')[0]}:h=${renderOptions.resolution.split('x')[1]},pad=ceil(iw/2)*2:ceil(ih/2)*2`)
//else padding will be padding hex(#966e6e) color
}else{
//get hex color
var paddingColor = '';
if(renderOptions.padding.toLowerCase().trim()=='white'){
paddingColor='#ffffff'
}else if(renderOptions.padding.toLowerCase().trim()=='black'){
paddingColor='#000000'
}else{
paddingColor=renderOptions.padding
}
cmdArr.push(`format=rgb24,scale=w='if(gt(a,1.7777777777777777),${renderOptions.resolution.split('x')[0]},trunc(${renderOptions.resolution.split('x')[1]}*a/2)*2)':h='if(lt(a,1.7777777777777777),${renderOptions.resolution.split('x')[1]},trunc(${renderOptions.resolution.split('x')[0]}/a/2)*2)',pad=w=${renderOptions.resolution.split('x')[0]}:h=${renderOptions.resolution.split('x')[1]}:x='if(gt(a,1.7777777777777777),0,(${renderOptions.resolution.split('x')[0]}-iw)/2)':y='if(lt(a,1.7777777777777777),0,(${renderOptions.resolution.split('x')[1]}-ih)/2)':color=${paddingColor}`)
}
//crf
cmdArr.push('-crf')
cmdArr.push('18')
//pix_fmt
cmdArr.push('-pix_fmt')
cmdArr.push('yuv420p')
//shortest
cmdArr.push('-shortest')
//stillimage
cmdArr.push('-tune')
cmdArr.push('stillimage')
//set video length (seconds) to trim ending
cmdArr.push('-t')
cmdArr.push(`${Math.ceil(totalLength)}`)
//output
cmdArr.push(`${renderOptions.outputFilepath}`)
//console.log('cmdArr = ', cmdArr)
//let args = ["-loop", "1", "-framerate", "2", "-i", "C:\\Users\\marti\\Documents\\IMAGE.jpg", "-i", "C:\\Users\\marti\\Documents\\AUDIO.wav", "-c:a", "pcm_s32le", "-filter_complex", "concat=n=1:v=0:a=1", "-vcodec", "libx264", "-bufsize", "3M", "-filter:v", "scale=w=1920:h=1946,pad=ceil(iw/2)*2:ceil(ih/2)*2", "-crf", "18", "-pix_fmt", "yuv420p", "-shortest", "-tune", "stillimage", "-t", "297", "C:\\Users\\marti\\Documents\\VIDEO.mkv"]
resolve(cmdArr)
})
}
async function getAudioLength(filepath){
return new Promise(async function (resolve, reject) {
const metadata = await musicMetadata.parseFile(filepath, { duration: true });
//console.log(`getAudioLength(${filepath}) duration=`,metadata.format.duration)
resolve(metadata.format.duration);
})
}
async function finishRequest(res, status){
console.log('finishRequest() status=',status)
//res.writeHead(200)
res.write(`res write status=${status}`);
res.end('');
}
/*
const server = http.createServer(requestListener);
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});
*/
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
enableRemoteModule: true,
nodeIntegration: true,
contextIsolation: false,
//debug tools
//showDevTools: false
},
//framless
frame: false,
backgroundColor: '#FFF',
icon: "./build/icon.png"
});
//load html
mainWindow.loadFile('./src/index.html');
//open devtools
//mainWindow.webContents.openDevTools()
//setup server
mainWindow.on('closed', function () {
mainWindow = null;
});
// check if there are any updates availiable once main window is ready. if there are, automatically download
mainWindow.once('ready-to-show', () => {
autoUpdater.checkForUpdatesAndNotify();
});
//notify that update is available
autoUpdater.on('update-available', () => {
mainWindow.webContents.send('update_available');
});
//notify that update has downloaded
autoUpdater.on('update-downloaded', () => {
mainWindow.webContents.send('update_downloaded');
});
}
app.on('ready', () => {
createWindow();
});
app.on('window-all-closed', function () {
app.quit();
});
app.on('activate', function () {
if (mainWindow === null) {
createWindow();
}
});
//send app version to main window
ipcMain.on('app_version', (event) => {
event.sender.send('app_version', { version: app.getVersion() });
});
//auto-update quit and install
ipcMain.on('restart_app', () => {
autoUpdater.quitAndInstall();
});
//open folder dir picker window and return string of folder path
ipcMain.handle('choose-dir', async (event) => {
dir = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory'],
message: 'Choose your output folder',
buttonLabel: 'Select this folder'
});
return dir.filePaths[0];
});
//open directory
ipcMain.handle('open-dir', async (event, filepath) => {
shell.showItemInFolder(filepath)
});
//open file with default application
ipcMain.handle('open-file', async (event, filepath) => {
shell.openPath(filepath)
});
//convert rgb string to hex
function rgbToHex(color) {
return "#" + componentToHex(parseInt(color[0])) + componentToHex(parseInt(color[1])) + componentToHex(parseInt(color[2]));
}
//convert to int to hex
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
//get colors using vibrant.js for image file
ipcMain.handle('get-image-colors', async (event, filename) => {
return new Promise(async function (resolve, reject) {
var colorData = []
try {
//get color data from image
//await getColors(path.join(__dirname, 'img.jpg')).then(colors => {
await getColors(filename).then(colors => {
//convert each swatch from rgb to hex and add to colorData{}
for (var x = 0; x < colors.length; x++) {
let rgbColor = colors[x]._rgb
let hexColor = rgbToHex(rgbColor)
colorData.push(hexColor)
}
})
resolve(colorData);
} catch (err) {
reject(err)
}
})
})
//get metadata for audio file
ipcMain.handle('get-audio-metadata', async (event, filename) => {
const metadata = await musicMetadata.parseFile(filename, { duration: true });
//console.log(`Music-metadata: track-number = ${metadata.common.track.no}, duration = ${metadata.format.duration} sec.`);
return metadata;
});
//get audio file length
ipcMain.handle('get-audio-length', async (event, filename) => {
return(await getAudioLength(filename))
});
//set ffmpegPath as global var
ipcMain.handle('set-ffmpeg-path', async (event, path) => {
ffmpegPath = path;
});
//set custom port
ipcMain.handle('set-custom-port', async (event, newPort) => {
let status = '';
try {
app.set('port', process.env.PORT || newPort);
status = 'success'
} catch (err) {
status = err
}
return (status)
});
ipcMain.handle('set-dir', async (event) => {
let defaultPath = path.dirname('/Users');
const { filePaths } = await dialog.showOpenDialog({
properties: ['openDirectory', 'createDirectory'],
defaultPath,
title: 'title',
message: 'message',
buttonLabel: 'buttonLabel'
});
return (filePaths && filePaths.length === 1) ? filePaths[0] : undefined;
});
ipcMain.handle('get-image-resolution', async (event, filename) => {
let width = '';
let height = '';
[width, height] = await getResolution(filename)
return [width, height];
});
async function getResolution(filename) {
return new Promise(async function (resolve, reject) {
sizeOf(filename, function (err, dimensions) {
if (!err) {
width = dimensions.width;
height = dimensions.height
resolve([width, height]);
} else {
console.log('err getting img dimmensions:', err)
reject(err)
}
});
})
}
/*
//handle auto-update events
autoUpdater.on('update-available', () => {
mainWindow.webContents.send('update_available');
});
autoUpdater.on('update-downloaded', () => {
mainWindow.webContents.send('update_downloaded');
});
*/