livestream.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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('/album/artist/:artist', function (req, resp) {
  33. var artist = req.params.artist;
  34. function f(a){
  35. resp.send(JSON.stringify(a));
  36. }
  37. tagger.getAlbumByArtist(artist,f);
  38. });
  39. app.get('/song/:song', function (req, resp) {
  40. resp.writeHead(200, {
  41. "Connection": "keep-alive"
  42. , "Content-Type": "audio/ogg"
  43. , "Accept-Ranges": "bytes" // Helps Chrome
  44. });
  45. var song = req.params.song;
  46. if (isNumeric(song)){
  47. tagger.pathFromID(song, transcode);
  48. }else{
  49. transcode(song);
  50. }
  51. function transcode(path){
  52. console.log("Transcoding " + path);
  53. transcoder = child_process.spawn("/usr/bin/ffmpeg", [ "-i",path, "-c:a", "libvorbis", "-vn", "-b:a", "192k", "-f", "ogg", "-y", "-" ], {detached: false});
  54. transcoder.stdout.pipe(resp);
  55. transcoder.stdout.on("data",function(data) { });
  56. req.on("close", function () { shutStream("closed") })
  57. req.on("end", function () { shutStream("ended") });
  58. function shutStream(event) {
  59. if (typeof transcoder !== "undefined")
  60. transcoder.kill();
  61. }
  62. }
  63. return true;
  64. });
  65. var server = app.listen(1337, function () {
  66. console.log("listenin");
  67. });
  68. function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); }