forked from mhlzr/html5-videoEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
384 lines (304 loc) · 12.1 KB
/
index.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
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(3000),
events = require('events'),
fs = require('fs'),
manager = require('./modules/manager').createManager(),
encoder = require('./modules/encoder'),
avisynth = require('./modules/avisynth'),
metadata = require('./modules/metadata');
/**
* EXPRESS CONFIGURATION
*/
app.use(express.bodyParser());
app.configure(function () {
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions : true, showStack : true }));
});
/**
* SOCKET.IO CONFIGURATION
*/
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
io.set('browser client', false); //does Socket.IO need to serve the static resources
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
/**
* OPEN PROJECT VIA PROJECT-ID
*/
app.get(new RegExp(/^\/([0-9a-fA-F]{24}$)/), function (req, res) {
manager.projects.isProjectExistent(req.params[0], function onResponse(exists) {
"use strict";
if (exists) res.redirect('/#' + req.params[0]);
else res.redirect('/');
});
});
/**
* OPEN COMPOSITION VIA PUBLIC-ID
*/
app.get(new RegExp(/^\/preview\/([0-9a-fA-F]{32}$)/), function (req, res) {
manager.compositions.isPublicCompositionExistent(req.params[0], function onResponse(exists) {
"use strict";
if (exists) res.redirect('/preview.html#' + req.params[0]);
else res.redirect('/');
});
});
/**
* ROUTE FOR DOWNLOADING VIDEO
*/
app.get(new RegExp(/^\/projects\/.*\/compositions\/\S*|\s*/), function (req, res) {
//there are currently only mp4s to download
var filePath = __dirname + '/public/' + req.url + '.mp4';
fs.exists(filePath, function (exists) {
if (exists) {
res.download(filePath);
} else {
res.writeHead(404);
res.end('Done!');
}
});
});
/**
* CLEAR ALL DATA
*/
app.get('/reset', function onReset(req, res) {
manager.clean(function onComplete(err) {
res.writeHead(200);
res.end('Done!');
});
});
/**
* Everything else is a 404
*/
app.get('*', function (req, res) {
res.writeHead(404);
res.end();
});
/**
* SOCKET.IO EVENTS
*/
io.sockets.on('connection', function (socket) {
/*
PROJECT CRUD
*/
socket.on('project:create', manager.projects.create);
socket.on('project:read', manager.projects.read);
socket.on('project:update', manager.projects.update);
socket.on('project:delete', manager.projects.remove);
/*
ASSET CRUD
*/
socket.on('asset:create', manager.assets.create);
socket.on('asset:read', manager.assets.read);
socket.on('asset:update', manager.assets.update);
socket.on('asset:delete', manager.assets.remove);
/*
File CRUD
*/
socket.on('file:create', manager.files.create);
socket.on('file:read', manager.files.read);
socket.on('file:update', manager.files.update);
socket.on('file:delete', function onRequest(data, callback) {
'use strict';
//the file gets unlinked first
manager.removePhysicalFile(data, function onUnlinked() {
//even if something fails, we still remove if from the db
manager.files.remove(data, callback);
});
});
/*
COMPOSITION CRUD
*/
socket.on('composition:create', manager.compositions.create);
socket.on('composition:read', manager.compositions.read);
socket.on('composition:update', manager.compositions.update);
socket.on('composition:delete', manager.compositions.remove);
/*
SEQUENCE CRUD
*/
socket.on('sequence:create', manager.sequences.create);
socket.on('sequence:read', manager.sequences.read);
socket.on('sequence:update', manager.sequences.update);
socket.on('sequence:delete', manager.sequences.remove);
/*
COLLECTIONS FETCH
*/
socket.on('library:read', manager.assets.getLibraryByProjectId);
socket.on('compositions:read', manager.compositions.getCompositionsByProjectId);
socket.on('files:read', manager.files.getFilesByAssetId);
socket.on('sequences:read', manager.sequences.getSequencesByCompositionId);
/*
UPLOADER
*/
socket.on('upload', function (data) {
if (!data.projectId || !data.id) {
throw new Error('Missing IDs');
}
//will be removed during update-process
var fileId = data.id;
manager.projects.getProjectPathByProjectId(data.projectId, function onPathFound(projectPath) {
if (!projectPath) throw new Error('No folder existent for Project');
//accept bytes an append to file
manager.acceptFilePartial(data, projectPath, function onDataAccepted(res) {
//inform client about progress
socket.emit('file/' + fileId + ':update', {
byteOffset : res.byteOffset,
isComplete : res.isComplete
});
//save to db
manager.files.update(res, function onUpdated(err) {
if (err) throw err;
});
//read metaData if file is complete (more accurate than clients meta)
if (res.isComplete) {
var filePath = manager.getAbsoluteFilePath(projectPath, data.fileName);
//get the asset from the db
manager.assets.getAssetByFileId(fileId, function onReceived(asset) {
//read metadata
metadata.getMetaData(asset.type, filePath, function onMetaDataRead(info) {
//inform client
socket.emit('asset/' + asset._id + ':update', info);
});
});
}
});
});
});
/*
TRANSCODE REQUEST
*/
socket.on('transcode', function (data) {
"use strict";
if (!data.projectId || !data.fileId || !data.originalFileId || !data.format) {
throw new Error('Missing parameters for transcoding!');
}
var settings = {};
settings.fileId = data.fileId;
settings.projectId = data.projectId;
settings.format = data.format;
settings.fileName = null; //will be read from db
settings.originalFileName = null;//will be read from db
//get original fileObject
manager.files.read({id : data.originalFileId}, function onRead(err, res) {
"use strict";
if (!res) return;
settings.originalFileName = res.remoteFileName + '.' + res.ext;
//get new fileObject
manager.files.read({id : data.fileId}, function onRead(err, res) {
"use strict";
if (!res) return;
settings.fileName = res.remoteFileName + '.' + res.ext;
//get projectPath
manager.projects.getProjectPathByProjectId(data.projectId, function onPathFound(projectPath) {
"use strict";
if (!projectPath) throw new Error('No assetFolder existent for Project');
settings.path = manager.getAbsoluteFilePath(projectPath);
encoder.addTranscodingJob(settings);
encoder.start();
});
});
});
});
/*
ENCODE REQUEST
*/
socket.on('encode', function (data) {
"use strict";
if (!data.projectId || !data.compositionId || !data.format) {
throw new Error('Missing parameters for encoding!');
}
var settings = {};
settings.seqs = [];
//get composition by id
manager.compositions.read({_id : data.compositionId }, function onFound(err, docs) {
settings.width = docs.width | 0;
settings.height = docs.height | 0;
settings.fps = docs.fps;
settings.duration = docs.duration;
settings.fileName = docs.name + '.' + data.format.ext;
settings.projectId = data.projectId;
settings.compositionId = data.compositionId;
//get all sequences that are used in the composition
manager.sequences.getSequencesByCompositionId({id : data.compositionId}, function onFound(err, seqs) {
//check if any sequences belong to composition
if (seqs.length < 1) return;
function addFileToSettings(i) {
if (i >= seqs.length) {
//complete
createAVSFile();
return;
}
var seq = seqs[i];
//get original files related to sequence by assetid
manager.files.getFilesByAssetId({id : seq.assetId}, function onFound(err, files) {
for (var j = 0; j < files.length; j++) {
if (files[j].isOriginal) {
//store filepath
seq.fileName = files[j].remoteFileName + '.' + files[j].ext;
settings.seqs.push(seq);
addFileToSettings(i + 1);
}
}
});
}
addFileToSettings(0);
function createAVSFile() {
//create AVS-script
var avsString = avisynth.createAVSFromComposition(settings);
//store to disk
manager.projects.getProjectPathByProjectId(data.projectId, function onFound(projectFolder) {
var path = manager.getAbsoluteFilePath(projectFolder, data.compositionId + '.avs', 'compositions');
//create avisynth-file
fs.writeFile(path, avsString, function (err) {
//send to encoder
settings.avsPath = path;
encoder.addEncodingJob(settings);
encoder.start();
});
});
}
});
});
});
/*
ENCODER EVENTS
*/
encoder.on('transcoding:progress', function onTranscodingProgress(event) {
socket.emit('file/' + event.fileId + ':update', {
encodingProgress : event.encodingProgress,
isComplete : event.isComplete
});
if (event.isComplete) {
//TODO get filesize from disk
//save to db
manager.files.update({id : event.fileId, isComplete : true}, function onUpdated(err) {
if (err) throw err;
});
}
});
encoder.on('encoding:progress', function onEncodingProgress(event) {
socket.emit('composition/' + event.compositionId + ':update', {
encodingProgress : event.encodingProgress,
isComplete : event.isComplete
});
if (event.isComplete) {
//TODO get filesize from disk
//save to db
//TODO file reference
/*
manager.compositions.update({id : event.compositionId, status : 'Encoded', files : [] }, function onUpdated(err) {
if (err) throw err;
});
*/
}
});
})
;
app.listen(80);