Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

next-video

> 前端框架
Open source

The easiest way to add video in your Nextjs app.

1.2K stars0 likes0 views
WebsiteGitHub

About

The easiest way to add video in your Nextjs app.

next-video

Next video is a react component for adding video to your next.js application. It extends both the `` element and your Next app with features for automatic video optimization.

  • Smart storage: Store large video files outside of your git repo
  • Auto optimized: Optimize video files and deliver via CDN for better playback performance and quality
  • Customizable UI: Choose from themes or build your own player controls
  • Posters & Previews: Zero-config placeholder images and timeline hover thumbnails
  • Wider compatibility: Use videos that aren’t supported natively by all browsers
  • Analytics built-in (optional): See how often videos get watched and track video performance
  • AI-powered: Add auto-generated captions to your videos and use transcripts
typescript
import Video from 'next-video';
import getStarted from '/videos/get-started.mp4';

export default function Page() {
  return ;
}

Setup

Automatic Setup

In the root of your Next.js project, run:

bash
npx -y next-video init

This will (with prompting):

  • install next-video as a dependency
  • create a /videos directory and update .gitignore
  • update your next.config.js file
  • if you're using TypeScript, add types for your video file imports
  • add next-video sync -w to your dev script in package.json
  • set up Mux credentials in .env.local for remote storage
  • add a sample video and demo page to test your setup

It will also update your .gitignore file to ignore video files in the /videos directory. Videos, particularly any of reasonable size, shouldn't be stored/tracked by git. Alternatively, if you'd like to store the original files you can remove the added gitignore lines and install git-lfs.

Remote storage and optimization

Vercel recommends using a dedicated content platform for video because video files are large and can lead to excessive bandwidth usage. By default, next-video uses Mux (a video API for developers), which is built by the the creators of Video.js, powers popular streaming apps like Patreon, and whose video performance monitoring is used on the largest live events in the world.

  • Sign up for Mux
  • Create an access token
  • Add environment variables to .env.local (or however you export local env variables)
bash
# .env.local
MUX_TOKEN_ID=[YOUR_TOKEN_ID]
MUX_TOKEN_SECRET=[YOUR_TOKEN_SECRET]

Manual Setup

Click to see the manual init steps.

Install the package

bash
cd your-next-app

# If your project is using NPM (the default for Next.js)
npm install next-video

# If your project is using Yarn
yarn add next-video

# If your project is using pnpm
pnpm add next-video

Create the videos directory

Create a /videos directory in your project root. This is where you'll store your video source files.

bash
mkdir videos

Add the following to your .gitignore to keep video files out of your repo while still tracking their JSON metadata:

# next-video
videos/*
!videos/*.json
!videos/*.js
!videos/*.ts
public/_next-video

Videos, particularly any of reasonable size, shouldn't be stored/tracked by git. Alternatively, if you'd like to store the original files you can remove the added gitignore lines and install git-lfs.

Add Next Video to your Next.js config

next.config.js

If you're using CommonJS modules:

javascript
const { withNextVideo } = require('next-video/process');

/** @type {import('next').NextConfig} */
const nextConfig = {}; // Your current Next Config object

module.exports = withNextVideo(nextConfig);

next.config.mjs

If you're using ES modules:

javascript
import { withNextVideo } from 'next-video/process';

/** @type {import('next').NextConfig} */
const nextConfig = {};

export default withNextVideo(nextConfig);

Add video import types to tsconfig.json

This is only required if you're using TypeScript, and makes sure your video file imports don't yell at you for missing types. Create a video.d.ts file in your project root:

typescript
// video.d.ts
/// 

Then add that file to the include array in tsconfig.json.

javascript
{
  // ...
  "include": ["video.d.ts", "next-env.d.ts", /* ... */ ]
  // ...
}

If you're using Turbopack (the default dev server in recent versions of Next.js), you'll also need to add a path alias so that /videos imports resolve correctly. Add the following to compilerOptions.paths in your tsconfig.json:

json
{
  "compilerOptions": {
    "paths": {
      "@/*": ["./src/*"],
      "@videos/*": ["./videos/*"]
    }
  }
}

Then use @videos/ instead of /videos/ in your imports:

typescript
import getStarted from '@videos/get-started.mp4';

Note: If you're using webpack (i.e. running next dev --webpack), /videos imports work without any additional path configuration.

Add the watch command to your dev script

Update your package.json dev script to automatically sync videos while the dev server is running:

json
// package.json
"scripts": {
  "dev": "next dev & npx next-video sync -w"
}

Set up remote storage with Mux

  1. Sign up for Mux if you don't have an account
  2. Create an access token with Mux Video permissions
  3. Add your credentials to .env.local:
bash
# .env.local
MUX_TOKEN_ID=[YOUR_TOKEN_ID]
MUX_TOKEN_SECRET=[YOUR_TOKEN_SECRET]

Make sure .env.local is in your .gitignore.

Add a test video

Add a video file (e.g. sample-video.mp4) to the /videos directory and run the sync command to upload and process it:

bash
npx next-video sync

Create a demo page

Create a page to verify your setup is working (app/demo-video/page.tsx or pages/demo-video.tsx):

typescript
import Video from 'next-video';
import sampleVideo from '/videos/sample-video.mp4';

export default function DemoVideo() {
  return ;
}

Turbopack users: If you configured the @videos/* path alias above, use @videos/sample-video.mp4 instead of /videos/sample-video.mp4 in your imports.

Then build and start your app, and navigate to /demo-video:

bash
npm run dev

Usage

Local videos (Demo)

Add videos locally to the /videos directory then run npx next-video sync. The videos will be automatically uploaded to remote storage and optimized. You'll notice /videos/[file-name].json files are also created. These are used to map your local video files to the new, remote-hosted video assets. These json files must be checked into git.

bash
npx next-video sync

You can also add next-video sync -w to the dev script to automatically sync videos as they're added to /videos while the dev server is running.

javascript
// package.json
"scripts": {
  "dev": "next dev & npx next-video sync -w",
},

Now you can use the `` component in your application. Let's say you've added a file called awesome-video.mp4 to /videos

typescript
import Video from 'next-video';
import awesomeVideo from '/videos/awesome-video.mp4';

export default function Page() {
  return ;
}

Turbopack users: Recent versions of Next.js use Turbopack as the default dev server. Turbopack doesn't resolve /videos imports out of the box. You'll need to add a path alias in your tsconfig.json:

json
"paths": {
  "@/*": ["./src/*"],
  "@videos/*": ["./videos/*"]
}

Then use @videos/awesome-video.mp4 instead of /videos/awesome-video.mp4 in your imports. This is not needed if you're using webpack.

While a video is being uploaded and processed, `` will attempt to play the local file. This only happens during local development because the local file is never uploaded to your git repo.

Remote videos

For videos that are already hosted remotely (for example on AWS S3), import the remote URL and refresh the page. This creates a local JSON file in the /videos folder and the sync script will start uploading the video.

typescript
import Video from 'next-video';
import awesomeVideo from 'https://www.mydomain.com/remote-video.mp4';

export default function Page() {
  return ;
}

If the hosted video is a single file like an MP4, the file will be automatically optimized for better deliverability and compatibility.

Remote videos with string source URL

In some cases you might not have the remote video URL's available at the time of import.

That can be solved by creating a new API endpoint in your Next.js app for /api/video with the following code.

App router (Next.js >=13)

javascript
// app/api/video/route.js
export { GET } from 'next-video/request-handler';

Pages router (Next.js)

javascript
// pages/api/video/[[...handler]].js
export { default } from 'next-video/request-handler';

Then set the src attribute to the URL of the remote video, refresh the page and the video will start processing.

typescript
import Video from 'next-video';

export default function Page() {
  return ;
}

[!IMPORTANT] The /api/video route is unauthenticated by default. The handler only serves asset descriptors that resolve inside the configured video folder (default videos/) — requests for paths outside that folder return 404. If you expose this route in production, consider gating it behind authentication.

Adopt existing Mux videos

If you've already uploaded videos to Mux (for example, through the Mux dashboard) and want to use them with next-video, you can use the adopt command to create local asset metadata files without re-uploading.

bash
npx next-video adopt 

The command will:

  • Fetch metadata from Mux (including the video title)
  • Prompt you for the asset name and configuration (in interactive mode)
  • Generate the asset JSON file with all necessary metadata
  • Provide ready-to-use import code for your Next.js application

Interactive mode (default)

bash
npx next-video adopt abc123XYZ456def789ghi0

You'll be prompted for:

  • Asset name (defaults to the video title from Mux or a generated name)
  • Whether to configure a custom thumbnail time
  • Confirmation to proceed if not using Mux as the default provider

Non-interactive mode

For automation or CI/CD, you can provide all options via flags:

bash
npx next-video adopt abc123XYZ456def789ghi0 \
  --name my-video \
  --dir videos \
  --thumbnail-time 5.5 \
  --no-interactive

Options

Option Alias Description Default
playbackId - The Mux playback ID of the asset to adopt (required) -
--dir -d Directory to save the asset metadata file videos
--name -n Custom name for the asset file (without extension) Video title or adopted-{playbackId}
--thumbnail-time -t Thumbnail time in seconds for the poster image -
--interactive -i Run in interactive mode true

Example output

After running the command, you'll get a file like videos/my-video.mp4.json and usage instructions:

typescript
import Video from 'next-video';
import myVideo from '/videos/my-video.mp4';

export default function Page() {
  return ;
}

Notes

  • The adopt command currently only works with Mux assets
  • You need a valid Mux playback ID
  • The video must already exist and be ready in your Mux account
  • For other providers, use the remote video import method

Change player theme ([Demo](https

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScripthacktoberfestnextnextjsreact

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架