video.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * video.js - video element for blessed
  3. * Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
  4. * https://github.com/chjj/blessed
  5. */
  6. /**
  7. * Modules
  8. */
  9. var cp = require('child_process');
  10. var Node = require('./node');
  11. var Box = require('./box');
  12. var Terminal = require('./terminal');
  13. /**
  14. * Video
  15. */
  16. function Video(options) {
  17. var self = this
  18. , shell
  19. , args;
  20. if (!(this instanceof Node)) {
  21. return new Video(options);
  22. }
  23. options = options || {};
  24. Box.call(this, options);
  25. if (this.exists('mplayer')) {
  26. shell = 'mplayer';
  27. args = ['-vo', 'caca', '-quiet', options.file];
  28. } else if (this.exists('mpv')) {
  29. shell = 'mpv';
  30. args = ['--vo', 'caca', '--really-quiet', options.file];
  31. } else {
  32. this.parseTags = true;
  33. this.setContent('{red-fg}{bold}Error:{/bold}'
  34. + ' mplayer or mpv not installed.{/red-fg}');
  35. return this;
  36. }
  37. var opts = {
  38. parent: this,
  39. left: 0,
  40. top: 0,
  41. width: this.width - this.iwidth,
  42. height: this.height - this.iheight,
  43. shell: shell,
  44. args: args.slice()
  45. };
  46. this.now = Date.now() / 1000 | 0;
  47. this.start = opts.start || 0;
  48. if (this.start) {
  49. if (shell === 'mplayer') {
  50. opts.args.unshift('-ss', this.start + '');
  51. } else if (shell === 'mpv') {
  52. opts.args.unshift('--start', this.start + '');
  53. }
  54. }
  55. var DISPLAY = process.env.DISPLAY;
  56. delete process.env.DISPLAY;
  57. this.tty = new Terminal(opts);
  58. process.env.DISPLAY = DISPLAY;
  59. this.on('click', function() {
  60. self.tty.pty.write('p');
  61. });
  62. // mplayer/mpv cannot resize itself in the terminal, so we have
  63. // to restart it at the correct start time.
  64. this.on('resize', function() {
  65. self.tty.destroy();
  66. var opts = {
  67. parent: self,
  68. left: 0,
  69. top: 0,
  70. width: self.width - self.iwidth,
  71. height: self.height - self.iheight,
  72. shell: shell,
  73. args: args.slice()
  74. };
  75. var watched = (Date.now() / 1000 | 0) - self.now;
  76. self.now = Date.now() / 1000 | 0;
  77. self.start += watched;
  78. if (shell === 'mplayer') {
  79. opts.args.unshift('-ss', self.start + '');
  80. } else if (shell === 'mpv') {
  81. opts.args.unshift('--start', self.start + '');
  82. }
  83. var DISPLAY = process.env.DISPLAY;
  84. delete process.env.DISPLAY;
  85. self.tty = new Terminal(opts);
  86. process.env.DISPLAY = DISPLAY;
  87. self.screen.render();
  88. });
  89. }
  90. Video.prototype.__proto__ = Box.prototype;
  91. Video.prototype.type = 'video';
  92. Video.prototype.exists = function(program) {
  93. try {
  94. return !!+cp.execSync('type '
  95. + program + ' > /dev/null 2> /dev/null'
  96. + ' && echo 1', { encoding: 'utf8' }).trim();
  97. } catch (e) {
  98. return false;
  99. }
  100. };
  101. /**
  102. * Expose
  103. */
  104. module.exports = Video;