video / src /jiangnan /export /verify.ts
Surf
暮归江南 · v3 步骤5:闪黑修复——播放器改尺寸不再留空帧、辉光前 NaN 卫兵、编码器自动/兼容选项、导出后回放自检并自动改用兼容编码重做
c5a2609
Raw History Blame Contribute Delete
2.65 kB
// Playback self-check of an exported MP4: the finished file is decoded by the browser's own
// video decoder (a <video> element) and sampled at many time points — every keyframe position
// plus the midpoints — so that an encoder / muxer problem that makes frames come out black
// in a player is caught here, not after the file was uploaded somewhere.
import { fadeBlack } from '../engine/timeline'
export interface VerifyResult {
sampled: number
blackAt: number[]
/** could not decode / seek at all (e.g. the browser cannot play the file) */
failed: string | null
}
export async function verifyMp4(blob: Blob, duration: number, keyInterval: number, onProgress?: (p: number) => void): Promise<VerifyResult> {
const url = URL.createObjectURL(blob)
const v = document.createElement('video')
v.muted = true
v.playsInline = true
v.preload = 'auto'
v.src = url
const c = document.createElement('canvas')
c.width = 32
c.height = 18
const g = c.getContext('2d', { willReadFrequently: true })!
const res: VerifyResult = { sampled: 0, blackAt: [], failed: null }
const once = (ev: string, timeout = 8000) => new Promise<void>((ok, bad) => {
const to = setTimeout(() => bad(new Error(`等待 ${ev} 超时`)), timeout)
v.addEventListener(ev, () => { clearTimeout(to); ok() }, { once: true })
v.addEventListener('error', () => { clearTimeout(to); bad(new Error('浏览器无法解码该文件')) }, { once: true })
})
try {
await once('loadeddata', 15000)
// keyframe positions, then the midpoints between them, then a few extra early frames
const pts: number[] = []
for (let t = 0; t < duration; t += keyInterval) pts.push(t + 0.02, t + keyInterval / 2)
for (const t of [0.5, 1.0, 1.5, duration - 2, duration - 1.5]) pts.push(t)
const list = [...new Set(pts.map((t) => Math.round(t * 100) / 100))].filter((t) => t >= 0 && t < duration - 0.05).sort((a, b) => a - b)
for (let i = 0; i < list.length; i++) {
const t = list[i]
v.currentTime = t
await once('seeked')
g.fillStyle = '#000'
g.fillRect(0, 0, 32, 18)
g.drawImage(v, 0, 0, 32, 18)
const d = g.getImageData(0, 0, 32, 18).data
let mx = 0
for (let k = 0; k < d.length; k += 4) {
const l = 0.2126 * d[k] + 0.7152 * d[k + 1] + 0.0722 * d[k + 2]
if (l > mx) mx = l
}
res.sampled++
if (mx < 6 && fadeBlack(t) < 0.97) res.blackAt.push(t)
onProgress?.((i + 1) / list.length)
}
} catch (e) {
res.failed = (e as Error).message
} finally {
v.removeAttribute('src')
v.load()
URL.revokeObjectURL(url)
}
return res
}