/
server.js
292 lines (204 loc) · 7.35 KB
/
server.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
var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var path = require('path');
var port = 6000;
var app = {};
var child_process = require('child_process').exec;
var ffmpeg = require('fluent-ffmpeg');
var fs = require('fs');
var dotenv = require('dotenv').load();
var mongoose = require('mongoose');
var Q = require('q');
var e = app.e = express();
app.server = app.server = http.createServer(e);
e.use('/static', express.static(__dirname + '/public'));
e.use(bodyParser.json()); //HELP this doesn't allow post to run
e.use(bodyParser.urlencoded({ extended: true })); //HELP what is extension
var jsonParser = bodyParser.json();
var urlencodedParser = bodyParser.urlencoded({ extended: true });
// set up mongodb mongoose connection & models
mongoose.connect(process.env.MONGO_URI);
var Video = require('./models/video');
app.server.listen(port, function() {
console.log('Listening on port ' + port + '\n');
});
//-----------------------------------CONCATENATE THAT VIDEO SHIT--------------------------------------------//
//testing purposes
//var vidarr=['../videoslicer/"dog slices"/1368547613.mp4','../videoslicer/"dog slices"/1814283056.mp4','../videoslicer/"dog slices"/1386206500.mp4','../videoslicer/"dog slices"/1706709066.mp4','../videoslicer/"dog slices"/1726081069.mp4','../videoslicer/"dog slices"/5044944993.mp4','../videoslicer/"dog slices"/4859706534.mp4']
//receive(vidarr);
//----------------
//create array of random integers for unique temporary file names
function receive(videos, res) {
var temparr = [];
child_process('mkdir tempVids', function (err, data) {
// console.log('tempVids made');
})
for(var i = 0; i < videos.length; i++) {
temparr[i] = Math.floor(Math.random() * (9999999999 - 1000000000 +1)) + 1000000000
if (i === videos.length-1) {
concat(videos, temparr, res);
}
}
}
// convert videos to .mpg
function concat(videos, temparr, res) {
console.log(videos.length-1);
for(var i = 0; i < videos.length; i++) {
(function(index){
child_process('ffmpeg -i ' + process.env.PATH_TO_DIR + process.env.PATH_TO_VIDEO_CLIPS + videos[index] + ' -qscale:v 1 ./tempVids/' + temparr[index] + '.mpg', function (err, data) {
//console.log(video[i]);
if (index === videos.length-1) {
//console.log('call merge');
merge(videos, temparr, res);
}
});
})(i);
}
}
// merge .mpg's into one .mpg, but this function just creates the string to execute
function merge(videos, temparr, res) {
// console.log('merge');
var maxTen = [];
if (temparr.length > 10){
var max = 10;
for(var i = 0; i < max; i++){
var rand = Math.floor(Math.random() * temparr.length);
var current = temparr[rand];
if(maxTen.indexOf(current) == -1){
maxTen.push(current);
} else {
i--;
}
}
} else {
maxTen = temparr;
}
console.log(maxTen);
var string = "cat ";
for (var i = 0; i < maxTen.length; i++){
string += './tempVids/' + maxTen[i] + ".mpg ";
}
execute(string, res);
}
function execute(string, res) {
string += "> intermediate_all.mpg";
console.log(string);
child_process(string, function (err, data) {
// console.log('call finish');
finish(res);
});
}
function finish(res) {
var randomPath = (Math.floor(Math.random() * (9999999999 - 1000000000 +1)) + 1000000000) + '.mp4';
var filepath = process.env.PATH_TO_DIR + process.env.PATH_TO_MERGED_CLIPS + randomPath;
child_process('ffmpeg -i intermediate_all.mpg -qscale:v 2 ' + filepath, function (err, data) {
if(err)
console.log(err);
thumbnail(filepath, function(thumbnailPath){
res.send({video: 'http://' + process.env.IP_ADDR + process.env.PATH_TO_MERGED_CLIPS + randomPath, image: 'http://' + process.env.IP_ADDR + process.env.PATH_TO_MERGED_CLIPS + thumbnailPath });
});
});
deleteTemps();
}
function deleteTemps() {
child_process('rm -r ./tempVids', function(err){
if (err)
console.log(err);
});
}
// function deleteTemps() {
// fs.unlink('./tempVids/', function(err){
// console.log(err);
// console.log('deleted?')
// });
// }
//------------------------------------STOP CONCATENATING BECAUSE YOU FUCKING DID IT-----------------------------------//
//------------------------------------THUMBNAIL, YO-------------------------------------------------------------------//
// var proc = ffmpeg('output.mp4')
// .takeScreenshots({
// count: 1,
// timemarks: [ '2' ]
// }, '/', function(err) {
// console.log('screenshots were saved');
// });
function thumbnail(finalVid, callback) {
var randomName = (Math.floor(Math.random() * (9999999999 - 1000000000 +1)) + 1000000000) + '.png';
//console.log('THUMBNAIL URL:' + 'ffmpeg -i '+ finalVid +' -ss 03 -vframes 1 ' + process.env.PATH_TO_DIR + process.env.PATH_TO_MERGED_CLIPS + randomName);
child_process('ffmpeg -i '+ finalVid +' -ss 03 -vframes 1 ' + process.env.PATH_TO_DIR + process.env.PATH_TO_MERGED_CLIPS + randomName, function (err) {
if (err)
console.log(err);
callback(randomName);
});
}
//------------------------------------THUMBNAIL, YO-------------------------------------------------------------------//
e.get('/api/dreamText', jsonParser, function(req, res) {
// req.query.dream
//function dreamTextTest(wordd){
//console.log('foo');
//console.log(req.query.dream);
//var res;
console.log(req.query.dream);
var noPunctuation = req.query.dream.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
var words = noPunctuation.split(" ");
console.log(words);
var fileList = [];
// loop through all words in the dream text paragraph
// jesus
// create an array of functions to which q.all waits til all of the
// arrays have been returned and then it returns those arrays in an array to the .then
Q.all(words.map(function(word){
var deferred = Q.defer();
findByTag(word).then(function(fileArr){
deferred.resolve(fileArr);
});
return deferred.promise
})).then(function(arrays){
// loop through and merge filenames
for(var i = 0; i < arrays.length; i++){
fileList = mergeWithoutDuplicates([fileList, arrays[i]]);
}
console.log(fileList);
receive(fileList, res);
});
// get dream text
// go through each word and query mongo
// get list of file names that are relevant
// pass to get video concatenated
// save that video in the root directory
// pass url through response
});
// merge arrays without adding duplicates
function mergeWithoutDuplicates(arguments){
var finalArr = [];
for(var i = 0; i < arguments.length; i++){
var current = arguments[i];
for(var j = 0; j < current.length; j++){
// if it's not found, then push to array
if(finalArr.indexOf(current[j]) === -1){
finalArr.push(current[j]);
}
}
}
return finalArr;
}
// return array of relevant video filenames
function findByTag(word){
var deferred = Q.defer();
Video.find({ tags: word }).exec(function(err, videos) {
if (err) throw err;
//console.log('--------- begining of searching ---------')
// log relevant videos
//console.log(videos);
//console.log('--------- end of searching ---------')
var fileArr = [];
for(var i = 0; i < videos.length; i++){
fileArr.push(videos[i].filename);
console.log(videos[i].filename);
}
deferred.resolve(fileArr);
});
return deferred.promise;
}
var testString = 'Running through the snow with family and friends. Outside with the dog, indoors with technology';
//dreamTextTest(testString);