当前位置:   article > 正文

vue3 播放 m3u8、RTSP视频_vue3 播放视频流

vue3 播放视频流

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

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

(1).先npm i video.js

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

具体代码如下

  1. <template>
  2. <div class="monitors">
  3. <div class="myVedioBox">
  4. <video
  5. class="video-js vjs-default-skin"
  6. ref="videoPlay1"
  7. muted
  8. :autoplay="true"
  9. :controls="true"
  10. style="object-fit: fill; width: 100%; height: 100%"
  11. >
  12. <source src="" type="application/x-mpegURL" />
  13. </video>
  14. </div>
  15. </div>
  16. </template>
  17. <script setup>
  18. import { nextTick, onMounted, reactive } from "vue";
  19. import videojs from "video.js";
  20. import "video.js/dist/video-js.min.css";
  21. const data = reactive({});
  22. const videoPlay1 = ref();
  23. let Player1 = null;
  24. const VideoPlay = () => {
  25. nextTick(()=>{
  26. Player1 = videojs(videoPlay1.value, {
  27. sources: [
  28. {
  29. src: "https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8",
  30. type: "application/x-mpegURL",
  31. },
  32. ],
  33. });
  34. })
  35. };
  36. onMounted(() => {
  37. VideoPlay();
  38. });
  39. </script>
  40. <style scoped lang='less'>
  41. * {
  42. box-sizing: border-box;
  43. }
  44. .monitors {
  45. width: 100%;
  46. height: 100%;
  47. background: #ebf1f6;
  48. border-radius: 8px 8px 8px 8px;
  49. border: 1px solid #95def7;
  50. padding: 15px 13px;
  51. .myVedioBox {
  52. width: 100%;
  53. height: 100%;
  54. }
  55. }
  56. </style>

实现效果

2.RTSP的视频流

这里需要一个websocket服务,博主是用nodejs搭的本地服务,还需要使用ffmpeg转码,不用着急,下面都会提到.此方法会比较耗资源.

(1).创建Node项目搭建websocket服务

这里需要已经下载了node,博主就不说下载node的步骤了,需要的可以评论博主或者搜索其他文章。

(a).新建一个文件夹用vscode或者cmd打开

(b).在终端中输入npm init初始化一个项目,这个时候应该是会多一个package.json文件

(c).接下来下载后面会使用到的库

npm  i fluent-ffmpeg ws websocket-stream @ffmpeg-installer/ffmpeg

(d).创建一个index.js文件,代码如下

  1. const WebSocket =require ('ws')
  2. const ffmpegPath = require('@ffmpeg-installer/ffmpeg')
  3. const webSocketStream =require('websocket-stream/stream')
  4. const ffmpeg = require('fluent-ffmpeg')
  5. ffmpeg.setFfmpegPath(ffmpegPath.path);
  6. const wbs = new WebSocket.Server({ port: 8888, perMessageDeflate: false })
  7. wbs.on('connection', handleConnection)
  8. function handleConnection (ws, req) {
  9. console.log("已连接")
  10. const url = req.url.slice(1)
  11. const stream = webSocketStream(ws, { binary: true })
  12. const ffmpegCommand = ffmpeg(url)
  13. .addInputOption('-analyzeduration', '100000', '-max_delay', '1000000')
  14. .on('start', function () { console.log('Stream started.') })
  15. .on('codecData', function () { console.log('Stream codecData.') })
  16. .on('error', function (err) {
  17. console.log('An error occured: ', err.message)
  18. stream.end()
  19. })
  20. .on('end', function () {
  21. console.log('Stream end!')
  22. stream.end()
  23. })
  24. .outputFormat('flv').videoCodec('copy').noAudio()
  25. stream.on('close', function () {
  26. ffmpegCommand.kill('SIGKILL')
  27. })
  28. try {
  29. // 执行命令 传输到实例流中返回给客户端
  30. ffmpegCommand.pipe(stream)
  31. console.log("执行命令 传输到实例流中返回给客户端")
  32. } catch (error) {
  33. console.log(error)
  34. }
  35. }

(e).node index.js 启动服务

(2).vue3连接服务

前端部分只使用了flv.js,先npm i flv.js下载.

然后直接上代码

  1. <template>
  2. <div class="wrap">
  3. <video
  4. id="videoElement"
  5. class="video"
  6. muted
  7. autoplay
  8. controls
  9. ref="player"
  10. ></video>
  11. </div>
  12. </template>
  13. <script setup>
  14. import flvjs from "flv.js"; // 引入flvjs
  15. import { onMounted, onUnmounted, ref } from "vue";
  16. const player = ref(null);
  17. onMounted(() => {
  18. // 如果浏览器支持flvjs,则执行相应的程序
  19. if (flvjs.isSupported()) {
  20. let videoElement = document.getElementById("videoElement");
  21. // 准备监控设备流地址
  22. const url =
  23. "rtsp://rtspstream:013d56a9d7d97829d890679cb8a2e4d3@zephyr.rtsp.stream/movie";
  24. // 创建一个flvjs实例
  25. // 下面的ws://localhost:8888换成你搭建的websocket服务地址,后面加上设备流地址
  26. player.value = flvjs.createPlayer({
  27. type: "flv",
  28. isLive: true,
  29. url: "ws://localhost:8888/" + url,
  30. });
  31. player.value.on("error", (e) => {
  32. // console.log(e)
  33. });
  34. // 将实例挂载到video元素上面
  35. // player.attachMediaElement(this.$refs.player)
  36. player.value.attachMediaElement(videoElement);
  37. try {
  38. player.value.load();
  39. player.value.play();
  40. } catch (error) {
  41. // console.log(error)
  42. }
  43. }
  44. });
  45. onUnmounted(() => {
  46. player.value && player.value.pause(); //暂停播放数据流
  47. player.value && player.value.unload(); //取消数据流加载
  48. player.value && player.value.detachMediaElement(); //将播放实例从节点中取出
  49. player.value && player.value.destroy(); //销毁播放实例
  50. player.value = null;
  51. });
  52. </script>
  53. <style lang="scss" scoped>
  54. .wrap {
  55. width: 100%;
  56. height: 100%;
  57. .video {
  58. // width: 300px;
  59. // height: 300px;
  60. width: 100%;
  61. height: 100%;
  62. }
  63. }
  64. </style>

实现效果如下

如果需要拿来测试的rtsp视频流可以在https://rtsp.stream/网站中填写邮箱登录可获取免费的rtsp视频.

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号