livestream.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var child_process = require("child_process");
  2. var express = require('express');
  3. var url = require('url');
  4. var tagger = require('./tagger');
  5. var app = express();
  6. app.get('/artist/:artist', function (req, resp) {
  7. var artist = req.params.artist;
  8. function f(a){
  9. resp.send(JSON.stringify(a));
  10. }
  11. tagger.getArtist(artist,f);
  12. });
  13. app.get('/song/:song', function (req, resp) {
  14. resp.writeHead(200, {
  15. "Connection": "keep-alive"
  16. , "Content-Type": "audio/ogg"
  17. , "Accept-Ranges": "bytes" // Helps Chrome
  18. });
  19. var song = req.params.song;
  20. if (isNumeric(song)){
  21. tagger.pathFromID(song, transcode);
  22. }else{
  23. transcode(song);
  24. }
  25. function transcode(path){
  26. console.log("Transcoding " + path);
  27. transcoder = child_process.spawn("/usr/bin/ffmpeg", [ "-i",path, "-c:a", "libvorbis", "-vn", "-b:a", "192k", "-f", "ogg", "-y", "-" ], {detached: false});
  28. transcoder.stdout.pipe(resp);
  29. transcoder.stdout.on("data",function(data) { });
  30. req.on("close", function () { shutStream("closed") })
  31. req.on("end", function () { shutStream("ended") });
  32. function shutStream(event) {
  33. if (typeof transcoder !== "undefined")
  34. transcoder.kill();
  35. }
  36. }
  37. return true;
  38. });
  39. var server = app.listen(1337, function () {
  40. });
  41. function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); }