livestream.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var child_process = require("child_process");
  2. var http = require('http');
  3. var url = require('url');
  4. var liveStream = function (req, resp) { // handle each client request by instantiating a new FFMPEG instance
  5. resp.writeHead(200, {
  6. "Connection": "keep-alive"
  7. , "Content-Type": "audio/ogg"
  8. , "Accept-Ranges": "bytes" // Helps Chrome
  9. });
  10. var song = "/storage/samba/Music/Chvrches - Every Open Eye/02 Leave A Trace.mp3";
  11. var query = url.parse(req.url, true).query;
  12. if ("song" in query)
  13. song=query.song;
  14. transcoder = child_process.spawn("/usr/bin/ffmpeg", [ "-i",song, "-c:a", "libvorbis", "-vn", "-b:a", "192k", "-f", "ogg", "-y", "-" ], {detached: false});
  15. transcoder.stdout.pipe(resp);
  16. transcoder.stdout.on("data",function(data) { });
  17. transcoder.stderr.on("data", function (data) {
  18. console.log(" -> " + data);
  19. });
  20. transcoder.on("exit", function (code) {
  21. console.log( " live FFMPEG terminated with code " + code);
  22. });
  23. transcoder.on("error", function (e) {
  24. console.log(" live FFMPEG system error: " + e);
  25. });
  26. req.on("close", function () {
  27. shutStream("closed")
  28. })
  29. req.on("end", function () {
  30. shutStream("ended")
  31. });
  32. function shutStream(event) {
  33. console.log("Live streaming connection to client has " + event)
  34. if (typeof transcoder !== "undefined") {
  35. transcoder.kill();
  36. }
  37. }
  38. return true
  39. }
  40. http.createServer(liveStream).listen(1337, "0.0.0.0");
  41. console.log("runnen");