Unverified Commit ae46eb88 authored by Lee Robinson's avatar Lee Robinson Committed by GitHub
parent 53a0a0fb
......@@ -4,8 +4,6 @@ nav_title: Videos
description: Recommendations and best practices for optimizing videos in your Next.js application.
---
# Optimizing videos in Next.js applications
This page outlines how to use videos with Next.js applications, showing how to store and display video files without affecting performance.
## Using `<video>` and `<iframe>`
......@@ -36,22 +34,18 @@ export function Video() {
### Common `<video>` tag attributes
| Attribute | Description | Example Value |
| ------------- | --------------------------------------------------------------------------------------------------------- | ------------------------------------------ |
| `src` | Specifies the source of the video file. | `<video src="/path/to/video.mp4"></video>` |
| `width` | Sets the width of the video player. | `<video width="320"></video>` |
| `height` | Sets the height of the video player. | `<video height="240"></video>` |
| `controls` | If present, it displays the default set of playback controls. | `<video controls></video>` |
| `autoplay` | Automatically starts playing the video when the page loads. Note: Autoplay policies vary across browsers. | `<video autoplay></video>` |
| `loop` | Loops the video playback. | `<video loop></video>` |
| `muted` | Mutes the audio by default. Often used with `autoplay`. | `<video muted></video>` |
| `preload` | Specifies how the video is preloaded. Values: `none`, `metadata`, `auto`. | `<video preload="none"></video>` |
| `playsInline` | Enables inline playback on iOS devices, often necessary for autoplay to work on iOS Safari. | `<video playsInline></video>` |
> **Good to know**: When using the `autoplay` attribute, it is important to also include the `muted` attribute to ensure the video plays automatically in most browsers, and the `playsInline` attribute for compatibility with iOS devices.
>
> ```
> <video autoplay muted playsInline></video>
> ```
| ------------- | --------------------------------------------------------------------------------------------------------- | ------------------------------------ |
| `src` | Specifies the source of the video file. | `<video src="/path/to/video.mp4" />` |
| `width` | Sets the width of the video player. | `<video width="320" />` |
| `height` | Sets the height of the video player. | `<video height="240" />` |
| `controls` | If present, it displays the default set of playback controls. | `<video controls />` |
| `autoplay` | Automatically starts playing the video when the page loads. Note: Autoplay policies vary across browsers. | `<video autoplay />` |
| `loop` | Loops the video playback. | `<video loop />` |
| `muted` | Mutes the audio by default. Often used with `autoplay`. | `<video muted />` |
| `preload` | Specifies how the video is preloaded. Values: `none`, `metadata`, `auto`. | `<video preload="none" />` |
| `playsInline` | Enables inline playback on iOS devices, often necessary for autoplay to work on iOS Safari. | `<video playsInline />` |
> **Good to know**: When using the `autoplay` attribute, it is important to also include the `muted` attribute to ensure the video plays automatically in most browsers and the `playsInline` attribute for compatibility with iOS devices.
For a comprehensive list of video attributes, refer to the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video#attributes).
......@@ -80,15 +74,15 @@ export default function Page() {
### Common `<iframe>` tag attributes
| Attribute | Description | Example Value |
| ----------------- | ---------------------------------------------------------------------- | --------------------------------------------- |
| `src` | The URL of the page to embed. | `<iframe src="https://example.com"></iframe>` |
| `width` | Sets the width of the iframe. | `<iframe width="500"></iframe>` |
| `height` | Sets the height of the iframe. | `<iframe height="300"></iframe>` |
| `frameborder` | Specifies whether or not to display a border around the iframe. | `<iframe frameborder="0"></iframe>` |
| `allowfullscreen` | Allows the iframe content to be displayed in full-screen mode. | `<iframe allowfullscreen></iframe>` |
| `sandbox` | Enables an extra set of restrictions on the content within the iframe. | `<iframe sandbox></iframe>` |
| `loading` | Optimize loading behavior (e.g., lazy loading). | `<iframe loading="lazy"></iframe>` |
| `title` | Provides a title for the iframe to support accessibility. | `<iframe title="Description"></iframe>` |
| ----------------- | ---------------------------------------------------------------------- | -------------------------------------- |
| `src` | The URL of the page to embed. | `<iframe src="https://example.com" />` |
| `width` | Sets the width of the iframe. | `<iframe width="500" />` |
| `height` | Sets the height of the iframe. | `<iframe height="300" />` |
| `frameborder` | Specifies whether or not to display a border around the iframe. | `<iframe frameborder="0" />` |
| `allowfullscreen` | Allows the iframe content to be displayed in full-screen mode. | `<iframe allowfullscreen />` |
| `sandbox` | Enables an extra set of restrictions on the content within the iframe. | `<iframe sandbox />` |
| `loading` | Optimize loading behavior (e.g., lazy loading). | `<iframe loading="lazy" />` |
| `title` | Provides a title for the iframe to support accessibility. | `<iframe title="Description" />` |
For a comprehensive list of iframe attributes, refer to the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attributes).
......@@ -105,27 +99,27 @@ Choose the embedding method that aligns with your application's requirements and
To embed videos from external platforms, you can use Next.js to fetch the video information and React Suspense to handle the fallback state while loading.
1. **Create a Server Component for video embedding**
**1. Create a Server Component for video embedding**
The first step is to create a [Server Component](https://nextjs.org/docs/app/building-your-application/rendering/server-components) that generates the appropriate iframe for embedding the video. This component will fetch the source URL for the video and render the iframe.
The first step is to create a [Server Component](https://nextjs.org/docs/app/building-your-application/rendering/server-components) that generates the appropriate iframe for embedding the video. This component will fetch the source URL for the video and render the iframe.
```jsx filename="app/ui/video-component.jsx"
export const async function VideoComponent (){
```jsx filename="app/ui/video-component.jsx"
export const async function VideoComponent (){
const src = await getVideoSrc()
return <iframe src={src} frameborder="0" allowfullscreen />
}
```
}
```
2. **Stream the video component using React Suspense**
**2. Stream the video component using React Suspense**
After creating the Server Component to embed the video, the next step is to [stream](https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming) the component using [React Suspense](https://react.dev/reference/react/Suspense).
After creating the Server Component to embed the video, the next step is to [stream](https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming) the component using [React Suspense](https://react.dev/reference/react/Suspense).
```jsx filename="app/page.jsx"
import { Suspense } from 'react'
import VideoComponent from '../ui/VideoComponent.jsx'
```jsx filename="app/page.jsx"
import { Suspense } from 'react'
import VideoComponent from '../ui/VideoComponent.jsx'
export default function Page() {
export default function Page() {
return (
<section>
<Suspense fallback={<p>Loading video...</p>}>
......@@ -134,15 +128,17 @@ To embed videos from external platforms, you can use Next.js to fetch the video
{/* Other content of the page */}
</section>
)
}
```
}
```
> **Good to know**: When embedding videos from external platforms, consider the following best practices:
>
> - **Responsive embedding**: Ensure the video embeds are responsive. Use CSS to make the iframe or video player adapt to different screen sizes.
> - **Consider data usage**: Implement [strategies for loading videos](https://yoast.com/site-speed-tips-for-faster-video/) based on network conditions, especially for users with limited data plans.
> - Ensure the video embeds are responsive. Use CSS to make the iframe or video player adapt to different screen sizes.
> - Implement [strategies for loading videos](https://yoast.com/site-speed-tips-for-faster-video/) based on network conditions, especially for users with limited data plans.
This approach results in a better user experience as it prevents the page from blocking, meaning the user can interact with the page while the video component streams in.
This approach results in a better user experience as it prevents the page from blocking, meaning the user can interact with the page while the video component streams in. For a more engaging and informative loading experience, consider using a loading skeleton as the fallback UI. So instead of showing a simple loading message, you can show a skeleton that resembles the video player like this:
For a more engaging and informative loading experience, consider using a loading skeleton as the fallback UI. So instead of showing a simple loading message, you can show a skeleton that resembles the video player like this:
```jsx filename="app/page.jsx"
import { Suspense } from 'react'
......@@ -214,7 +210,7 @@ async function VideoComponent({ fileName }) {
};
```
In this approach, the page uses the video’s `@vercel/blob` URL to display the video using the `VideoComponent`. React Suspense is used to show a fallback until the video URL is fetched and the video is ready to be displayed.
In this approach, the page uses the video's `@vercel/blob` URL to display the video using the `VideoComponent`. React Suspense is used to show a fallback until the video URL is fetched and the video is ready to be displayed.
### Adding subtitles to your video
......@@ -251,20 +247,27 @@ To continue learning more about video optimization and best practices, please re
- **Understanding video formats and codecs**: Choose the right format and codec, like MP4 for compatibility or WebM for web optimization, for your video needs. For more details, see [Mozilla's guide on video codecs](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Video_codecs).
- **Video compression**: Use tools like FFmpeg to effectively compress videos, balancing quality with file size. Learn about compression techniques at [FFmpeg's official website](https://www.ffmpeg.org/).
- **Resolution and bitrate adjustment**: Adjust resolution and bitrate based on the viewing platform, with lower settings for mobile devices. For an in-depth understanding, read [DaCast’s guide](https://www.dacast.com/blog/bitrate-vs-resolution/#:~:text=The%20two%20measure%20different%20aspects,yield%20different%20qualities%20of%20video) on bitrate and resolution.
- **Resolution and bitrate adjustment**: Adjust [resolution and bitrate](https://www.dacast.com/blog/bitrate-vs-resolution/#:~:text=The%20two%20measure%20different%20aspects,yield%20different%20qualities%20of%20video) based on the viewing platform, with lower settings for mobile devices.
- **Content Delivery Networks (CDNs)**: Utilize a CDN to enhance video delivery speed and manage high traffic. When using some storage solutions, such as Vercel Blob, CDN functionality is automatically handled for you. [Learn more](https://www.maestro.io/blog/video-cdn/) about CDNs and their benefits.
Explore these video streaming platforms for integrating video into your Next.js projects:
- **Open source `next-video` component**
- Provides a `<Video>` component for Next.js, compatible with various hosting services including Vercel Blob, S3, Backblaze, and Mux.
- [Detailed documentation](https://next-video.dev/docs) for using `next-video.dev` with different hosting services.
- **Cloudinary Integration**
- Official [documentation and integration guides](https://support.cloudinary.com/hc/en-us/categories/200477762-Client-Libraries-and-Integration-Guides) for using Cloudinary with Next.js.
- A [specific guide](https://next.cloudinary.dev/nextjs-13) on integrating Cloudinary with Next.js.
- **Mux Video API**
- Mux provides a [starter template](https://github.com/muxinc/video-course-starter-kit) for creating a video course with Mux and Next.js.
- Learn about Mux's recommendations for embedding [high-performance video for your Next.js application](https://www.mux.com/for/nextjs).
- Explore an [example project](https://with-mux-video.vercel.app/) demonstrating Mux with Next.js.
- **Fastly**
- Learn more about integrating Fastly's solutions for [video on demand](https://www.fastly.com/products/streaming-media/video-on-demand) and streaming media into Next.js.
### Open source `next-video` component
- Provides a `<Video>` component for Next.js, compatible with various hosting services including Vercel Blob, S3, Backblaze, and Mux.
- [Detailed documentation](https://next-video.dev/docs) for using `next-video.dev` with different hosting services.
### Cloudinary Integration
- Official [documentation and integration guides](https://support.cloudinary.com/hc/en-us/categories/200477762-Client-Libraries-and-Integration-Guides) for using Cloudinary with Next.js.
- A [specific guide](https://next.cloudinary.dev/nextjs-13) on integrating Cloudinary with Next.js.
### Mux Video API
- Mux provides a [starter template](https://github.com/muxinc/video-course-starter-kit) for creating a video course with Mux and Next.js.
- Learn about Mux's recommendations for embedding [high-performance video for your Next.js application](https://www.mux.com/for/nextjs).
- Explore an [example project](https://with-mux-video.vercel.app/) demonstrating Mux with Next.js.
### Fastly
- Learn more about integrating Fastly's solutions for [video on demand](https://www.fastly.com/products/streaming-media/video-on-demand) and streaming media into Next.js.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment