PeerTube/server/tools/peertube-watch.ts

43 lines
1.4 KiB
TypeScript
Raw Normal View History

import { registerTSPaths } from '../helpers/register-ts-paths'
registerTSPaths()
import * as program from 'commander'
import { join } from 'path'
import { execSync } from 'child_process'
program
.name('watch')
.arguments('<url>')
2021-02-03 09:33:05 +01:00
.addOption(
new program.Option('-g, --gui <player>', 'player type')
.default('vlc')
.choices([ 'airplay', 'stdout', 'chromecast', 'mpv', 'vlc', 'mplayer', 'xbmc' ])
)
2019-05-24 17:42:35 +02:00
.option('-r, --resolution <res>', 'video resolution', '480')
2021-02-03 09:33:05 +01:00
.addHelpText('after', '\n\n Examples:\n\n' +
' $ peertube watch -g mpv https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10\n' +
' $ peertube watch --gui stdout https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10\n' +
' $ peertube watch https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10\n'
)
.action((url, options) => run(url, options))
.parse(process.argv)
2021-02-03 09:33:05 +01:00
function run (url: string, options: program.OptionValues) {
2019-05-24 17:42:35 +02:00
if (!url) {
console.error('<url> positional argument is required.')
process.exit(-1)
}
2019-05-24 17:42:35 +02:00
const cmd = 'node ' + join(__dirname, 'node_modules', 'webtorrent-hybrid', 'bin', 'cmd.js')
2021-02-03 09:33:05 +01:00
const args = ` --${options.gui} ` +
url.replace(/(\/videos\/watch\/)|\/w\//, '/download/torrents/') +
2021-02-03 09:33:05 +01:00
`-${options.resolution}.torrent`
try {
execSync(cmd + args)
} catch (err) {
console.error('Cannto exec command.', err)
process.exit(-1)
}
}