• vue中使用video.js,且可以截图、录制和下载视频


    一、视频播放

    1. 安装video.js

    // video.js
    npm install video.js

    2. 引用(main.js)

    import videojs from "video.js";

    import "video.js/dist/video-js.css";

    Vue.prototype.$video = videojs;

    3.vue中添加视频代码

    1. <template>
    2. <video
    3. id="myVideoId"
    4. class="video-js vjs-big-play-centered vjs-fluid"
    5. controls
    6. preload="auto"
    7. width="100%"
    8. height="100%"
    9. >
    10. <source type="application/x-mpegURL" :src="playURL" />
    11. </video>
    12. </template>
    1. export default {
    2. data() {
    3. return {
    4. transcribeStr: "录 像",
    5. myPlayer: null,
    6. // 视频播放地址
    7. playURL: ""
    8. };
    9. },
    10. mounted() {
    11. this.initVideo();
    12. },
    13. watch: {},
    14. methods: {
    15. initVideo() {
    16. //此处初始化的调用,我放在了获取数据之后的方法内,而不是放在钩子函数mounted
    17. //页面dom元素渲染完毕,执行回调里面的方法
    18. this.$nextTick(() => {
    19. this.myPlayer = this.$video(document.getElementById("myVideoId"), {
    20. //确定播放器是否具有用户可以与之交互的控件。没有控件,启动视频播放的唯一方法是使用autoplay属性或通过Player API。
    21. controls: true,
    22. //自动播放属性,muted:静音播放
    23. autoplay: true,
    24. //建议浏览器是否应在<video>加载元素后立即开始下载视频数据。
    25. preload: "auto",
    26. //设置视频播放器的显示宽度(以像素为单位)
    27. // width: "800px",
    28. //设置视频播放器的显示高度(以像素为单位)
    29. // height: "400px",
    30. controlBar: {
    31. playToggle: true
    32. }
    33. });
    34. });
    35. }
    36. },
    37. beforeDestroy() {
    38. // 组件销毁时,清除播放器
    39. if (this.myPlayer) this.myPlayer.dispose();
    40. }
    41. };

    二、视频录制和下载

    1. 安装录制所需插件

    npm i recordrtc

    2.引用(main.js)

    import RecordRTC from "recordrtc";

    Vue.prototype.$recordRTC = RecordRTC;

    3.vue中添加视频录制和下载代码(本功能基于上面代码)

    1. <div @click="transcribe">
    2. <i class="record-procees" v-if="isRecorder"></i>
    3. {{ transcribeStr }}
    4. </div>
    1. // 录制
    2. transcribe() {
    3. this.isRecorder = !this.isRecorder;
    4. if (this.isRecorder) {
    5. this.transcribeStr = "结 束";
    6. if (!this.canvas) this.canvas = document.createElement("canvas");
    7. this.recorder = this.$recordRTC(this.canvas, {
    8. type: "canvas"
    9. });
    10. this.recorder.startRecording();
    11. this.drawMedia();
    12. } else {
    13. this.transcribeStr = "录 像";
    14. this.recorder.stopRecording(() => {
    15. const url = window.URL.createObjectURL(this.recorder.getBlob());
    16. this.downloadFile(url, "mp4");
    17. cancelAnimationFrame(this.animationFrame);
    18. this.canvas = null;
    19. this.animationFrame = null;
    20. });
    21. }
    22. },
    23. // 刷新canvas
    24. drawMedia() {
    25. const ctx = this.canvas.getContext("2d");
    26. // 找到需要截图的video标签
    27. const video = this.myPlayer.el().querySelector("video");
    28. this.canvas.setAttribute("width", video.videoWidth);
    29. this.canvas.setAttribute("height", video.videoHeight);
    30. ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
    31. // requestAnimationFrame 根据电脑显示帧数进行循环
    32. this.animationFrame = requestAnimationFrame(() => this.drawMedia());
    33. },
    34. // 下载
    35. downloadFile: function(blob, fileType) {
    36. const a = document.createElement("a");
    37. a.style.display = "none";
    38. a.href = blob;
    39. // const time = this.parseTime(new Date())
    40. const time = new Date().getTime();
    41. a.download = `${time}.${fileType}`;
    42. document.body.appendChild(a);
    43. a.click();
    44. setTimeout(function() {
    45. document.body.removeChild(a);
    46. window.URL.revokeObjectURL(blob);
    47. }, 1000);
    48. },
    1. <style>
    2. .record-procees {
    3. display: inline-block;
    4. height: 10px;
    5. width: 10px;
    6. margin-top: 12px;
    7. margin-right: 6px;
    8. background: red;
    9. border-radius: 8px;
    10. animation: blings 1s linear infinite;
    11. }
    12. @keyframes blings {
    13. 0% {
    14. opacity: 0.1;
    15. }
    16. 100% {
    17. opacity: 1;
    18. }
    19. }
    20. style>

    三、截图

    <li @click="screenshotHandle">截图</li>
    1. screenshotHandle() {
    2. const fileType = "png";
    3. // 找到需要截图的video标签
    4. // video 实列
    5. const video = this.myPlayer.el().querySelector("video");
    6. // const video = this.video;
    7. console.log(video, "video");
    8. const canvas = document.createElement("canvas");
    9. canvas.width = video.videoWidth;
    10. canvas.height = video.videoHeight;
    11. console.log(canvas, "canvas");
    12. canvas
    13. .getContext("2d")
    14. .drawImage(video, 0, 0, canvas.width, canvas.height); // 图片大小和视频分辨率一致
    15. const strDataURL = canvas.toDataURL("image/" + fileType); // canvas中video中取一帧图片并转成dataURL
    16. let arr = strDataURL.split(","),
    17. mime = arr[0].match(/:(.*?);/)[1],
    18. bstr = atob(arr[1]),
    19. n = bstr.length,
    20. u8arr = new Uint8Array(n);
    21. while (n--) {
    22. u8arr[n] = bstr.charCodeAt(n);
    23. }
    24. const blob = new Blob([u8arr], {
    25. type: mime
    26. });
    27. const url = window.URL.createObjectURL(blob);
    28. this.downloadFile(url, "png");
    29. },

  • 相关阅读:
    基于知识蒸馏的夜间低照度图像增强及目标检测
    6G网络需求、架构及技术趋势
    【微机接口】可编程串行异步通信芯片8250
    Java泛型简介、定义和使用含有泛型的类、定义和使用含有泛型的方法、定义和使用含有泛型的接口、泛型通配符?、受限泛型
    Fluent计算出现“Floating point exception”时的解决办法
    小脑萎缩患者平时生活中应该注意哪些?
    ASRT从零搭建并测试
    蓝桥杯1039
    【云原生 · DevOps】DevOps 解决方案
    抗CD4单抗偶联表阿霉素/单克隆抗体Zh805/特异性靶向肽A54偶联阿霉素的制备
  • 原文地址:https://blog.csdn.net/weixin_44042792/article/details/136681121