当前位置:首页 > VUE

vue实现监控视频播放

2026-01-21 07:50:05VUE

Vue 实现监控视频播放方案

使用 video.js 实现基础播放

安装 video.js 和对应的 Vue 适配器:

npm install video.js @videojs-player/vue

创建视频播放组件:

<template>
  <video-player
    src="监控视频地址"
    controls
    autoplay
    :options="playerOptions"
  />
</template>

<script>
import { defineComponent } from 'vue'
import { VideoPlayer } from '@videojs-player/vue'

export default defineComponent({
  components: { VideoPlayer },
  setup() {
    const playerOptions = {
      fluid: true,
      aspectRatio: '16:9',
      techOrder: ['html5'],
      sources: [{
        src: '监控视频地址',
        type: 'application/x-mpegURL' // 适配HLS流
      }]
    }
    return { playerOptions }
  }
})
</script>

实现RTSP流播放

由于浏览器不支持直接播放RTSP,需要转码服务:

vue实现监控视频播放

后端使用FFmpeg转码RTSP为HLS:

ffmpeg -i rtsp://监控地址 -c copy -f hls -hls_time 2 -hls_list_size 3 -hls_flags delete_segments stream.m3u8

前端通过HLS.js播放:

vue实现监控视频播放

<template>
  <video ref="videoEl" controls></video>
</template>

<script>
import Hls from 'hls.js'

export default {
  mounted() {
    const video = this.$refs.videoEl
    if (Hls.isSupported()) {
      const hls = new Hls()
      hls.loadSource('http://转码服务器地址/stream.m3u8')
      hls.attachMedia(video)
      hls.on(Hls.Events.MANIFEST_PARSED, () => {
        video.play()
      })
    } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
      video.src = 'http://转码服务器地址/stream.m3u8'
      video.addEventListener('loadedmetadata', () => {
        video.play()
      })
    }
  }
}
</script>

实现多画面监控

使用CSS Grid布局多个视频源:

<template>
  <div class="video-grid">
    <video-player 
      v-for="(src, index) in videoSources" 
      :key="index"
      :src="src"
    />
  </div>
</template>

<style>
.video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 10px;
}
</style>

添加时间轴回放功能

对于存储的监控录像,实现时间选择:

<template>
  <input 
    type="datetime-local" 
    v-model="selectedTime"
    @change="changeVideoTime"
  />
</template>

<script>
export default {
  methods: {
    changeVideoTime() {
      const timestamp = new Date(this.selectedTime).getTime()
      // 调用API获取对应时间段的视频
      fetchVideoByTimestamp(timestamp)
    }
  }
}
</script>

性能优化建议

  • 使用Web Worker处理视频分析
  • 对非活动标签页降低视频帧率
  • 实现动态码率切换
  • 添加loading状态和错误处理

完整示例组件

<template>
  <div class="surveillance-container">
    <div class="toolbar">
      <button @click="toggleFullscreen">全屏</button>
      <input type="datetime-local" v-model="playbackTime">
    </div>

    <div class="video-wrapper" ref="videoContainer">
      <video-player
        ref="player"
        :options="playerOptions"
        @ready="handlePlayerReady"
      />
    </div>
  </div>
</template>

<script>
import { VideoPlayer } from '@videojs-player/vue'
import Hls from 'hls.js'

export default {
  components: { VideoPlayer },
  data() {
    return {
      playerOptions: {
        autoplay: true,
        controls: true,
        sources: [{
          src: '',
          type: 'application/x-mpegURL'
        }]
      },
      playbackTime: null,
      hls: null
    }
  },
  methods: {
    handlePlayerReady(player) {
      this.videoPlayer = player
      this.initHlsPlayer()
    },
    initHlsPlayer() {
      if (Hls.isSupported()) {
        this.hls = new Hls()
        this.hls.loadSource(this.playerOptions.sources[0].src)
        this.hls.attachMedia(this.videoPlayer.tech().el())
      }
    },
    toggleFullscreen() {
      if (!document.fullscreenElement) {
        this.$refs.videoContainer.requestFullscreen()
      } else {
        document.exitFullscreen()
      }
    }
  }
}
</script>

标签: 视频播放vue
分享给朋友:

相关文章

vue实现滚动中断

vue实现滚动中断

Vue 实现滚动中断的方法 在 Vue 中实现滚动中断通常涉及监听滚动事件,并在特定条件下阻止默认行为或停止滚动。以下是几种常见方法: 监听滚动事件并阻止默认行为 通过 @scroll 或 @wh…

vue实现飘窗原理

vue实现飘窗原理

vue实现飘窗原理 飘窗效果通常指在页面上悬浮显示的弹窗或通知,跟随页面滚动或固定在某个位置。Vue实现这一效果主要依赖组件化开发、CSS定位和动态数据绑定。 组件结构设计 创建一个独立的Vue组件…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 ECh…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…

vue实现单点

vue实现单点

Vue 实现单点登录(SSO)方案 单点登录(SSO)允许用户在多个相关但独立的系统中使用同一组凭证登录。以下是基于 Vue 的实现方案: 基于 OAuth2/OpenID Connect 的实现…

vue实现轮询

vue实现轮询

实现轮询的基本方法 在Vue中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: data…