ipywebrtc#

ipywebrtc provides WebRTC and the MediaStream-API in Jupyter notebooks. This allows e.g. to create screenshots from a MediaStream and analyse them further with skimage. With ipywebrtc you can not only read video, image, audio and widget data but also record stream objects. It even provides a simple chat function.

Installation#

ipywebrtc is installed in both the kernel and the Jupyter environment:

$ pipenv install ipywebrtc

Examples#

Example VideoStream#

[1]:
from ipywebrtc import VideoStream
[2]:
bbb = VideoStream.from_url("https://github.com/maartenbreddels/ipywebrtc/raw/master/docs/source/Big.Buck.Bunny.mp4")
[3]:
bbb

Record#

A record button can be created with MediaRecorder.record, for videos with:

[4]:
from ipywebrtc import VideoRecorder


recorder = VideoRecorder(stream=bbb)
recorder

Save#

The stream can either be saved via the download button or programmatically, for example with:

[ ]:
recorder.save("bbb.webm")

Example WidgetStream#

A WidgetStream creates a MediaStream from any widget.

[6]:
from ipywebrtc import VideoStream, WidgetStream
[7]:
from pythreejs import (
    AmbientLight,
    DirectionalLight,
    Mesh,
    MeshLambertMaterial,
    OrbitControls,
    PerspectiveCamera,
    Renderer,
    Scene,
    SphereGeometry,
)


ball = Mesh(
    geometry=SphereGeometry(radius=1),
    material=MeshLambertMaterial(color="red"),
    position=[2, 1, 0],
)

c = PerspectiveCamera(
    position=[0, 5, 5],
    up=[0, 1, 0],
    children=[
        DirectionalLight(color="white", position=[3, 5, 1], intensity=0.5)
    ],
)

scene = Scene(children=[ball, c, AmbientLight(color="#777777")])

renderer = Renderer(
    camera=c, scene=scene, controls=[OrbitControls(controlling=c)]
)

renderer

The following webgl_stream is updated after something changes in the scene above. You can do this by moving the ball with the mouse.

[8]:
webgl_stream = WidgetStream(widget=renderer)
webgl_stream

Alternatively, you can also use a slider:

[9]:
from ipywidgets import FloatSlider


slider = FloatSlider(
    value=7.5,
    step=0.1,
    description="Test:",
    disabled=False,
    continuous_update=False,
    orientation="horizontal",
    readout=True,
    readout_format=".1f",
)

slider