|
@@ -1,53 +1,72 @@
|
|
|
var child_process = require("child_process");
|
|
var child_process = require("child_process");
|
|
|
-var http = require('http');
|
|
|
|
|
|
|
+var express = require('express');
|
|
|
var url = require('url');
|
|
var url = require('url');
|
|
|
|
|
+var tagger = require('./tagger');
|
|
|
|
|
|
|
|
-var liveStream = function (req, resp) { // handle each client request by instantiating a new FFMPEG instance
|
|
|
|
|
|
|
+var app = express();
|
|
|
|
|
+app.get('/artist/:artist', function (req, resp) {
|
|
|
|
|
+ var artist = req.params.artist;
|
|
|
|
|
+ function f(a){
|
|
|
|
|
+ resp.send(JSON.stringify(a));
|
|
|
|
|
+ }
|
|
|
|
|
+ tagger.getArtist(artist,f);
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+app.get('/artists', function (req, resp) {
|
|
|
|
|
+ function f(a){
|
|
|
|
|
+ resp.send(JSON.stringify(a));
|
|
|
|
|
+ }
|
|
|
|
|
+ tagger.getArtists(f);
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+app.get('/albums', function (req, resp) {
|
|
|
|
|
+ function f(a){
|
|
|
|
|
+ resp.send(JSON.stringify(a));
|
|
|
|
|
+ }
|
|
|
|
|
+ tagger.getAlbums(f);
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+app.get('/album/:album', function (req, resp) {
|
|
|
|
|
+ var album = req.params.album;
|
|
|
|
|
+ function f(a){
|
|
|
|
|
+ resp.send(JSON.stringify(a));
|
|
|
|
|
+ }
|
|
|
|
|
+ tagger.getAlbum(album,f);
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+app.get('/song/:song', function (req, resp) {
|
|
|
resp.writeHead(200, {
|
|
resp.writeHead(200, {
|
|
|
"Connection": "keep-alive"
|
|
"Connection": "keep-alive"
|
|
|
, "Content-Type": "audio/ogg"
|
|
, "Content-Type": "audio/ogg"
|
|
|
, "Accept-Ranges": "bytes" // Helps Chrome
|
|
, "Accept-Ranges": "bytes" // Helps Chrome
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- var song = "/storage/samba/Music/Chvrches - Every Open Eye/02 Leave A Trace.mp3";
|
|
|
|
|
-
|
|
|
|
|
- var query = url.parse(req.url, true).query;
|
|
|
|
|
- if ("song" in query)
|
|
|
|
|
- song=query.song;
|
|
|
|
|
|
|
+ var song = req.params.song;
|
|
|
|
|
+ if (isNumeric(song)){
|
|
|
|
|
+ tagger.pathFromID(song, transcode);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ transcode(song);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- transcoder = child_process.spawn("/usr/bin/ffmpeg", [ "-i",song, "-c:a", "libvorbis", "-vn", "-b:a", "192k", "-f", "ogg", "-y", "-" ], {detached: false});
|
|
|
|
|
- transcoder.stdout.pipe(resp);
|
|
|
|
|
|
|
+ function transcode(path){
|
|
|
|
|
+ console.log("Transcoding " + path);
|
|
|
|
|
+ transcoder = child_process.spawn("/usr/bin/ffmpeg", [ "-i",path, "-c:a", "libvorbis", "-vn", "-b:a", "192k", "-f", "ogg", "-y", "-" ], {detached: false});
|
|
|
|
|
+ transcoder.stdout.pipe(resp);
|
|
|
|
|
|
|
|
- transcoder.stdout.on("data",function(data) { });
|
|
|
|
|
|
|
+ transcoder.stdout.on("data",function(data) { });
|
|
|
|
|
+ req.on("close", function () { shutStream("closed") })
|
|
|
|
|
+ req.on("end", function () { shutStream("ended") });
|
|
|
|
|
|
|
|
- transcoder.stderr.on("data", function (data) {
|
|
|
|
|
- console.log(" -> " + data);
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ function shutStream(event) {
|
|
|
|
|
+ if (typeof transcoder !== "undefined")
|
|
|
|
|
+ transcoder.kill();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+});
|
|
|
|
|
|
|
|
- transcoder.on("exit", function (code) {
|
|
|
|
|
- console.log( " live FFMPEG terminated with code " + code);
|
|
|
|
|
- });
|
|
|
|
|
|
|
+var server = app.listen(1337, function () {
|
|
|
|
|
+});
|
|
|
|
|
|
|
|
- transcoder.on("error", function (e) {
|
|
|
|
|
- console.log(" live FFMPEG system error: " + e);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- req.on("close", function () {
|
|
|
|
|
- shutStream("closed")
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- req.on("end", function () {
|
|
|
|
|
- shutStream("ended")
|
|
|
|
|
- });
|
|
|
|
|
|
|
|
|
|
- function shutStream(event) {
|
|
|
|
|
- console.log("Live streaming connection to client has " + event)
|
|
|
|
|
- if (typeof transcoder !== "undefined") {
|
|
|
|
|
- transcoder.kill();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return true
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-http.createServer(liveStream).listen(1337, "0.0.0.0");
|
|
|
|
|
-console.log("runnen");
|
|
|
|
|
|
|
+function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); }
|