Skip to content

Play WebM in the video preview when FFmpeg cannot open the container - #343725

Merged
Henning Dieterichs (hediet) merged 3 commits into
mainfrom
hediet/webm-preview-mse
Oct 23, 2026
Merged

Henning Dieterichs (hediet) merged 3 commits into
mainfrom
hediet/webm-preview-mse

Conversation

@hediet

@hediet Henning Dieterichs (hediet) commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Refs #195758

Note

This is a workaround. #195758 tracks adding WebM support to the FFmpeg that VS Code ships. Once that lands, the fallback is no longer reached and should be removed; the code links to the issue.

WebM videos don't play in VS Code's video preview: opening one shows "An error occurred while loading the video file." Shipped VS Code builds use an FFmpeg built without the WebM/Matroska container, so <video src> fails with PipelineStatus::DEMUXER_ERROR_COULD_NOT_OPEN: FFmpegDemuxer: open context failed, even though the VP8/VP9 decoders are present and canPlayType reports WebM as supported. This also affects WebM recordings made by Playwright's page.screencast.

This change makes the video preview fall back to Media Source Extensions when that happens. MediaSource parses WebM in Chromium itself, without FFmpeg's demuxer.

Changes

  • The preview still sets <video src> first, so MP4, and WebM on builds whose FFmpeg supports it, behave as before.
  • Only when a video fails with MEDIA_ERR_SRC_NOT_SUPPORTED, the webview fetches the file and checks for the WebM/Matroska signature. It reads the codecs from the header (VP8, VP9, AV1, Opus, Vorbis), creates a MediaSource with a matching SourceBuffer, and appends the bytes. If anything fails, it shows the existing error message.
  • The preview's Content Security Policy now allows blob: for media and connect-src to the webview resource origin.

The fallback streams the file and is bounded so it cannot make already-failing files worse: it stops after the first bytes when the file isn't WebM, and gives up above 200 MB, showing the existing error message. These limits only apply after normal playback has failed, so videos that play natively are unaffected.

Before and after

Recorded in Code OSS from this branch. To reproduce the shipped behavior, the dev build's ffmpeg.dll was temporarily replaced with the one from Insiders 1.143.0 (20f16aa534). Both recordings open the same evidence-todo.webm from the Explorer, with mediaPreview.video.autoPlay enabled.

Before: the preview shows the load error.

(video to be attached)

After: the same file plays.

(video to be attached)

How to test

  1. On a build whose FFmpeg lacks WebM support (Insiders on Windows, macOS or Linux), open a VP8 WebM file, for example one recorded with Playwright's page.screencast.
  2. The video preview plays it and can seek, instead of showing the load error.
  3. Open an MP4 file: it still plays through the regular path.

Validation:

  • scripts\test.bat --grep "Media Preview": 14 passing.
  • ESLint on mediaPlayerEditorInput.ts: no problems.
  • Manual check in Code OSS on Windows with the shipped ffmpeg.dll:
    • The WebM plays (784×764, playback advancing, no error) through the fallback.
    • An MP4 still plays through the regular path.
    • A 300 MB .mp4 that isn't a video shows the error after 0.5 s, without reading the file.
    • A 300 MB file with a WebM header shows the error after about 3 s, at the 200 MB limit (the webview resource loader sends no Content-Length).
  • In Insiders itself, the same bytes failed with a plain <video> element, then played and seeked through MediaSource.

Not tested on macOS or Linux.

Copilot AI balanced review requested due to automatic review settings October 23, 2026 08:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Codec detection can reject valid WebM files, and failed non-WebM videos are unnecessarily loaded entirely into memory.

2 open issues
What changed in this PR

Adds a Media Source Extensions fallback to VS Code’s video preview so WebM files can play when the shipped FFmpeg cannot open their container, addressing issue #195758.

Changes:

  • Preserves direct playback and attempts a one-time fallback for unsupported video sources.
  • Detects WebM codecs and appends the fetched file to a matching MediaSource buffer.
  • Updates the Content Security Policy to allow resource fetching and blob media URLs.
File Description
src/​vs/​workbench/​contrib/​mediaPreview/​browser/​mediaPlayerEditorInput.ts Adds WebM fallback playback and the required Content Security Policy permissions.

Review effort: Balanced

Comment on lines +306 to +307
const header = Array.from(bytes.subarray(0, 65536), byte => String.fromCharCode(byte)).join('');
const codecs = Object.keys(webmCodecs).filter(id => header.includes(id)).map(id => webmCodecs[id]);
Comment on lines +315 to +316
const bytes = new Uint8Array(await response.arrayBuffer());
const type = getWebmType(bytes);
…them

Shipped Electron builds use an FFmpeg without the WebM/Matroska demuxer, so <video src> fails with DEMUXER_ERROR_COULD_NOT_OPEN. When a video fails with MEDIA_ERR_SRC_NOT_SUPPORTED, read the file, detect its WebM codecs, and play it through MediaSource, which parses WebM in Chromium itself.

Refs #195758

Co-authored-by: Copilot <[email protected]>
Stream the file instead of reading it at once: stop at the first bytes when it is not WebM, and give up above 200 MB. This only applies after normal playback has already failed, so videos that play natively are unaffected.

Co-authored-by: Copilot <[email protected]>
@hediet
Henning Dieterichs (hediet) merged commit bb29884 into main Oct 23, 2026
59 checks passed
@hediet
Henning Dieterichs (hediet) deleted the hediet/webm-preview-mse branch October 23, 2026 15:39
@vs-code-engineering vs-code-engineering Bot added this to the 1.144.0 milestone Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants