当前位置:首页 > 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中实现表格的多行编辑功能,可以通过以下方法完成。这里以Element UI的表格组件为例,但思路适用于其他UI库或原生实现。 数据准备 定义表格数据和编辑状态管理变量:…

vue实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 297…

vue实现视窗

vue实现视窗

Vue 实现视窗功能 在 Vue 中实现视窗功能通常涉及监听浏览器窗口大小变化、响应式布局或创建自定义弹窗组件。以下是几种常见实现方式: 监听浏览器窗口大小变化 使用 Vue 的 mounted…

vue实现分屏

vue实现分屏

Vue 实现分屏的方法 分屏功能通常用于展示多个独立内容区域,以下是几种常见的实现方式: 使用 CSS Grid 布局 通过 CSS Grid 可以轻松创建分屏布局,在 Vue 组件中直接应用样式…

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(…

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式,…