peertube-watch.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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) => {
  31. run(url, cmd)
  32. .catch(err => {
  33. console.error(err)
  34. process.exit(-1)
  35. })
  36. })
  37. .parse(process.argv)
  38. async function run (url: string, program: any) {
  39. if (!url) {
  40. console.error('<url> positional argument is required.')
  41. process.exit(-1)
  42. }
  43. const cmd = 'node ' + join(__dirname, 'node_modules', 'webtorrent-hybrid', 'bin', 'cmd.js')
  44. const args = ` --${program.gui} ` +
  45. url.replace('videos/watch', 'download/torrents') +
  46. `-${program.resolution}.torrent`
  47. execSync(cmd + args)
  48. }