当前位置:首页 > 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,需要转码服务:

后端使用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播放:

<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>

添加时间轴回放功能

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

vue实现监控视频播放

<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 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue datepicker 实现

vue datepicker 实现

实现 Vue Datepicker 的基本方法 在 Vue 项目中实现日期选择功能,可以使用第三方库如 vue-datepicker 或 v-calendar。以下是两种常见实现方式: 安装 vue…

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <template>…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

vue router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…