livestream.js 2.1 KB

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