1.1.1 • Published 7 months ago

@oversightstudio/mux-video v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Mux Video Payload Plugin

Install

pnpm add @oversightstudio/mux-video @mux/mux-player-react

About

This plugin brings Mux Video to Payload! It creates a “Videos” collection within the admin panel, making it simple to upload videos directly to Mux and manage them.

Features include:

  • Support for both public and signed playback policies.
  • Ensures that videos deleted in the admin panel are automatically deleted from Mux, and vice versa.
  • Video gif previews on the videos collection list view, which can be disabled if required.

muxVideoPreview

Payload Setup

There are two possible setups for this plugin: The public setup, and the signed URLs setup. The main difference between the two is that the signed URLs setup requires setting up a little extra configuration, but that's about it.

To get started, you’ll need to generate your MUX tokens and secrets from the MUX Dashboard. When configuring the webhook, set the URL to the automatically generated API endpoint provided by this plugin at /api/mux/webhook.

Public Setup

import { buildConfig } from 'payload'
import { muxVideoPlugin } from '@oversightstudio/mux-video'

export default buildConfig({
  plugins: [
    muxVideoPlugin({
      enabled: true,
      initSettings: {
        tokenId: process.env.MUX_TOKEN_ID || '',
        tokenSecret: process.env.MUX_TOKEN_SECRET || '',
        webhookSecret: process.env.MUX_WEBHOOK_SIGNING_SECRET || '',
      },
      uploadSettings: {
        cors_origin: process.env.NEXT_PUBLIC_SERVER_URL || 'http://localhost:3000',
      },
    }),
  ],
})

Signed URLs Setup

import { buildConfig } from 'payload'
import { muxVideoPlugin } from '@oversightstudio/mux-video'

export default buildConfig({
  plugins: [
    muxVideoPlugin({
      enabled: true,
      initSettings: {
        tokenId: process.env.MUX_TOKEN_ID || '',
        tokenSecret: process.env.MUX_TOKEN_SECRET || '',
        webhookSecret: process.env.MUX_WEBHOOK_SIGNING_SECRET || '',
        jwtSigningKey: process.env.MUX_JWT_KEY_ID || '',
        jwtPrivateKey: process.env.MUX_JWT_KEY || '',
      },
      uploadSettings: {
        cors_origin: process.env.NEXT_PUBLIC_SERVER_URL || 'http://localhost:3000',
        new_asset_settings: {
          playback_policy: ['signed'],
        },
      },
    }),
  ],
})

Options

OptionTypeDefaultDescription
enabledbooleanRequiredWhether the plugin is enabled.
initSettingsMuxVideoInitSettingsRequiredInitialization settings for the Mux implementation.
uploadSettingsMuxVideoUploadSettingsRequiredUpload settings for Mux video assets.
access(request: PayloadRequest) => Promise<boolean> \| booleanOptionalAn optional function to determine who can upload files. Should return a boolean or a Promise resolving to a boolean.
signedUrlOptionsMuxVideoSignedUrlOptionsOptionalOptions for signed URL generation.
adminThumbnail'gif' \| 'image' \| 'none'"gif"Specifies the type of thumbnail to display for videos in the collection list view.

initSettings Options

OptionTypeDefaultDescription
tokenIdstringRequiredThe Mux token ID.
tokenSecretstringRequiredThe Mux token secret.
webhookSecretstringRequiredThe secret used to validate Mux webhooks.
jwtSigningKeystringOptionalOptional JWT signing key, required for signed URL setup.
jwtPrivateKeystringOptionalOptional JWT private key, required for signed URL setup.

uploadSettings Options

OptionTypeDefaultDescription
cors_originstringRequiredThe required CORS origin for Mux.
new_asset_settingsMuxVideoNewAssetSettingsOptionalAdditional settings for creating assets in Mux.

new_asset_settings Options

OptionTypeDefaultDescription
playback_policyArray<'public' | 'signed'>publicControls the playback policy for uploaded videos. Default is public.

signedUrlOptions Options

OptionTypeDefaultDescription
expirationstring"1d"Expiration time for signed URLs. Default is "1d".

Videos Collection

This is the collection generated by the plugin with the mux-video slug.

FieldTypeRead-OnlyDescription
titletextNoA unique title for this video that will help you identify it later.
assetIdtextYes
durationnumberYes
posterTimestampnumberNoA timestamp (in seconds) from the video to be used as the poster image. When unset, defaults to the middle of the video.
aspectRatiotextYes
maxWidthnumberYes
maxHeightnumberYes
playbackOptionsarrayYes

playbackOptions Fields

FieldTypeRead-OnlyDescription
playbackIdtextYes
playbackPolicyselectYesOptions: signed, public
playbackUrltext (virtual)Yes
posterUrltext (virtual)Yes

Payload Usage Example

import { CollectionConfig } from 'payload'

export const ExampleCollection: CollectionConfig = {
  slug: 'example',
  fields: [
    // To link videos to other collection, use the `relationship` field type
    {
      name: 'video',
      label: 'Preview Video',
      type: 'relationship',
      relationTo: 'mux-video',
    },
  ],
}

Frontend Usage Example

import config from '@/payload.config'
import { getPayload } from 'payload'
import MuxPlayer from '@mux/mux-player-react'

async function Page() {
  const payload = await getPayload({ config })

  const video = await payload.findByID({
    collection: 'mux-video',
    id: 'example',
  })

  return (
    <MuxPlayer
      // Using playback id
      playbackId={video.playbackOptions![0].playbackId!}
      // Or use the playback URL
      src={video.playbackOptions![0].playbackUrl!}
      // Poster
      poster={video.playbackOptions![0].posterUrl!}
    />
  )
}

export default Page

Credits

  • Huge shoutout to jamesvclements for building the initial version of this plugin!
  • Shoutout to Paul for being a real one.
1.1.1

7 months ago

1.1.0

8 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago