/    Sign up×
Bounties /Pin to ProfileBookmark

Integrating video.js with Astro

+ 5,000
Copy linkTweet thisAlerts:
Oct 09.2023

I’m familiar with using the open-source HTML5 player framework, video.js, for non-Astro projects. But, I’m having trouble getting it to work with Astro after trying both CDN and npm installations. Has anyone faced and resolved this?

to post a answer
Front-endJavaScript

1 Replies

Davinci•003 auto-replyAstro is a modern front-end framework for building fast, optimized websites and applications. Video.js is a web video player built from the ground up for an HTML5 world. Here's how you can integrate Video.js with Astro:

First, install Video.js via npm:

``bash
npm install video.js
`

Then, in your Astro component, you can import Video.js and use it in the
onMount function. Here's an example:

`jsx
---
import { onMount } from 'astro/client';
import videojs from 'video.js';
import 'video.js/dist/video-js.css';
---

<video
id="my-video"
class="video-js"
controls
preload="auto"
width="640"
height="264"
data-setup="{}"
>
<source src="MY_VIDEO_SOURCE.mp4" type="video/mp4" />

To view this video please enable JavaScript, and consider upgrading to a
web browser that
<a href="https://videojs.com/html5-video-support/" target="_blank"
>supports HTML5 video</a
>

</video>

<script>
onMount(() => {
const player = videojs('my-video');
return () => {
// This is important to do when your component unmounts
player.dispose();
};
});
</script>
`

In this example, we're using the
onMount function from astro/client` to initialize Video.js when the component mounts. We're also cleaning up (disposing) the player when the component unmounts to prevent memory leaks.

Remember to replace "MY_VIDEO_SOURCE.mp4" with the actual path to your video file.

Was this helpful?

×

Success!

Help @cosmic spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...