更新
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
/** 从视频通话区域截取当前最大的一路 video 帧(WebRTC 画面) */
|
||||
export async function captureVideoFrameFromElement(
|
||||
container: HTMLElement
|
||||
): Promise<Blob | null> {
|
||||
const videos = Array.from(container.querySelectorAll('video')) as HTMLVideoElement[]
|
||||
let best: HTMLVideoElement | null = null
|
||||
let bestArea = 0
|
||||
for (const v of videos) {
|
||||
if (v.readyState < 2) continue
|
||||
const vw = v.videoWidth || v.clientWidth
|
||||
const vh = v.videoHeight || v.clientHeight
|
||||
const area = vw * vh
|
||||
if (area > bestArea) {
|
||||
bestArea = area
|
||||
best = v
|
||||
}
|
||||
}
|
||||
if (!best) return null
|
||||
const vw = best.videoWidth || best.clientWidth
|
||||
const vh = best.videoHeight || best.clientHeight
|
||||
if (!vw || !vh) return null
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = vw
|
||||
canvas.height = vh
|
||||
const ctx = canvas.getContext('2d')
|
||||
if (!ctx) return null
|
||||
try {
|
||||
ctx.drawImage(best, 0, 0, vw, vh)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
canvas.toBlob((b) => resolve(b), 'image/jpeg', 0.92)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user