-
Notifications
You must be signed in to change notification settings - Fork 2
/
renderer.js
364 lines (320 loc) · 11 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
var currentvideo = '';
var bitrate = '';
function combinevideos(videos)
{
var fs = require('fs');
console.log("combining "+ videos.length +" videos");
startLoading()
var bin = 'resources\\ffmpeg\\bin\\ffmpeg.exe';
if (!fs.existsSync(bin)) {
var bin = 'ffmpeg\\bin\\ffmpeg.exe';
}
var ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(bin);
var mergedname = calcName();
var command = '';
var outpath = require('path').dirname(videos[0].path)+"\\cut\\";
if (!fs.existsSync(outpath)){
fs.mkdirSync(outpath);
}
outpath+=mergedname;
for (let f of videos)
{
var path = f.path;
if(command=='')
command = ffmpeg(path);
else
command.input(path);
}
command.on('error', function(err) {
console.log('An error occurred: ' + err.message);
endLoading()
})
.on('end', function() {
console.log('Merging finished !');
const {shell} = require('electron')
shell.showItemInFolder(outpath);
endLoading();
$("#video").html('<h2>Finished!</h2>\
<p>New video name: '+mergedname+'</p>\
<p>Path: '+outpath+'</p>\
<video\
id="video-active"\
class="video-active"\
width="640"\
height="390"\
controls="controls">\
<source src="'+outpath+'" type="video/mp4">\
</video>');
})
.mergeToFile(outpath, '.');
/*
var command = ffmpeg('C:\\Users\\Chris\\Videos\\Tom Clancy\'s Rainbow Six Siege\\val_trick_1.mp4')
.input('C:\\Users\\Chris\\Videos\\Tom Clancy\'s Rainbow Six Siege\\val_trick_2.mp4')
.on('error', function(err) {
console.log('An error occurred: ' + err.message);
endLoading()
})
.on('end', function() {
console.log('Merging finished !');
endLoading()
})
.mergeToFile('C:\\Users\\Chris\\Videos\\Tom Clancy\'s Rainbow Six Siege\\merged.mp4', '.');
*/
}
function loadVideo(file)
{
var path = require('path').dirname(file);
var ffmpeg = require('fluent-ffmpeg');
ffmpeg.ffprobe(file,function(err, metadata) {
bitrate = metadata.streams[0].bit_rate;
console.log("Found bitrate of video! It's: "+bitrate);
});
currentvideo = file;
console.log("loading "+file);
var videotag = '<h2>Loaded "'+require('path').basename(file)+'"</h2>\
<p>from path: '+path+'</p>\
<video\
id="video-active"\
class="video-active"\
width="640"\
height="390"\
controls="controls">\
<source src="'+file+'" type="video/mp4">\
</video>\
<div>\
Name of the new Video:<br/>\
<input type="text" id="newname" value="'+calcName()+'"/>\
</div>\
<div id="current">0:00</div>\
<div id="duration">0:00</div>\
<button onClick="setStarttime()">Set starttime</button> <input id="starttime" type="text" placeholder="starttime"/><br/>\
<button onClick="setEndtime()">Set endtime</button> <input id="endtime" type="text" placeholder="endtime"/>\<br/>\
<button onClick="saveScreenshot()">Save screenshot</button>\
<div class="checkbox">\
<label><input id="nosound" type="checkbox" value="1">Remove sound</label>\
</div>\
<div class="checkbox">\
<label><input id="halfsize" type="checkbox" value="1">Half the resolution of the video</label>\
</div>\
<div class="checkbox">\
<label><input id="gif" type="checkbox" value="1">Create as gif (big files!)</label>\
</div>\
<div class="checkbox">\
<label><input id="pictshare" type="checkbox" value="1">Upload to PictShare after cut</label>\
</div>\
<div class="checkbox">\
<label><input id="fps" type="checkbox" value="1">Set to 30 FPS</label>\
</div>\
<div class="checkbox">\
<label><input id="nvenc" type="checkbox" checked value="1">Use GPU encoding</label>\
</div>\
\
<div class="radio">\
<label><input type="radio" name="speedchange" value="4">Quater speed</label>\
</div>\
<div class="radio">\
<label><input type="radio" name="speedchange" value="2">Half speed</label>\
</div>\
<div class="radio">\
<label><input type="radio" name="speedchange" checked value="1">Normal speed</label>\
</div>\
<div class="radio">\
<label><input type="radio" name="speedchange" value="0.5">2x faster</label>\
</div>\
<div class="radio">\
<label><input type="radio" name="speedchange" value="0.25">4x faster</label>\
</div>\
<button onClick="cutIt()">Cut it!</button>\
';
$("#video").html(videotag);
$("#video-active").on(
"timeupdate",
function(event){
onTrackedVideoFrame(this.currentTime, this.duration);
});
}
function calcName()
{
var Sentencer = require('sentencer');
return Sentencer.make("{{ noun }}{{ noun }}")+'.mp4';
}
function cutIt()
{
var fs = require('fs');
console.log("starting the cutting");
startLoading()
var cmd=require('node-cmd');
var bin = 'resources\\ffmpeg\\bin\\ffmpeg.exe';
var bin2 = 'resources\\ffmpeg\\bin\\ffprobe.exe';
if (!fs.existsSync(bin)) {
var bin = 'ffmpeg\\bin\\ffmpeg.exe';
var bin2 = 'ffmpeg\\bin\\ffprobe.exe';
}
var path = require('path').dirname(currentvideo)+"\\cut";
if (!fs.existsSync(path)){
fs.mkdirSync(path);
}
var starttime = parseFloat($("#starttime").val());
var duration = parseFloat($("#endtime").val());
var outfile = path+'\\'+ $("#newname").val() ;
var ffmpeg = require('fluent-ffmpeg');
var command = ffmpeg(currentvideo);
command.setFfmpegPath(bin);
command.setFfprobePath(bin2);
var noaudio = false;
if(document.getElementById('gif').checked)
{
outfile+='.gif';
command.fps(15).size('640x?');
noaudio = true;
}
if(document.getElementById('nosound').checked)
{
command.noAudio();
noaudio = true;
}
if(document.getElementById('fps').checked && !document.getElementById('gif').checked)
command.fps(30);
if(document.getElementById('halfsize').checked && !document.getElementById('gif').checked)
command.size('50%');
if(document.getElementById('nvenc').checked && !document.getElementById('gif').checked)
command.videoCodec('h264_nvenc').videoBitrate('10000k')
var playbackspeed = parseFloat($('input[name="speedchange"]:checked').val());
//if(playbackspeed!=1)
// command.noAudio();
if(noaudio === false)
switch(playbackspeed)
{
case 2: //half
command.audioFilters('atempo=0.5')
break;
case 4: //quater
command.audioFilters('atempo=0.5,atempo=0.5')
break;
case 0.5: //double
command.audioFilters('atempo=2')
break;
case 0.25: //quatripple
command.audioFilters('atempo=2,atempo=2')
break;
}
//var command = bin+" -y -i \""+currentvideo+"\" -ss "+ starttime + " -t "+ (duration*playbackspeed) + commandattachments;
//command+=" "+'"'+outfile+'"';
command.output(outfile).seek(starttime*playbackspeed).duration(duration*playbackspeed)
.videoFilters('setpts='+playbackspeed+'*PTS')
.on('end', function() {
console.log('Finished processing');
const {shell} = require('electron')
shell.showItemInFolder(outfile);
if(document.getElementById('pictshare').checked)
uploadFileToPictshare(outfile);
else
endLoading();
}).on('progress', function(progress) {
setProgress(progress.percent);
console.log('Processing: ' + progress.percent + '% done');
})
.run();
//console.log(command);
/*
cmd.get(
command,
function(data){
console.log('finito',data);
if(document.getElementById('pictshare').checked)
uploadFileToPictshare(outfile);
else
endLoading();
const {shell} = require('electron')
shell.showItemInFolder(outfile);
}
);
*/
}
function setProgress(value) {
var elem = document.getElementById("progressbar");
var width = 1;
elem.style.width = Math.round(value) + '%';
elem.innerHTML = Math.round(value) + '%';
}
function startLoading()
{
$('#myModal').modal('show');
$("#loading").html(getSpinner(true));
}
function endLoading()
{
$('#myModal').modal('hide');
$("#loading").html("");
}
function setStarttime()
{
$("#starttime").val($("#current").text());
}
function setEndtime()
{
var starttime = parseFloat($("#starttime").val());
var current = parseFloat($("#current").text());
$("#endtime").val(current-starttime);
}
function saveScreenshot()
{
var fs = require('fs');
console.log("starting the cutting");
startLoading()
var cmd=require('node-cmd');
var bin = 'resources\\ffmpeg\\bin\\ffmpeg.exe';
var bin2 = 'resources\\ffmpeg\\bin\\ffprobe.exe';
if (!fs.existsSync(bin)) {
var bin = 'ffmpeg\\bin\\ffmpeg.exe';
var bin2 = 'ffmpeg\\bin\\ffprobe.exe';
}
var path = require('path').dirname(currentvideo)+"\\cut";
if (!fs.existsSync(path)){
fs.mkdirSync(path);
}
var frametime = parseFloat($("#current").text());
var outfile = path+'\\'+ $("#newname").val().split('.').slice(0, -1).join('.')+'-'+$("#current").text()+'.jpg' ;
var ffmpeg = require('fluent-ffmpeg');
var command = ffmpeg(currentvideo);
command.setFfmpegPath(bin);
command.setFfprobePath(bin2);
command.output(outfile).seek(frametime).frames(1)
.on('end', function() {
console.log('Finished processing');
const {shell} = require('electron')
shell.showItemInFolder(outfile);
endLoading();
}).on('progress', function(progress) {
setProgress(progress.percent);
console.log('Processing: ' + progress.percent + '% done');
})
.run();
}
function uploadFileToPictshare(file)
{
$("#loadingtext").html('Uploading to PictShare');
console.log("uploading to pictshare");
var req = require('request').post('https://pictshare.net/backend.php', function (err, resp, body) {
if (err) {
console.log('Error!');
} else {
console.log('URL: ' + body);
var o = JSON.parse(body);
if(o.hash===undefined || !o.hash){
endLoading()
return false;
}
var url = 'https://pictshare.net/'+o.hash;
const {shell} = require('electron')
shell.openExternal(url);
endLoading()
}
});
var form = req.form();
form.append('postimage', require('fs').createReadStream(file), {
filename: 'postimage',
contentType: 'text/plain'
});
}