-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.js
90 lines (70 loc) · 2.34 KB
/
web.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
// Generated by CoffeeScript 1.8.0
(function() {
var app, convert_and_send, express, ffmpeg, fs, http, mkdirp, path, qs, server, writeToFile, ytdl;
express = require('express');
ffmpeg = require('fluent-ffmpeg');
http = require('http');
ytdl = require('ytdl');
path = require('path');
fs = require('fs');
qs = require('querystring');
mkdirp = require('mkdirp');
app = express();
server = http.createServer(app);
app.set('port', process.env.PORT || 3000);
app.set('views', "" + __dirname + "/views");
app.set('view engine', 'jade');
app.use(express["static"](__dirname + "/public"));
server.listen(app.get('port'), function() {
return console.log("Express server listening on port " + (app.get('port')));
});
app.get('/', function(req, res) {
console.log("rendering");
return res.render('index');
});
app.get('/convert', function(req, res) {
var url;
res.contentType('mp3');
url = req.query.url;
console.log(url);
return ytdl.getInfo(url, function(err, info) {
var pathToMovie, size;
if (err != null) {
console.log(err.message);
return;
}
pathToMovie = path.join(__dirname, 'public', 'tmp', info.video_id);
console.log(pathToMovie);
console.log("downloading and converting");
size = 0;
return mkdirp(pathToMovie, function(err) {
var download, file;
file = fs.createWriteStream(pathToMovie + "/video.mp4");
download = ytdl(url);
download.pipe(file, {
end: false
});
download.on('data', function(chunk) {
size += chunk.length;
return console.log(size);
});
return download.on('end', function() {
console.log("Download done");
return convert_and_send(pathToMovie, res, info.video_id);
});
});
});
});
convert_and_send = function(pathToMovie, res, videoId) {
console.log("converting");
return new ffmpeg(pathToMovie + "/video.mp4").setFfmpegPath(__dirname + "/ffmpeg").audioCodec('libmp3lame').format('mp3').on('end', function() {
console.log("end");
return res.status(200).send("/tmp/" + videoId + "/music.mp3");
}).saveToFile(pathToMovie + "/music.mp3", {
end: true
});
};
writeToFile = function(name) {
return fs.appendFile('log.txt', name);
};
}).call(this);