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, VideoRecorder
[2]:
!cd ../../../../data/
!dvc  import-url https://github.com/maartenbreddels/ipywebrtc/raw/master/docs/source/Big.Buck.Bunny.mp4
Importing 'https://github.com/maartenbreddels/ipywebrtc/raw/master/docs/source/Big.Buck.Bunny.mp4' -> 'Big.Buck.Bunny.mp4'
Saving information to 'Big.Buck.Bunny.mp4.dvc'.

To track the changes with git, run:

        git add Big.Buck.Bunny.mp4.dvc

[3]:
video = VideoStream.from_url('../../../../data/Big.Buck.Bunny.mp4')
video

Record#

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

[4]:
recorder = VideoRecorder(stream=video)
recorder

Save#

The stream can either be saved via the download button or programmatically, e.g. with:

[5]:
recorder.save('../../../../data/example.webm')

Example WidgetStream#

A WidgetStream creates a MediaStream from any widget.

[6]:
from ipywebrtc import WidgetStream, VideoStream
[7]:
from pythreejs import Mesh, SphereGeometry, MeshLambertMaterial, PerspectiveCamera, DirectionalLight, Scene, AmbientLight, Renderer, OrbitControls
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