]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - support/doc/plugins/guide.md
Try to support other codecs
[github/Chocobozzz/PeerTube.git] / support / doc / plugins / guide.md
index 41d70c372b000714896d514a929303f216cfc43c..a4a1a136d178343b5906a09192f187f3d22e16f1 100644 (file)
@@ -21,6 +21,8 @@
     - [Custom Modal](#custom-modal)
     - [Translate](#translate)
     - [Get public settings](#get-public-settings)
+    - [Add custom fields to video form](#add-custom-fields-to-video-form)
+    - [Add new transcoding profiles](#add-new-transcoding-profiles)
   - [Publishing](#publishing)
 - [Write a plugin/theme](#write-a-plugintheme)
   - [Clone the quickstart repository](#clone-the-quickstart-repository)
@@ -29,6 +31,7 @@
   - [Update package.json](#update-packagejson)
   - [Write code](#write-code)
   - [Add translations](#add-translations)
+  - [Build your plugin](#build-your-plugin)
   - [Test your plugin/theme](#test-your-plugintheme)
   - [Publish](#publish)
 - [Plugin & Theme hooks/helpers API](#plugin--theme-hookshelpers-api)
@@ -163,7 +166,7 @@ registerSetting({
   name: 'admin-name',
   label: 'Admin name',
   type: 'input',
-  // type: input | input-checkbox | input-textarea | markdown-text | markdown-enhanced
+  // type: input | input-checkbox | input-password | input-textarea | markdown-text | markdown-enhanced
   default: 'my super name'
 })
 
@@ -379,6 +382,155 @@ peertubeHelpers.getSettings()
   })
 ```
 
+#### Add custom fields to video form
+
+To add custom fields in the video form (in *Plugin settings* tab):
+
+```js
+async function register ({ registerVideoField, peertubeHelpers }) {
+  const descriptionHTML = await peertubeHelpers.translate(descriptionSource)
+  const commonOptions = {
+    name: 'my-field-name,
+    label: 'My added field',
+    descriptionHTML: 'Optional description',
+    type: 'input-textarea',
+    default: ''
+  }
+
+  for (const type of [ 'upload', 'import-url', 'import-torrent', 'update' ]) {
+    registerVideoField(commonOptions, { type })
+  }
+}
+```
+
+PeerTube will send this field value in `body.pluginData['my-field-name']` and fetch it from `video.pluginData['my-field-name']`.
+
+So for example, if you want to store an additional metadata for videos, register the following hooks in **server**:
+
+```js
+async function register ({
+  registerHook,
+  storageManager
+}) {
+  const fieldName = 'my-field-name'
+
+  // Store data associated to this video
+  registerHook({
+    target: 'action:api.video.updated',
+    handler: ({ video, body }) => {
+      if (!body.pluginData) return
+
+      const value = body.pluginData[fieldName]
+      if (!value) return
+
+      storageManager.storeData(fieldName + '-' + video.id, value)
+    }
+  })
+
+  // Add your custom value to the video, so the client autofill your field using the previously stored value
+  registerHook({
+    target: 'filter:api.video.get.result',
+    handler: async (video) => {
+      if (!video) return video
+      if (!video.pluginData) video.pluginData = {}
+
+      const result = await storageManager.getData(fieldName + '-' + video.id)
+      video.pluginData[fieldName] = result
+
+      return video
+    }
+  })
+}
+```
+
+#### Add new transcoding profiles
+
+Adding transcoding profiles allow admins to change ffmpeg encoding parameters and/or encoders.
+A transcoding profile has to be chosen by the admin of the instance using the admin configuration.
+
+```js
+async function register ({
+  transcodingManager
+}) {
+
+  // Adapt bitrate when using libx264 encoder
+  {
+    const builder = (options) => {
+      const { input, resolution, fps, streamNum } = options
+
+      const streamString = streamNum ? ':' + streamNum : ''
+
+      // You can also return a promise
+      return {
+        outputOptions: [
+        // Use a custom bitrate
+          '-b' + streamString + ' 10K'
+        ]
+      }
+    }
+
+    const encoder = 'libx264'
+    const profileName = 'low-quality'
+
+    // Support this profile for VOD transcoding
+    transcodingManager.addVODProfile(encoder, profileName, builder)
+
+    // And/Or support this profile for live transcoding
+    transcodingManager.addLiveProfile(encoder, profileName, builder)
+  }
+
+  {
+    const builder = (options) => {
+      const { streamNum } = options
+
+      const streamString = streamNum ? ':' + streamNum : ''
+
+      // Always copy stream when PeerTube use libfdk_aac or aac encoders
+      return {
+        copy: true
+      }
+    }
+
+    const profileName = 'copy-audio'
+
+    for (const encoder of [ 'libfdk_aac', 'aac' ]) {
+      transcodingManager.addVODProfile(encoder, profileName, builder)
+    }
+  }
+```
+
+PeerTube will try different encoders depending on their priority.
+If the encoder is not available in the current transcoding profile or in ffmpeg, it tries the next one.
+Plugins can change the order of these encoders and add their custom encoders:
+
+```js
+async function register ({
+  transcodingManager
+}) {
+
+  // Adapt bitrate when using libx264 encoder
+  {
+    const builder = () => {
+      return {
+        outputOptions: []
+      }
+    }
+
+    // Support libopus and libvpx-vp9 encoders (these codecs could be incompatible with the player)
+    transcodingManager.addVODProfile('libopus', 'test-vod-profile', builder)
+
+    // Default priorities are ~100
+    // Lowest priority = 1
+    transcodingManager.addVODEncoderPriority('audio', 'libopus', 1000)
+
+    transcodingManager.addVODProfile('libvpx-vp9', 'test-vod-profile', builder)
+    transcodingManager.addVODEncoderPriority('video', 'libvpx-vp9', 1000)
+
+    transcodingManager.addLiveProfile('libopus', 'test-live-profile', builder)
+    transcodingManager.addLiveEncoderPriority('audio', 'libopus', 1000)
+  }
+```
+
 
 ### Publishing
 
@@ -498,6 +650,31 @@ Translation files are just objects, with the english sentence as the key and the
 }
 ```
 
+### Build your plugin
+
+If you added client scripts, you'll need to build them using webpack.
+
+Install webpack:
+
+```
+$ npm install
+```
+
+Add/update your files in the `clientFiles` array of `webpack.config.js`:
+
+```
+$ $EDITOR ./webpack.config.js
+```
+
+Build your client files:
+
+```
+$ npm run build
+```
+
+You built files are in the `dist/` directory. Check `package.json` to correctly point to them.
+
+
 ### Test your plugin/theme
 
 You'll need to have a local PeerTube instance:
@@ -547,7 +724,7 @@ and republish it on NPM. Remember that the PeerTube index will take into account
 
 ## Plugin & Theme hooks/helpers API
 
-See the dedicated documentation: https://docs.joinpeertube.org/#/api-plugins
+See the dedicated documentation: https://docs.joinpeertube.org/api-plugins
 
 
 ## Tips
@@ -569,7 +746,7 @@ registerHook({
   }
 })
 ```
-  * Don't try to require parent PeerTube modules, only use `peertubeHelpers`. If you need another helper or a specific hook, please [create an issue](https://github.com/Chocobozzz/PeerTube/issues/new)
+  * Don't try to require parent PeerTube modules, only use `peertubeHelpers`. If you need another helper or a specific hook, please [create an issue](https://github.com/Chocobozzz/PeerTube/issues/new/choose)
   * Don't use PeerTube dependencies. Use your own :)
 
 If your plugin is broken with a new PeerTube release, update your code and the `peertubeEngine` field of your `package.json` field.