<div class="w-[100%] relative" :style="{'height':props.menu?'70%':'100%'}">
<video :id="'myVideo'+props.index" class="video-js vjs-default-skin vjs-big-play-centered" autoplay muted :controls="props.menu"
width="100%" >
<source type="application/x-mpegURL" :src="videoSrc" />
</video>
<div v-if="isShow" class="text-[red] absolute top-[10px] left-[50%] translate-x-[-50%] flex items-center">
<div
class="rounded-[50%] w-[16px] h-[16px] p-[4px] leading-[16px] mr-[10px] mt-[-4px] border-[1px] text-[red] cursor-pointer">
<div class="w-[6px] h-[6px] rounded-[1px] bg-[red]"></div>
</div>
{{ recordTime }}
</div>
<div v-if="props.menu" class="h-[30px]">
<el-icon class="absolute bottom-[-24px] right-[13px] text-[#333] cursor-pointer" @click="getCurPic"
style="margin: 0.1em 0.1em 0 0" :size="17">
<IconifyIconOffline :icon="Camera" />
</el-icon>
<div @click="recordIng"
class="rounded-[50%] w-[16px] h-[16px] p-[4px] leading-[16px] border-[1px] absolute bottom-[-23px] right-[60px] text-[#333] cursor-pointer">
<div class="w-[6px] h-[6px] rounded-[50%] bg-[#333]"></div>
</div>
<div class="absolute bottom-[-26px] right-[100px] flex items-center">
<div class="text-[#333] text-[14px] mr-[5px]">字幕:</div>
<el-switch size="small" v-model="subtitleSwitch" />
</div>
</div>
</div>
import { onMounted, onBeforeUnmount, ref, nextTick, reactive } from "vue";
import { useShow } from "hook";
const {
recordIng,
getVideo,
playervideo,
getCurPic,
cropperModel,
subtitleSwitch,
canvasWidth,
canvasHeight,
recordTime,
isShow,
submit,
posArray,
videoSrc
} = useShow(props.index, props.menu);
const props = defineProps < {
menu: {
type: Boolean;
default: true;
};
index: {
type: Number;
default: 9;
};
} > ();
onBeforeUnmount(() => {
playervideo?.value?.dispose();
});
onMounted(() => {
getVideo();
});
import { ref, reactive, nextTick } from "vue";
import flvjs from "flv.js";
import videojs from "video.js";
export function useShow(index, menu) {
const cropperModel = ref(false);
const canvasWidth = ref();
const canvasHeight = ref();
const playervideo = ref();
const tempPos = ref([]);
const posArray = reactive([]);
const subtitleSwitch = ref(true);
const videoSrc = ref("http://192.168.3.160:28323/demo/stream-1/hls.m3u8");
const isShow = ref(false);
const timer = ref();
const recordTime = ref("00:00:00");
const hour = ref(0);
const minutes = ref(0);
const seconds = ref(0);
const submit = () => {
console.log(...posArray);
};
const history = [];
function Point(x, y, type) {
this.x = x;
this.y = y;
this.type = type;
}
const windowToCanvas = (e, mycanvas) => {
const rect = mycanvas.getBoundingClientRect();
return new Point(
e.clientX - rect.left * (mycanvas.width / rect.width),
e.clientY - rect.top * (mycanvas.height / rect.height),
e.which
);
};
const showLastHistory = (ctx, history) => {
ctx.putImageData(history[history.length - 1]["data"], 0, 0);
};
const addHistoy = (history, ctx, mycanvas) => {
history.push({
data: ctx.getImageData(0, 0, mycanvas.width, mycanvas.height)
});
};
const drawerRect = (ctx, left, top, w, h) => {
ctx.strokeStyle = "#00B42A";
ctx.lineWidth = "2";
ctx.save();
ctx.beginPath();
ctx.rect(left, top, w, h);
ctx.stroke();
ctx.restore();
return {
data: [left, top, w, h]
};
};
const startTimer = () => {
seconds.value += 1;
if (seconds.value >= 60) {
seconds.value = 0;
minutes.value = minutes.value + 1;
}
if (minutes.value >= 60) {
minutes.value = 0;
hour.value = hour.value + 1;
}
recordTime.value =
(hour.value < 10 ? "0" + hour.value : hour.value) +
":" +
(minutes.value < 10 ? "0" + minutes.value : minutes.value) +
":" +
(seconds.value < 10 ? "0" + seconds.value : seconds.value);
};
const recordIng = () => {
isShow.value = !isShow.value;
recordTime.value = "00:00:00";
hour.value = 0;
minutes.value = 0;
seconds.value = 0;
clearInterval(timer.value);
timer.value = setInterval(startTimer, 1000);
};
const canvasSign = (mycanvas, ctx) => {
mycanvas.onclick = null;
mycanvas.onmousedown = function (e) {
tempPos.value = [];
e.preventDefault();
const mousedown = windowToCanvas(e, mycanvas);
mycanvas.onmousemove = function (e) {
e.preventDefault();
showLastHistory(ctx, history);
const point = windowToCanvas(e, mycanvas);
const w = Math.abs(point.x - mousedown.x);
const h = Math.abs(point.y - mousedown.y);
const left = point.x > mousedown.x ? mousedown.x : point.x;
const top = point.y > mousedown.y ? mousedown.y : point.y;
const pos = drawerRect(ctx, left, top, w, h);
tempPos.value.push(pos);
};
mycanvas.onmouseup = function (e) {
e.preventDefault();
addHistoy(history, ctx, mycanvas);
mycanvas.onmousemove = null;
posArray.push(tempPos.value[tempPos.value.length - 1]);
console.log(posArray, "这是截取的图片坐标");
};
};
addHistoy(history, ctx, mycanvas);
};
const photograph = () => {
const img = document.getElementById("myVideo9");
const width = img.clientWidth;
const height = img.clientHeight;
canvasWidth.value = width;
canvasHeight.value = height;
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
nextTick(() => {
ctx.drawImage(img.childNodes[0], 0, 0, width, height);
canvasSign(canvas, ctx);
const img = new Image();
img.crossOrigin = "";
img.onload = function () {
ctx.drawImage(img, 0, 0, width, height);
canvasSign(canvas, ctx);
};
img.src = "https://avatars3.githubusercontent.com/u/496048?s=120&v=4";
});
};
const getCurPic = () => {
cropperModel.value = true;
nextTick(() => {
photograph();
});
};
const getVideo = () => {
playervideo.value = videojs("myVideo" + index, {
controls: menu,
bigPlayButton: menu,
textTrackDisplay: menu,
posterImage: menu,
errorDisplay: menu,
controlBar: {
playToggle: false,
progressControl: false,
volumePanel: menu,
pictureInPictureToggle: false,
remainingTimeDisplay: false,
fullscreenToggle: menu
},
userActions: {
click: false
},
poster:
"https://img2.baidu.com/it/u=289789983,1298381275&fm=253&fmt=auto&app=120&f=JPEG?w=863&h=500"
});
};
return {
posArray,
submit,
isShow,
recordTime,
getVideo,
playervideo,
getCurPic,
cropperModel,
recordIng,
subtitleSwitch,
videoSrc,
canvasWidth,
canvasHeight
};
}