Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix(video): store video sources as virtual files instead of inlining (#9812)

Shahmir Varqha370dc0a28f0c
diff --git a/marimo/_output/data/data.py b/marimo/_output/data/data.py
index eee8f25c43..54ca4a66db 100644
--- a/marimo/_output/data/data.py
+++ b/marimo/_output/data/data.py
@@ -58,6 +58,21 @@ def audio(data: bytes, ext: str = "wav") -> VirtualFile:
return item.virtual_file
+def video(data: bytes, ext: str = "mp4") -> VirtualFile:
+ """Create a virtual file from a video.
+
+ Args:
+ data (bytes): Video data in bytes
+ ext (str): File extension
+
+ Returns:
+ A `VirtualFile` object.
+ """
+ item = VirtualFileLifecycleItem(ext=ext, buffer=data)
+ item.add_to_cell_lifecycle_registry()
+ return item.virtual_file
+
+
def csv(data: str | bytes | io.BytesIO) -> VirtualFile:
"""Create a virtual file for CSV data.
diff --git a/marimo/_plugins/stateless/video.py b/marimo/_plugins/stateless/video.py
index 9e721d1003..6a0faf663e 100644
--- a/marimo/_plugins/stateless/video.py
+++ b/marimo/_plugins/stateless/video.py
@@ -2,7 +2,9 @@
from __future__ import annotations
import io
+import os
+import marimo._output.data.data as mo_data
from marimo._output.builder import h
from marimo._output.hypertext import Html
from marimo._output.rich_help import mddoc
@@ -10,6 +12,36 @@ from marimo._output.utils import create_style, normalize_dimension
from marimo._plugins.core.media import io_to_data_url
+def _get_resolved_src(
+ src: str | bytes | io.BytesIO | io.BufferedReader,
+) -> str | None:
+ """Determine the correct URL for the given video source.
+
+ Local files, bytes, and file-like objects are stored as virtual files so
+ that the (potentially large) video data is served via a URL rather than
+ inlined as a base64 data URL. This mirrors how `mo.audio` and `mo.image`
+ handle their sources.
+ """
+ if isinstance(src, (io.BufferedReader, io.BytesIO)):
+ pos = src.tell()
+ src.seek(0)
+ resolved_src = mo_data.video(src.read()).url
+ src.seek(pos)
+ return resolved_src
+
+ if isinstance(src, bytes):
+ return mo_data.video(src).url
+
+ if isinstance(src, str):
+ expanded = os.path.expanduser(src)
+ if os.path.isfile(expanded):
+ with open(expanded, "rb") as f:
+ ext = os.path.splitext(expanded)[1] or ".mp4"
+ return mo_data.video(f.read(), ext=ext).url
+
+ return io_to_data_url(src, fallback_mime_type="video/mp4")
+
+
@mddoc
def video(
src: str | bytes | io.BytesIO | io.BufferedReader,
@@ -25,14 +57,19 @@ def video(
Example:
```python3
+ # Render a video from a URL
mo.video(
src="https://v3.cdnpk.net/videvo_files/video/free/2013-08/large_watermarked/hd0992_preview.mp4",
controls=False,
)
+
+ # Render a video from a local file
+ mo.video(src="path/to/video.mp4")
```
Args:
- src: the URL of the video or a file-like object
+ src: the URL of the video, a path to a local file, `bytes`, or a
+ file-like object opened in binary mode
controls: whether to show the controls
muted: whether to mute the video
autoplay: whether to autoplay the video.
@@ -45,12 +82,7 @@ def video(
Returns:
`Html` object
"""
- # Convert to bytes right away since can only be read once
- if isinstance(src, io.BufferedReader):
- src.seek(0)
- src = src.read()
-
- resolved_src = io_to_data_url(src, fallback_mime_type="video/mp4")
+ resolved_src = _get_resolved_src(src)
styles = create_style(
{
"width": normalize_dimension(width),
diff --git a/tests/_plugins/stateless/test_video.py b/tests/_plugins/stateless/test_video.py
new file mode 100644
index 0000000000..4aa2d92514
--- /dev/null
+++ b/tests/_plugins/stateless/test_video.py
@@ -0,0 +1,84 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+from marimo._plugins.stateless.video import video
+from marimo._runtime.context import get_context
+from marimo._runtime.runtime import Kernel
+from tests.conftest import ExecReqProvider
+
+
+async def test_video_url() -> None:
+ result = video("https://example.com/test.mp4")
+ assert "src='https://example.com/test.mp4'" in result.text
+ # External URLs are not stored as virtual files / inlined
+ assert "data:" not in result.text
+
+
+async def test_video_nonexistent_path_passthrough() -> None:
+ # A path that isn't a readable file (e.g. a public/ file when the cwd is
+ # not the notebook directory) is passed through as-is rather than inlined.
+ src = "public/__marimo_test_does_not_exist__.mp4"
+ result = video(src)
+ assert f"src='{src}'" in result.text
+ assert "data:" not in result.text
+
+
+async def test_video_bytes(k: Kernel, exec_req: ExecReqProvider) -> None:
+ await k.run(
+ [
+ exec_req.get(
+ """
+ import marimo as mo
+ video = mo.video(b"hello")
+ """
+ ),
+ ]
+ )
+ assert len(get_context().virtual_file_registry.registry) == 1
+ for fname in get_context().virtual_file_registry.registry:
+ assert fname.endswith(".mp4")
+
+
+async def test_video_bytes_io(k: Kernel, exec_req: ExecReqProvider) -> None:
+ await k.run(
+ [
+ exec_req.get(
+ """
+ import io
+ import marimo as mo
+ bytestream = io.BytesIO(b"hello")
+ video = mo.video(bytestream)
+ """
+ ),
+ ]
+ )
+ assert len(get_context().virtual_file_registry.registry) == 1
+ for fname in get_context().virtual_file_registry.registry:
+ assert fname.endswith(".mp4")
+
+
+async def test_video_local_file(k: Kernel, exec_req: ExecReqProvider) -> None:
+ # A local file is stored as a virtual file (and served via a URL) rather
+ # than being inlined into the output as a base64 data URL. This is what
+ # keeps large videos from blowing past the output size limit.
+ await k.run(
+ [
+ exec_req.get(
+ """
+ import os
+ import marimo as mo
+ with open("test_video.mp4", "wb") as f:
+ f.write(b"fake video bytes")
+ video = mo.video("test_video.mp4")
+ os.remove("test_video.mp4")
+ """
+ ),
+ ]
+ )
+ assert len(get_context().virtual_file_registry.registry) == 1
+ for fname in get_context().virtual_file_registry.registry:
+ assert fname.endswith(".mp4")
+ # The resolved src is a virtual-file URL, not an inline data URL.
+ video_html = k.globals["video"]
+ assert "data:" not in video_html.text
+ assert "@file/" in video_html.text