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
``tsx
import Video from 'next-video';
import getStarted from '/videos/get-started.mp4';
export default function Page() {
return ;
}
`
Setup
$3
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
- update your next.config.js file
- if you're using TypeScript, add types for your video file imports
- create a /videos directory in your project which is where you will put all video source files.
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.
$3
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]
`
$3
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
`
#### Add Next Video to your Next.js config
next.config.js
If you're using CommonJS modules:
`
js
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:
`
js
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.
video.d.ts
should have been created in your project root when you ran npx next-video init
, if not you can create it manually:`
ts
// video.d.ts
///
`
Then add that file to the
include
array in tsconfig.json
.`
js
{
// ...
"include": ["video.d.ts", "next-env.d.ts", / ... / ]
// ...
}
`
Usage
$3
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.`
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.`
js
// package.json
"scripts": {
"dev": "next dev & npx next-video sync -w",
},
`
Now you can use the