-
Notifications
You must be signed in to change notification settings - Fork 12
/
video-splitter.js
102 lines (95 loc) · 3.07 KB
/
video-splitter.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
var spawn = require('child_process').spawn
spawn('open', ['http://127.0.0.1:1337/']);
var http = require('http');
var path = require("path");
var fs = require("fs");
var exec = require('child_process').exec;
http.createServer(function (req, res) {
if (req.method === 'OPTIONS') {
console.log('!OPTIONS');
var headers = {};
headers["Access-Control-Allow-Origin"] = "*";
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
headers["Access-Control-Allow-Credentials"] = false;
headers["Access-Control-Max-Age"] = '86400';
headers["Access-Control-Allow-Headers"] = "X-reqed-With, X-HTTP-Method-Override, Content-Type, Accept";
res.writeHead(200, headers);
res.end();
}
else if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var data = JSON.parse(body);
var localPath = __dirname;
var inputFilePath = localPath+"/videos/"+data.inputFilePath;
var outputFilePath = localPath+"/videos/output-"+data.inputFilePath
var start = data.begin;
var end = data.end;
var command = "ffmpeg -y -ss "+start+" -t "+(end-start)+" -i "+inputFilePath+" -vcodec copy -acodec copy "+outputFilePath;
exec(command, function(error, stdout, stderr) {
var msg = ""
if(error) {
console.log(error);
msg = error.toString();
res.writeHead(500, {'Content-Type': 'text/plain'});
}
else {
console.log(stdout);
res.writeHead(200, {'Content-Type': 'text/plain'});
}
res.end(msg);
});
});
}
else if (req.method == 'GET') {
var filename = "index.html";
if(req.url != "/") {
filename = req.url
}
var ext = path.extname(filename);
var localPath = __dirname;
var validExtensions = {
".html" : "text/html",
".js": "application/javascript",
".css": "text/css",
".txt": "text/plain",
".jpg": "image/jpeg",
".gif": "image/gif",
".png": "image/png",
".ico": "image/x-icon"
};
var mimeType = validExtensions[ext];
if (mimeType) {
localPath += "/interface/"+filename;
fs.exists(localPath, function(exists) {
if(exists) {
console.log("Serving file: " + localPath);
getFile(localPath, res, mimeType);
} else {
console.log("File not found: " + localPath);
res.writeHead(404);
res.end();
}
});
} else {
console.log("Invalid file extension detected: " + ext)
}
}
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
function getFile(localPath, res, mimeType) {
fs.readFile(localPath, function(err, contents) {
if(!err) {
res.setHeader("Content-Length", contents.length);
res.setHeader("Content-Type", mimeType);
res.statusCode = 200;
res.end(contents);
} else {
res.writeHead(500);
res.end();
}
});
}