]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - support/doc/tools.md
Refactor auth flow
[github/Chocobozzz/PeerTube.git] / support / doc / tools.md
1 # CLI tools guide
2
3 <!-- START doctoc generated TOC please keep comment here to allow auto update -->
4 <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
5 **Table of Contents**
6
7 - [Remote Tools](#remote-tools)
8 - [Dependencies](#dependencies)
9 - [Installation](#installation)
10 - [CLI wrapper](#cli-wrapper)
11 - [peertube-import-videos.js](#peertube-import-videosjs)
12 - [peertube-upload.js](#peertube-uploadjs)
13 - [peertube-watch.js](#peertube-watchjs)
14 - [peertube-plugins.js](#peertube-pluginsjs)
15 - [peertube-redundancy.js](#peertube-redundancyjs)
16 - [Server tools](#server-tools)
17 - [parse-log](#parse-log)
18 - [create-transcoding-job.js](#create-transcoding-jobjs)
19 - [create-import-video-file-job.js](#create-import-video-file-jobjs)
20 - [prune-storage.js](#prune-storagejs)
21 - [optimize-old-videos.js](#optimize-old-videosjs)
22 - [update-host.js](#update-hostjs)
23 - [reset-password.js](#reset-passwordjs)
24 - [plugin install/uninstall](#plugin-installuninstall)
25 - [REPL (Read Eval Print Loop)](#repl-read-eval-print-loop)
26 - [.help](#help)
27 - [Lodash example](#lodash-example)
28 - [YoutubeDL example](#youtubedl-example)
29 - [Models examples](#models-examples)
30
31 <!-- END doctoc generated TOC please keep comment here to allow auto update -->
32
33 ## Remote Tools
34
35 You need at least 512MB RAM to run the script.
36 Scripts can be launched directly from a PeerTube server, or from a separate server, even a desktop PC.
37 You need to follow all the following steps even if you are on a PeerTube server (including cloning the git repository in a different directory than your production installation because the scripts utilize non-production dependencies).
38
39 ### Dependencies
40
41 Install the [PeerTube dependencies](dependencies.md) except PostgreSQL and Redis.
42
43 ### Installation
44
45 Clone the PeerTube repo to get the latest version (even if you are on your PeerTube server):
46
47 ```bash
48 $ git clone https://github.com/Chocobozzz/PeerTube.git
49 $ CLONE="$(pwd)/PeerTube"
50 $ cd ${CLONE}
51 ```
52
53 Install dependencies and build CLI tools:
54
55 ```bash
56 $ NOCLIENT=1 yarn install --pure-lockfile
57 $ npm run setup:cli
58 ```
59
60 ### CLI wrapper
61
62 The wrapper provides a convenient interface to the following scripts.
63 You can access it as `peertube` via an alias in your `.bashrc` like `alias peertube="cd /your/peertube/directory/ && node ./dist/server/tools/peertube.js"` (you have to keep the `cd` command):
64
65 ```
66 Usage: peertube [command] [options]
67
68 Options:
69
70 -v, --version output the version number
71 -h, --help output usage information
72
73 Commands:
74
75 auth [action] register your accounts on remote instances to use them with other commands
76 upload|up upload a video
77 import-videos|import import a video from a streaming platform
78 watch|w watch a video in the terminal ✩°。⋆
79 repl initiate a REPL to access internals
80 plugins|p [action] manage instance plugins
81 redundancy|r [action] manage video redundancies
82 help [cmd] display help for [cmd]
83 ```
84
85 The wrapper can keep track of instances you have an account on. We limit to one account per instance for now.
86
87 ```bash
88 $ peertube auth add -u 'PEERTUBE_URL' -U 'PEERTUBE_USER' --password 'PEERTUBE_PASSWORD'
89 $ peertube auth list
90 ┌──────────────────────────────┬──────────────────────────────┐
91 │ instance │ login │
92 ├──────────────────────────────┼──────────────────────────────┤
93 │ 'PEERTUBE_URL' │ 'PEERTUBE_USER' │
94 └──────────────────────────────┴──────────────────────────────┘
95 ```
96
97 You can now use that account to upload videos without feeding the same parameters again.
98
99 ```bash
100 $ peertube up <videoFile>
101 ```
102
103 And now that your video is online, you can watch it from the confort of your terminal (use `peertube watch --help` to see the supported players):
104
105 ```bash
106 $ peertube watch https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10
107 ```
108
109 To list, install, uninstall dynamically plugins/themes of an instance:
110
111 ```bash
112 $ peertube plugins list
113 $ peertube plugins install --path /local/plugin/path
114 $ peertube plugins install --npm-name peertube-plugin-myplugin
115 $ peertube plugins uninstall --npm-name peertube-plugin-myplugin
116 ```
117
118 #### peertube-import-videos.js
119
120 You can use this script to import videos from all [supported sites of youtube-dl](https://rg3.github.io/youtube-dl/supportedsites.html) into PeerTube.
121 Be sure you own the videos or have the author's authorization to do so.
122
123 ```sh
124 $ node dist/server/tools/peertube-import-videos.js \
125 -u 'PEERTUBE_URL' \
126 -U 'PEERTUBE_USER' \
127 --password 'PEERTUBE_PASSWORD' \
128 --target-url 'TARGET_URL'
129 ```
130
131 * `PEERTUBE_URL` : the full URL of your PeerTube server where you want to import, eg: https://peertube.cpy.re
132 * `PEERTUBE_USER` : your PeerTube account where videos will be uploaded
133 * `PEERTUBE_PASSWORD` : password of your PeerTube account (if `--password PEERTUBE_PASSWORD` is omitted, you will be prompted for it)
134 * `TARGET_URL` : the target url you want to import. Examples:
135 * YouTube:
136 * Channel: https://www.youtube.com/channel/ChannelId
137 * User https://www.youtube.com/c/UserName or https://www.youtube.com/user/UserName
138 * Video https://www.youtube.com/watch?v=blabla
139 * Vimeo: https://vimeo.com/xxxxxx
140 * Dailymotion: https://www.dailymotion.com/xxxxx
141
142 The script will get all public videos from Youtube, download them and upload to PeerTube.
143 Already downloaded videos will not be uploaded twice, so you can run and re-run the script in case of crash, disconnection...
144
145 Videos will be publicly available after transcoding (you can see them before that in your account on the web interface).
146
147 **NB**: If you want to synchronize a Youtube channel to your PeerTube instance (ensure you have the agreement from the author),
148 you can add a [crontab rule](https://help.ubuntu.com/community/CronHowto) (or an equivalent of your OS) and insert
149 these rules (ensure to customize them to your needs):
150
151 ```
152 # Update youtube-dl every day at midnight
153 0 0 * * * /usr/bin/npm rebuild youtube-dl --prefix /PATH/TO/PEERTUBE/
154
155 # Synchronize the YT channel every sunday at 22:00 all the videos published since last monday included
156 0 22 * * 0 /usr/bin/node /PATH/TO/PEERTUBE/dist/server/tools/peertube-import-videos.js -u '__PEERTUBE_URL__' -U '__USER__' --password '__PASSWORD__' --target-url 'https://www.youtube.com/channel/___CHANNEL__' --since $(date --date="-6 days" +%Y-%m-%d)
157 ```
158
159 Also you may want to subscribe to the PeerTube channel in order to manually check the synchronization is successful.
160
161 #### peertube-upload.js
162
163 You can use this script to import videos directly from the CLI.
164
165 Videos will be publicly available after transcoding (you can see them before that in your account on the web interface).
166
167 ```bash
168 $ cd ${CLONE}
169 $ node dist/server/tools/peertube-upload.js --help
170 ```
171
172 #### peertube-watch.js
173
174 You can use this script to play videos directly from the CLI.
175
176 It provides support for different players:
177
178 - ascii (default ; plays in ascii art in your terminal!)
179 - mpv
180 - mplayer
181 - vlc
182 - stdout
183 - xbmc
184 - airplay
185 - chromecast
186
187
188 #### peertube-plugins.js
189
190 Install/update/uninstall or list local or NPM PeerTube plugins:
191
192 ```bash
193 $ cd ${CLONE}
194 $ node dist/server/tools/peertube-plugins.js --help
195 $ node dist/server/tools/peertube-plugins.js list --help
196 $ node dist/server/tools/peertube-plugins.js install --help
197 $ node dist/server/tools/peertube-plugins.js update --help
198 $ node dist/server/tools/peertube-plugins.js uninstall --help
199
200 $ node dist/server/tools/peertube-plugins.js install --path /my/plugin/path
201 $ node dist/server/tools/peertube-plugins.js install --npm-name peertube-theme-example
202 ```
203
204 #### peertube-redundancy.js
205
206 Manage (list/add/remove) video redundancies:
207
208 To list your videos that are duplicated by remote instances:
209
210 ```bash
211 $ node dist/server/tools/peertube.js redundancy list-remote-redundancies
212 ```
213
214 To list remote videos that your instance duplicated:
215
216 ```bash
217 $ node dist/server/tools/peertube.js redundancy list-my-redundancies
218 ```
219
220 To duplicate a specific video in your redundancy system:
221
222 ```bash
223 $ node dist/server/tools/peertube.js redundancy add --video 823
224 ```
225
226 To remove a video redundancy:
227
228 ```bash
229 $ node dist/server/tools/peertube.js redundancy remove --video 823
230 ```
231
232 ## Server tools
233
234 These scripts should be run on the server, in `peertube-latest` directory.
235
236 ### parse-log
237
238 To parse PeerTube last log file:
239
240 ```bash
241 $ cd /var/www/peertube/peertube-latest
242 $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run parse-log -- --level info
243 ```
244
245 `--level` is optional and could be `info`/`warn`/`error`
246
247 ### create-transcoding-job.js
248
249 You can use this script to force transcoding of an existing video. PeerTube needs to be running.
250
251 To generate transcoding jobs depending on the instance configuration:
252 ```bash
253 $ cd /var/www/peertube/peertube-latest
254 $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run create-transcoding-job -- -v [videoUUID]
255 ```
256
257 Or to transcode to a specific resolution:
258 ```bash
259 $ cd /var/www/peertube/peertube-latest
260 $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run create-transcoding-job -- -v [videoUUID] -r [resolution]
261 ```
262 The resolution should be an integer (`1080`, `720`, `480`, etc.)
263
264 To generate an HLS playlist for a video:
265
266 ```bash
267 $ cd /var/www/peertube/peertube-latest
268 $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run create-transcoding-job -- --generate-hls -v [videoUUID]
269 ```
270
271 ### create-import-video-file-job.js
272
273 You can use this script to import a video file to replace an already uploaded file or to add a new resolution to a video. PeerTube needs to be running.
274
275 ```bash
276 $ cd /var/www/peertube/peertube-latest
277 $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run create-import-video-file-job -- -v [videoUUID] -i [videoFile]
278 ```
279
280 ### prune-storage.js
281
282 Some transcoded videos or shutdown at a bad time can leave some unused files on your storage.
283 Stop PeerTube and delete these files (a confirmation will be demanded first):
284
285 ```bash
286 $ cd /var/www/peertube/peertube-latest
287 $ sudo systemctl stop peertube && sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run prune-storage
288 ```
289
290 ### optimize-old-videos.js
291
292 Before version v1.0.0-beta.16, Peertube did not specify a bitrate for the
293 transcoding of uploaded videos. This means that videos might be encoded into
294 very large files that are too large for streaming. This script re-transcodes
295 these videos so that they can be watched properly, even on slow connections.
296
297 ```bash
298 $ cd /var/www/peertube/peertube-latest
299 $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run optimize-old-videos
300 ```
301
302
303 ### update-host.js
304
305 **Changing the hostname is unsupported and may be a risky operation, especially if you have already federated.**
306 If you started PeerTube with a domain, and then changed it you will have
307 invalid torrent files and invalid URLs in your database. To fix this, you have
308 to run the command below (keep in mind your follower instances will NOT update their URLs).
309
310 ```bash
311 $ cd /var/www/peertube/peertube-latest
312 $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run update-host
313 ```
314
315 ### reset-password.js
316
317 To reset a user password from CLI, run:
318
319 ```bash
320 $ cd /var/www/peertube/peertube-latest
321 $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run reset-password -- -u target_username
322 ```
323
324
325 ### plugin install/uninstall
326
327 The difference with `peertube plugins` CLI is that these scripts can be used even if PeerTube is not running.
328 If PeerTube is running, you need to restart it for the changes to take effect (whereas with `peertube plugins` CLI, plugins/themes are dynamically loaded on the server).
329
330 To install/update a plugin or a theme from the disk:
331
332 ```bash
333 $ cd /var/www/peertube/peertube-latest
334 $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run plugin:install -- --plugin-path /local/plugin/path
335 ```
336
337 From NPM:
338
339 ```bash
340 $ cd /var/www/peertube/peertube-latest
341 $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run plugin:install -- --npm-name peertube-plugin-myplugin
342 ```
343
344 To uninstall a plugin or a theme:
345
346 ```bash
347 $ cd /var/www/peertube/peertube-latest
348 $ sudo -u peertube NODE_CONFIG_DIR=/var/www/peertube/config NODE_ENV=production npm run plugin:uninstall -- --npm-name peertube-plugin-myplugin
349 ```
350
351 ### REPL (Read Eval Print Loop)
352
353 If you want to interact with the application libraries and objects even when PeerTube is not running, there is a REPL for that.
354
355 usage: `node ./dist/server/tools/peertube-repl.js`
356
357 "The default evaluator will, by default, assign the result of the most recently evaluated expression to the special variable `_` (underscore). Explicitly setting `_` to a value will disable this behavior."
358
359 - type `.help` to list commands available in the repl, notice it starts with a dot
360 - type `.exit` to exit, note that you still have to press CTRL-C to actually exit, or press CTRL-C (3 times) without typing `.exit` to exit
361 - type `context` to list all available objects and libraries in the context, note: `Promise` is also available but it's not listed in the context, in case you need promises for something
362 - type `env` to see the loaded environment variables
363 - type `path` to access path library
364 - type `lodash` to access lodash library
365 - type `uuidv1` to access uuid/v1 library
366 - type `uuidv3` to access uuid/v3 library
367 - type `uuidv4` to access uuid/v4 library
368 - type `uuidv5` to access uuid/v5 library
369 - type `YoutubeDL` to access youtube-dl library
370 - type `cli` to access the cli helpers object
371 - type `logger` to access the logger; if you log to it, it will write to stdout and to the peertube.log file
372 - type `constants` to access the constants loaded by the server
373 - type `coreUtils` to access the core-utils helpers object
374 - type `ffmpegUtils` to access the ffmpeg-utils helpers object
375 - type `peertubeCryptoUtils` to access the peertube-crypto helpers object
376 - type `signupUtils` to access the signup helpers object
377 - type `utils` to access the utils helpers object
378 - type `YoutubeDLUtils` to access the youtube-dl helpers object
379 - type `sequelizeTypescript` to access sequelizeTypescript
380 - type `modelsUtils` to access the models/utils
381 - type `models` to access the shortcut to sequelizeTypescript.models
382 - type `transaction` to access the shortcut to sequelizeTypescript.transaction
383 - type `query` to access the shortcut to sequelizeTypescript.query
384 - type `queryInterface` to access the shortcut to sequelizeTypescript.queryInterface
385
386 #### .help
387
388 ```
389 PeerTube [1.0.0] (b10eb595)> .help
390 .break Sometimes you get stuck, this gets you out
391 .clear Break, and also clear the local context
392 .editor Enter editor mode
393 .exit Exit the repl
394 .help Print this help message
395 .load Load JS from a file into the REPL session
396 .r Reset REPL
397 .reset Reset REPL
398 .save Save all evaluated commands in this REPL session to a file
399 PeerTube [1.0.0] (b10eb595)>
400 ```
401
402 #### Lodash example
403
404 ```
405 PeerTube [1.0.0] (b10eb595)> lodash.keys(context)
406 [ 'global',
407 'console',
408 'DTRACE_NET_SERVER_CONNECTION',
409 'DTRACE_NET_STREAM_END',
410 'DTRACE_HTTP_SERVER_REQUEST',
411 'DTRACE_HTTP_SERVER_RESPONSE',
412 'DTRACE_HTTP_CLIENT_REQUEST',
413 'DTRACE_HTTP_CLIENT_RESPONSE',
414 'process',
415 'Buffer',
416 'clearImmediate',
417 'clearInterval',
418 'clearTimeout',
419 'setImmediate',
420 'setInterval',
421 'setTimeout',
422 'XMLHttpRequest',
423 'compact2string',
424 'module',
425 'require',
426 'path',
427 'repl',
428 'context',
429 'env',
430 'lodash',
431 'uuidv1',
432 'uuidv3',
433 'uuidv4',
434 'uuidv5',
435 'cli',
436 'logger',
437 'constants',
438 'Sequelize',
439 'sequelizeTypescript',
440 'modelsUtils',
441 'models',
442 'transaction',
443 'query',
444 'queryInterface',
445 'YoutubeDL',
446 'coreUtils',
447 'ffmpegUtils',
448 'peertubeCryptoUtils',
449 'signupUtils',
450 'utils',
451 'YoutubeDLUtils' ]
452 PeerTube [1.0.0] (b10eb595)>
453 ```
454
455 #### YoutubeDL example
456 ```
457 YoutubeDL.getInfo('https://www.youtube.com/watch?v=I5ZN289jjDo', function(err, data) {console.log(err, data)})
458 ```
459
460 #### Models examples
461 ```
462 PeerTube [1.0.0] (b10eb595)> new models.ActorModel({id: 3}).getVideoChannel().then(function(data){console.log(data.dataValues.name)})
463 Promise {
464 _bitField: 0,
465 _fulfillmentHandler0: undefined,
466 _rejectionHandler0: undefined,
467 _promise0: undefined,
468 _receiver0: undefined }
469 PeerTube [1.0.0] (b10eb595)> Main root channel
470 PeerTube [1.0.0] (b10eb595)> let out; new models.UserModel({id: 1}).getAccount().then(function (data) {out = data.dataValues.id})
471 Promise {
472 _bitField: 0,
473 _fulfillmentHandler0: undefined,
474 _rejectionHandler0: undefined,
475 _promise0: undefined,
476 _receiver0: undefined }
477 PeerTube [1.0.0] (b10eb595)> out
478 2
479 PeerTube [1.0.0] (b10eb595)>
480 ```