livestream.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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('/artists', function (req, resp) {
  14. function f(a){
  15. resp.send(JSON.stringify(a));
  16. }
  17. tagger.getArtists(f);
  18. });
  19. app.get('/albums', function (req, resp) {
  20. function f(a){
  21. resp.send(JSON.stringify(a));
  22. }
  23. tagger.getAlbums(f);
  24. });
  25. app.get('/album/:album', function (req, resp) {
  26. var album = req.params.album;
  27. function f(a){
  28. resp.send(JSON.stringify(a));
  29. }
  30. tagger.getAlbum(album,f);
  31. });
  32. app.get('/song/:song', function (req, resp) {
  33. resp.writeHead(200, {
  34. "Connection": "keep-alive"
  35. , "Content-Type": "audio/ogg"
  36. , "Accept-Ranges": "bytes" // Helps Chrome
  37. });
  38. var song = req.params.song;
  39. if (isNumeric(song)){
  40. tagger.pathFromID(song, transcode);
  41. }else{
  42. transcode(song);
  43. }
  44. function transcode(path){
  45. console.log("Transcoding " + path);
  46. transcoder = child_process.spawn("/usr/bin/ffmpeg", [ "-i",path, "-c:a", "libvorbis", "-vn", "-b:a", "192k", "-f", "ogg", "-y", "-" ], {detached: false});
  47. transcoder.stdout.pipe(resp);
  48. transcoder.stdout.on("data",function(data) { });
  49. req.on("close", function () { shutStream("closed") })
  50. req.on("end", function () { shutStream("ended") });
  51. function shutStream(event) {
  52. if (typeof transcoder !== "undefined")
  53. transcoder.kill();
  54. }
  55. }
  56. return true;
  57. });
  58. var server = app.listen(1337, function () {
  59. console.log("listenin");
  60. });
  61. function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); }