peertube-watch.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { registerTSPaths } from '../helpers/register-ts-paths'
  2. registerTSPaths()
  3. import * as program from 'commander'
  4. import { join } from 'path'
  5. import { execSync } from 'child_process'
  6. program
  7. .name('watch')
  8. .arguments('<url>')
  9. .option('-g, --gui <player>', 'player type', /^(airplay|stdout|chromecast|mpv|vlc|mplayer|xbmc)$/i, 'vlc')
  10. .option('-r, --resolution <res>', 'video resolution', '480')
  11. .on('--help', function () {
  12. console.log(' Available Players:')
  13. console.log()
  14. console.log(' - mpv')
  15. console.log(' - mplayer')
  16. console.log(' - vlc')
  17. console.log(' - stdout')
  18. console.log(' - xbmc')
  19. console.log(' - airplay')
  20. console.log(' - chromecast')
  21. console.log()
  22. console.log()
  23. console.log(' Examples:')
  24. console.log()
  25. console.log(' $ peertube watch -g mpv https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10')
  26. console.log(' $ peertube watch --gui stdout https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10')
  27. console.log(' $ peertube watch https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10')
  28. console.log()
  29. })
  30. .action((url, cmd) => run(url, cmd))
  31. .parse(process.argv)
  32. function run (url: string, program: any) {
  33. if (!url) {
  34. console.error('<url> positional argument is required.')
  35. process.exit(-1)
  36. }
  37. const cmd = 'node ' + join(__dirname, 'node_modules', 'webtorrent-hybrid', 'bin', 'cmd.js')
  38. const args = ` --${program.gui} ` +
  39. url.replace('videos/watch', 'download/torrents') +
  40. `-${program.resolution}.torrent`
  41. try {
  42. execSync(cmd + args)
  43. } catch (err) {
  44. console.error('Cannto exec command.', err)
  45. process.exit(-1)
  46. }
  47. }