HTML overlays on video: record, export, send
The picture below is one canvas. It draws a <div> that holds a video, a caption bar, a poll and a CSS-animated ticker.
Vote in the poll and change the name. Then record it with the video's sound, export it frame by frame, or send it over WebRTC as a camera
would be sent. The result plays under the buttons.
Nothing recorded yet.
Show the code video and HTML in one canvas, three ways out
- The markup. The video and the overlays are children of one
drawableelement in the canvas. - The CSS. Ordinary positioning, a transition and an animation.
- The paint handler. One
drawElementImage()call, with the destination size given. It runs for each video frame and each DOM change. - Record.
canvas.captureStream()gives the picture andvideo.captureStream()gives the sound.MediaRecorderwrites both. - Export frame by frame. Seek the video, set each animation's
currentTime, wait for onepaint, wrap the canvas in aVideoFrameand encode it. The sound track is decoded, cut and encoded as Opus. - Send. The canvas stream is added to an
RTCPeerConnectionlike a camera track. - The three buttons on this page.
- The whole library file.
How it works
- The canvas is a 2D canvas with a fixed 640 by 360 buffer. Every recording has that size on every screen.
- The scene is DOM. The poll buttons take clicks inside the canvas, and the result is in the next frame of the stream.
- Recording and sending use the canvas in real time. Export does not: it moves the video and the animations one frame at a time, so a slow device produces the same file as a fast one.
Live checks
Each check builds its own small canvas in a corner of the window. The picture above isn't touched.
| Result | Experiment | Detail |
|---|
Findings
From Chromium 157 with software video decoding. The rows above say what your browser does.
- Works: recording with sound.
video.captureStream()gives an audio track while the element is muted, andMediaRecorderaccepts it next to the canvas track. The file has the video frame, the HTML over it and the sound. - Works: frame-exact export. After
seeked, onerequestPaint()gives a canvas with exactly the frame asked for. The frame numbers read back from an exported file were the ones asked for, and an element moved by a CSS animation was at the position for each frame's time. The cost here was about 20 to 35 ms per frame at 640 by 360. - Works: sound in the export.
decodeAudioData()on anOfflineAudioContextneeds no user gesture. The slice is encoded withAudioEncoder(Opus) and written as a second WebM track by/lib/webm-mux.js. - Works: the canvas as a camera. The stream passes through
RTCPeerConnectionand the far side receives the overlays. A camera insrcObjectcan be the video under them. - Needed: give
drawElementImage()a size. With only a position, the element is drawn at the size that shows it at its CSS size on screen. In a 640 px buffer shown 354 px wide, a 640 px scene is drawn 1157 buffer pixels wide and most of it is cut off.drawElementImage(scene, 0, 0, canvas.width, canvas.height)fills the buffer at any shown size, and it scales the hit-test region too, so the poll buttons take taps where they are drawn with no call toupdateElementGeometry(). - Limit: real time needs the tab.
paintstops in a hidden tab, so a live recording or stream freezes there. The frame-by-frame export stops too and continues when the tab is shown again. - Limit: the export seeks a file. A camera or another live stream can't be seeked, so only the live recording works for it.
- Not verified here: seek accuracy with a hardware decoder,
AudioEncoderon Android, and how long a real phone takes per frame. The second and third live checks answer the first two on your device.