当前位置:首页 > 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 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。 使…

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue实现pc

vue实现pc

Vue 实现 PC 端应用开发 Vue.js 是一个流行的前端框架,适用于构建 PC 端 Web 应用。以下是关键步骤和最佳实践: 项目初始化 使用 Vue CLI 或 Vite 创建项目:…

vue 实现图钉

vue 实现图钉

实现图钉效果的步骤 在Vue中实现图钉(固定位置)效果,可以通过CSS的position: sticky属性或监听滚动事件动态修改元素样式。以下是两种常见方法: 方法一:使用CSS的posi…