File size: 2,653 Bytes
c5a2609
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// 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
}