-
Notifications
You must be signed in to change notification settings - Fork 5
/
web.coffee
64 lines (53 loc) · 1.83 KB
/
web.coffee
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
express = require 'express'
ffmpeg = require 'fluent-ffmpeg'
http = require 'http'
ytdl = require 'ytdl'
path = require 'path'
fs = require 'fs'
qs = require 'querystring'
app = express()
server = http.createServer(app)
app.configure ->
app.set 'port', process.env.PORT || 3000
app.set 'views', "#{__dirname}/views"
app.set 'view engine', 'jade'
server.listen app.get('port'), ->
console.log "Express server listening on port #{app.get 'port'}"
app.get '/', (req, res) -> res.render 'index'
app.get '/play', (req, res) ->
ytdl.getInfo req.query.url, (err, info) ->
if err?
success = false
else
console.log info
success = true
title = info.title
thumb = info.thumbnail_url
res.render 'play', yt_url: req.query.url, title: title, thumb: thumb, success: success
app.get '/convert', (req, res) ->
console.log "processing #{req.query.url}"
res.contentType('mp3')
dest = path.join( __dirname, 'tmp', req.query.url )
ytdl.getInfo req.query.url, (err, info) ->
if err?
console.log err.message
return
pathToMovie = path.join( __dirname, 'tmp', info.video_id )
if fs.existsSync("#{pathToMovie}.mp3")
console.log "already downloaded"
res.sendfile("#{pathToMovie}.mp3")
else if fs.existsSync pathToMovie
convert_and_send(pathToMovie, res)
else
console.log "downloading and converting"
file = fs.createWriteStream(pathToMovie)
ytdl(req.query.url).pipe(file)
file.on 'close', -> convert_and_send(pathToMovie, res)
convert_and_send = (pathToMovie, res) ->
console.log "converting"
new ffmpeg({ source: pathToMovie, nolog: true })
.withAudioCodec('libmp3lame')
.toFormat('mp3')
.saveToFile "#{pathToMovie}.mp3", (retcode, error) ->
unless err?
res.sendfile("#{pathToMovie}.mp3")