vue3 播放 m3u8、RTSP视频

本文详细介绍了如何在Vue.js项目中使用video.js处理m3u8视频流和通过websocket与ffmpeg实现实时RTSP流的播放,包括安装依赖、配置代码示例以及与flv.js的整合,提供完整的开发流程和测试资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为项目中会遇到播放不同视频流的情况,这里聚集了博主遇到的情况,合集整理给大家。

1.m3u8的视频流,使用vedio.js

(1).先npm i video.js

(2).引用vedio.js并创建实例,进行资源配置

具体代码如下

<template>
  <div class="monitors">
    <div class="myVedioBox">
      <video
        class="video-js vjs-default-skin"
        ref="videoPlay1"
        muted
        :autoplay="true"
        :controls="true"
        style="object-fit: fill; width: 100%; height: 100%"
      >
        <source src="" type="application/x-mpegURL" />
      </video>
    </div>
  </div>
</template>
<script setup>
import { nextTick, onMounted, reactive } from "vue";
import videojs from "video.js";
import "video.js/dist/video-js.min.css";

const data = reactive({});
const videoPlay1 = ref
### 在 Vue3 中集成 RTSP 流媒体播放器 为了实现在 Vue3 项目中播放 RTSP 流,通常的做法不是直接处理 RTSP 协议,因为大多数现代浏览器不支持原生解码 RTSP 流。相反,推荐的方法是使用中间件服务器将 RTSP 转换成更友好的格式如 HLS 或 WebRTC,再利用前端库来解析这些流。 一种常见的方案是采用 FFmpeg 或 GStreamer 这样的工具作为转码服务端[^1]。这类软件可以接收来自 IP 摄像头或其他设备的 RTSP 输入并将其重新打包为 HTTP Live Streaming (HLS) 输出。对于客户端部分,在 Vue3 应用里可以通过 `video.js` 和其插件 `videojs-contrib-hls` 来加载和渲染 HLS 格式的直播流。 下面是一个简单的例子展示如何设置: #### 安装依赖项 首先安装必要的 npm 包: ```bash npm install video.js videojs-contrib-hls vue-video-player --save ``` #### 创建 VideoPlayer 组件 创建一个新的组件用于封装视频播放逻辑: ```vue <template> <div class="player-wrapper"> <video ref="videoPlayer" class="video-js vjs-default-skin"></video> </div> </template> <script setup> import { onMounted, ref } from &#39;vue&#39;; import videojs from &#39;video.js&#39;; import &#39;video.js/dist/video-js.css&#39;; // Import the plugin and register it with Video.js. import videoJsHls from &#39;videojs-contrib-hls&#39;; videojs.registerTech(&#39;VideoJsHls&#39;, videoJsHls); const props = defineProps({ options: { type: Object, default() { return {}; } } }); let player; onMounted(() => { // Initialize a new Player instance after component mounted. player = videojs( this.$refs.videoPlayer, props.options, function onPlayerReady() { console.log(&#39;Your player is ready!&#39;); } ); // Destroying the player when the component unmounts to prevent memory leaks. }); </script> ``` #### 使用 VideoPlayer 组件 最后,在页面上实例化这个自定义组件,并传递适当配置参数给它: ```html <template> <!-- Other template code --> <VideoPlayer :options="{ sources: [{ src: &#39;http://your-stream-url.com/playlist.m3u8&#39;, type: &#39;application/x-mpegURL&#39; }] }"/> <!-- More template content --> </template> <script setup> import VideoPlayer from &#39;./components/VideoPlayer.vue&#39; </script> ``` 这样就完成了一个基本的功能实现。需要注意的是实际部署时还需要考虑跨域资源共享(CORS),以及确保服务器能够稳定提供所需的数据传输速率等问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值