stream.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. import childProcess = require('child_process');
  3. import os = require('os');
  4. import path = require('path');
  5. import my_request = require('../my_request');
  6. import log = require('../log');
  7. /**
  8. * Streams the video to disk.
  9. */
  10. export default function(rtmpUrl: string, rtmpInputPath: string, swfUrl: string, filePath: string,
  11. fileExt: string, mode: string, verbose: boolean, done: (err: Error) => void)
  12. {
  13. let cp;
  14. let cmd;
  15. if (mode === 'RTMP')
  16. {
  17. cmd = command('rtmpdump') + ' ' +
  18. '-r "' + rtmpUrl + '" ' +
  19. '-y "' + rtmpInputPath + '" ' +
  20. '-W "' + swfUrl + '" ' +
  21. '-o "' + filePath + fileExt + '"';
  22. }
  23. else if (mode === 'HLS')
  24. {
  25. cmd = command('ffmpeg') + ' ' +
  26. '-user_agent "' + my_request.getUserAgent() + '" ' +
  27. '-y -xerror -discard none ' +
  28. '-i "' + rtmpInputPath + '" ' +
  29. '-c copy -bsf:a aac_adtstoasc ' +
  30. '"' + filePath + '.mp4"';
  31. }
  32. else
  33. {
  34. log.error('No such mode: ' + mode);
  35. }
  36. cp = childProcess.exec(cmd,
  37. {
  38. maxBuffer: Infinity,
  39. }, done);
  40. if (verbose === true)
  41. {
  42. cp.stdin.pipe(process.stdin);
  43. cp.stdout.pipe(process.stdout);
  44. cp.stderr.pipe(process.stderr);
  45. }
  46. }
  47. /**
  48. * Determines the command for the operating system.
  49. */
  50. function command(exe: string): string
  51. {
  52. if (os.platform() !== 'win32')
  53. {
  54. return exe;
  55. }
  56. return '"' + path.join(__dirname, '../../bin/' + exe + '.exe') + '"';
  57. }