Add player mode in watch/embed urls
[oweals/peertube.git] / server / tools / peertube-watch.ts
1 import * as program from 'commander'
2 import * as summon from 'summon-install'
3 import { join } from 'path'
4 import { execSync } from 'child_process'
5 import { root } from '../helpers/core-utils'
6
7 let videoURL
8
9 program
10   .name('watch')
11   .arguments('<url>')
12   .option('-g, --gui <player>', 'player type', /^(airplay|stdout|chromecast|mpv|vlc|mplayer|ascii|xbmc)$/i, 'ascii')
13   .option('-i, --invert', 'invert colors (ascii player only)', true)
14   .option('-r, --resolution <res>', 'video resolution', /^(240|360|720|1080)$/i, '720')
15   .on('--help', function () {
16     console.log('  Available Players:')
17     console.log()
18     console.log('    - ascii')
19     console.log('    - mpv')
20     console.log('    - mplayer')
21     console.log('    - vlc')
22     console.log('    - stdout')
23     console.log('    - xbmc')
24     console.log('    - airplay')
25     console.log('    - chromecast')
26     console.log()
27     console.log('  Note: \'ascii\' is the only option not using WebTorrent and not seeding back the video.')
28     console.log()
29     console.log('  Examples:')
30     console.log()
31     console.log('    $ peertube watch -g mpv https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10')
32     console.log('    $ peertube watch --gui stdout https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10')
33     console.log('    $ peertube watch https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10')
34     console.log()
35   })
36   .action((url) => {
37     videoURL = url
38   })
39   .parse(process.argv)
40
41 if (!videoURL) {
42   console.error('<url> positional argument is required.')
43   process.exit(-1)
44 } else { program['url'] = videoURL }
45
46 handler(program)
47
48 function handler (argv) {
49   if (argv['gui'] === 'ascii') {
50     summon('peerterminal')
51     const peerterminal = summon('peerterminal')
52     peerterminal([ '--link', videoURL, '--invert', argv['invert'] ])
53   } else {
54     summon('webtorrent-hybrid')
55     const CMD = 'node ' + join(root(), 'node_modules', 'webtorrent-hybrid', 'bin', 'cmd.js')
56     const CMDargs = ` --${argv.gui} ` +
57                     argv['url'].replace('videos/watch', 'download/torrents') +
58                     `-${argv.resolution}.torrent`
59     execSync(CMD + CMDargs)
60   }
61 }