Slide 23
Slide 23 text
import fs from "fs"
import {spawn} from "child_process"
import canvasToBuffer from 'electron-canvas-to-buffer'
window.addEventListener('DOMContentLoaded', () => {
const FRAMERATE = 30
const VIDEO_DURATION_SEC = 30
const OUTPUT_FRAMES = VIDEO_DURATION_SEC * 30
const cRand = () => ((Math.random() * 256) | 0).toString(16)
const canvas = document.querySelector(‘#canvas’)
Object.assign(canvas, {width: 640, height: 360})
const ctx = canvas.getContext('2d')
const encoder = spawn('ffmpeg', `-i pipe:0 -framerate ${FRAMERATE} -c:v png_pipe -c:v libx264
-r ${OUTPUT_FRAMES} -s 640x360 test.mp4`.split(' '))
for (let i = 0; i < OUTPUT_FRAMES i++) {
ctx.fillStyle = '#' + [cRand(), cRand(), cRand()].join('')
ctx.fillRect(0, 0, 640, 360)
let buffer = canvasToBuffer(canvas, 'image/png')
encoder.stdin.write(buffer)
}
encoder.stdin.end()
})