Vue中使用video.js實(shí)現(xiàn)截圖和視頻錄制與下載
一、視頻播放
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中添加視頻代碼
<template> <video id="myVideoId" class="video-js vjs-big-play-centered vjs-fluid" controls preload="auto" width="100%" height="100%" > <source type="application/x-mpegURL" :src="playURL" /> </video> </template> export default { data() { return { transcribeStr: "錄 像", myPlayer: null, // 視頻播放地址 playURL: "", }; }, mounted() { this.initVideo(); }, watch: {}, methods: { initVideo() { //此處初始化的調(diào)用,我放在了獲取數(shù)據(jù)之后的方法內(nèi),而不是放在鉤子函數(shù)mounted //頁面dom元素渲染完畢,執(zhí)行回調(diào)里面的方法 this.$nextTick(() => { this.myPlayer = this.$video(document.getElementById("myVideoId"), { //確定播放器是否具有用戶可以與之交互的控件。沒有控件,啟動(dòng)視頻播放的唯一方法是使用autoplay屬性或通過Player API。 controls: true, //自動(dòng)播放屬性,muted:靜音播放 autoplay: true, //建議瀏覽器是否應(yīng)在<video>加載元素后立即開始下載視頻數(shù)據(jù)。 preload: "auto", //設(shè)置視頻播放器的顯示寬度(以像素為單位) // width: "800px", //設(shè)置視頻播放器的顯示高度(以像素為單位) // height: "400px", controlBar: { playToggle: true } }); }); } }, beforeDestroy() { // 組件銷毀時(shí),清除播放器 if (this.myPlayer) this.myPlayer.dispose(); } };
二、視頻錄制和下載
1. 安裝錄制所需插件
npm i recordrtc
2.引用(main.js)
import RecordRTC from "recordrtc"; Vue.prototype.$recordRTC = RecordRTC;
3.vue中添加視頻錄制和下載代碼(本功能基于上面代碼)
<div @click="transcribe"> <i class="record-procees" v-if="isRecorder"></i> {{ transcribeStr }} </div> // 錄制 transcribe() { this.isRecorder = !this.isRecorder; if (this.isRecorder) { this.transcribeStr = "結(jié) 束"; if (!this.canvas) this.canvas = document.createElement("canvas"); this.recorder = this.$recordRTC(this.canvas, { type: "canvas" }); this.recorder.startRecording(); this.drawMedia(); } else { this.transcribeStr = "錄 像"; this.recorder.stopRecording(() => { const url = window.URL.createObjectURL(this.recorder.getBlob()); this.downloadFile(url, "mp4"); cancelAnimationFrame(this.animationFrame); this.canvas = null; this.animationFrame = null; }); } }, // 刷新canvas drawMedia() { const ctx = this.canvas.getContext("2d"); // 找到需要截圖的video標(biāo)簽 const video = this.myPlayer.el().querySelector("video"); this.canvas.setAttribute("width", video.videoWidth); this.canvas.setAttribute("height", video.videoHeight); ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight); // requestAnimationFrame 根據(jù)電腦顯示幀數(shù)進(jìn)行循環(huán) this.animationFrame = requestAnimationFrame(() => this.drawMedia()); }, // 下載 downloadFile: function(blob, fileType) { const a = document.createElement("a"); a.style.display = "none"; a.href = blob; // const time = this.parseTime(new Date()) const time = new Date().getTime(); a.download = `${time}.${fileType}`; document.body.appendChild(a); a.click(); setTimeout(function() { document.body.removeChild(a); window.URL.revokeObjectURL(blob); }, 1000); }, <style> .record-procees { display: inline-block; height: 10px; width: 10px; margin-top: 12px; margin-right: 6px; background: red; border-radius: 8px; animation: blings 1s linear infinite; } @keyframes blings { 0% { opacity: 0.1; } 100% { opacity: 1; } } </style>
三、截圖
<li @click="screenshotHandle">截圖</li> screenshotHandle() { const fileType = "png"; // 找到需要截圖的video標(biāo)簽 // video 實(shí)列 const video = this.myPlayer.el().querySelector("video"); // const video = this.video; console.log(video, "video"); const canvas = document.createElement("canvas"); canvas.width = video.videoWidth; canvas.height = video.videoHeight; console.log(canvas, "canvas"); canvas .getContext("2d") .drawImage(video, 0, 0, canvas.width, canvas.height); // 圖片大小和視頻分辨率一致 const strDataURL = canvas.toDataURL("image/" + fileType); // canvas中video中取一幀圖片并轉(zhuǎn)成dataURL let arr = strDataURL.split(","), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } const blob = new Blob([u8arr], { type: mime }); const url = window.URL.createObjectURL(blob); this.downloadFile(url, "png"); },
到此這篇關(guān)于Vue中使用video.js實(shí)現(xiàn)截圖和視頻錄制與下載的文章就介紹到這了,更多相關(guān)Vue video.js截圖和視頻錄制與下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3之父子組件異步props數(shù)據(jù)的傳值方式
這篇文章主要介紹了Vue3之父子組件異步props數(shù)據(jù)的傳值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04Vue實(shí)現(xiàn)動(dòng)態(tài)添加或者刪除對(duì)象和對(duì)象數(shù)組的操作方法
這篇文章主要介紹了在Vue項(xiàng)目中實(shí)現(xiàn)動(dòng)態(tài)添加或者刪除對(duì)象和對(duì)象數(shù)組的操作方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09關(guān)于vue-router的beforeEach無限循環(huán)的問題解決
本篇文章主要介紹了關(guān)于vue-router的beforeEach無限循環(huán)的問題解決,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09vue video和vue-video-player實(shí)現(xiàn)視頻鋪滿教程
這篇文章主要介紹了vue video和vue-video-player實(shí)現(xiàn)視頻鋪滿教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10el-table 選擇框根據(jù)條件設(shè)置某項(xiàng)不可選中的操作代碼
這篇文章主要介紹了el-table 選擇框根據(jù)條件設(shè)置某項(xiàng)不可選中的操作代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03vue-cli-service build 環(huán)境設(shè)置方式
這篇文章主要介紹了vue-cli-service build 環(huán)境設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-01-01vue自定義指令實(shí)現(xiàn)一鍵復(fù)制功能
本文旨在記錄解決在工作中一鍵復(fù)制功能得需求,本文主要使用了Vue3+TypeScript+Ant-Design-Vue,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11Vue?+?Axios?請(qǐng)求接口方法與傳參方式詳解
使用Vue的腳手架搭建的前端項(xiàng)目,通常都使用Axios封裝的接口請(qǐng)求,項(xiàng)目中引入的方式不做多介紹,本文主要介紹接口調(diào)用與不同形式的傳參方法。對(duì)Vue?+?Axios?請(qǐng)求接口方法與傳參問題感興趣的朋友一起看看吧2021-12-12