This example illustrates how to read frames from a webcam and send them to a browser.
First install the required packages:
$ pip install aiohttp aiortc
When you start the example, it will create an HTTP server which you can connect to from your browser:
$ python webcam.py
You can then browse to the following page with your browser:
Once you click Start the server will send video from its webcam to the browser.
Warning
Due to the timing of when Firefox starts responding to mDNS requests and the current lack of ICE trickle support in aiortc, this example may not work with Firefox. For details see:
aiortc/aiortc#481 and https://bugzilla.mozilla.org/show_bug.cgi?id=1691189
If you want to play a media file instead of using the webcam, run:
$ python webcam.py --play-from video.mp4
If you want to play an OGG file containing Opus audio without decoding the frames, run:
$ python webcam.py --play-from audio.ogg --play-without-decoding --audio-codec audio/opus
You can generate an example of such a file using:
$ ffmpeg -f lavfi -i "sine=frequency=1000:duration=20" -codec:a libopus -f ogg audio.ogg
If you want to play an MPEGTS file containing H.264 video without decoding the frames, run:
$ python webcam.py --play-from video.ts --play-without-decoding --video-codec video/H264
You can generate an example of such a file using:
$ ffmpeg -f lavfi -i testsrc=duration=20:size=640x480:rate=30 -pix_fmt yuv420p -codec:v libx264 -profile:v baseline -level 31 -f mpegts video.ts
If you want to play a WebM file containing VP8 video without decoding the frames, run:
$ python webcam.py --play-from video.webm --play-without-decoding --video-codec video/VP8
You can generate an example of such a file using:
$ ffmpeg -f lavfi -i testsrc=duration=20:size=640x480:rate=30 -codec:v vp8 -f webm video.webm
The original idea for the example was from Marios Balamatsias.
Support for playback without decoding was based on an example by Renan Prata.