peertube-watch.ts 1.6 KB

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