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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue props實(shí)現(xiàn)父組件給子組件傳遞數(shù)據(jù)的方式
Vue中的配置項(xiàng)Props能讓組件接收外部傳遞過來的數(shù)據(jù),本文給大家介紹了Vue props實(shí)現(xiàn)父組件給子組件傳遞數(shù)據(jù)的幾種方式,文中有詳細(xì)的實(shí)現(xiàn)方式,具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10
vue項(xiàng)目實(shí)現(xiàn)對某個(gè)區(qū)域繪制水印
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目實(shí)現(xiàn)對某個(gè)區(qū)域繪制水印,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue3 reactive函數(shù)用法實(shí)戰(zhàn)教程
reactive是Vue3中提供實(shí)現(xiàn)響應(yīng)式數(shù)據(jù)的方法,reactive的用法與ref的用法相似,也是將數(shù)據(jù)變成響應(yīng)式數(shù)據(jù),當(dāng)數(shù)據(jù)發(fā)生變化時(shí)UI也會(huì)自動(dòng)更新,這篇文章主要介紹了vue3 reactive函數(shù)用法,需要的朋友可以參考下2022-11-11
使用elementuiadmin去掉默認(rèn)mock權(quán)限控制的設(shè)置
這篇文章主要介紹了使用elementuiadmin去掉默認(rèn)mock權(quán)限控制的設(shè)置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

