WebGear API Usage Examples:¶
Requirements¶
Installation with Asyncio Support¶
WebGear API is the part of asyncio package of VidGear, thereby you need to install VidGear with asyncio support as follows:
ASGI Server¶
You'll also need to install an ASGI Server to run following WebGear usage examples, and by default WebGear ships the state-of-the-art uvicorn Server. But you can also use other ASGI server such as daphne, or hypercorn with it.
 
Bare-Minimum Usage with Default MJPEG Mode ¶
By default (streaming_mode="mjpeg"), WebGear only streams JPEG-encoded frames over HTTP at /video endpoint, with no FFmpeg dependency. See Modes of Operation âž¶ for its pros and cons.
MJPEG Performance Enhancing Attributes
In MJPEG Mode, WebGear provides certain performance enhancing attributes for its options dictionary parameter to cope with performance-throttling.
Following JPEG Performance Enhancing Attributes only work in MJPEG Mode (and in H.264 Mode's MJPEG fallback stream), and have no effect on H.264 encoding, except frame_size_reduction.
-
frame_size_reduction: (int/float) This attribute controls the size reduction(in percentage) of the frame to be streamed on Server. Its value has the most significant effect on WebGear performance: More its value, smaller will be frame size and faster will be live streaming. The value defaults to25, and must be lower than90(higher values are faster, with max compression and barely visible frame-size) and no lower than0(slowest, no compression, Original frame-size). Its recommended value is between40~60. This attribute downsizes frames in H.264 Mode too. Its usage is as follows: -
Various Encoding Parameters:
In MJPEG Mode, the input video frames are first encoded into Motion JPEG (M-JPEG or MJPEG) compression format, in which each video frame or interlaced field of a digital video sequence is compressed separately as a JPEG image using
simplejpeglibrary, before sending onto a server. Therefore, WebGear API provides various attributes to have full control over JPEG encoding performance and quality, which are as follows:-
jpeg_compression_quality: (int/float) This attribute controls the JPEG quantization factor. Its value varies from10to100(the higher is the better quality but performance will be lower). Its default value is90. Its usage is as follows: -
jpeg_compression_fastdct: (bool) This attribute if True, WebGear API uses fastest DCT method that speeds up decoding by 4-5% for a minor loss in quality. Its default value is alsoTrue, and its usage is as follows: -
jpeg_compression_fastupsample: (bool) This attribute if True, WebGear API use fastest color upsampling method. Its default value isFalse, and its usage is as follows:
-
Let's implement our Bare-Minimum usage example in MJPEG Mode with these Performance Enhancing Attributes âž¶ for speeding up the output, as follows:
For accessing WebGear on different Client Devices on the network, use "0.0.0.0" as host value instead of "localhost" on Host Machine. More information can be found here âž¶
which can be accessed on any browser on your machine at http://localhost:8000/.
Â
Bare-Minimum Usage with Low-Latency H.264 Streaming ¶
New in v0.3.7
Low-Latency H.264 Streaming was added in v0.3.7.
When streaming_mode="h264", WebGear encodes frames with FFmpeg (libx264 -preset ultrafast -tune zerolatency) and pushes each complete H.264 frame as a binary WebSocket message at /ws/video. WebGear's Default Theme decodes them with the browser's hardware-accelerated WebCodecs VideoDecoder inside a Web Worker and paints them on a <canvas>, while a telemetry HUD shows live FPS, bandwidth and decode latency.
Higher Server CPU usage
Every connected viewer starts its own FFmpeg encoder process in this mode, so server CPU usage grows with the number of viewers. Use MJPEG Mode for many simultaneous viewers on a low-power server. See Modes of Operation âž¶ for more details.
Critical Requirements
- Server: FFmpeg with
libx264encoder must be installed (or set viacustom_ffmpeg). On Windows , WebGear looks for FFmpeg on startup, and may download a static FFmpeg build if none is found. - Browser: WebCodecs support (Chrome/Edge 94+, Safari 16.4+, Firefox 130+) inside a Secure Context, i.e.
https://orhttp://localhost.
Automatic MJPEG Fallback
The /video MJPEG endpoint stays available, and the Default Theme automatically falls back to it whenever WebCodecs is unavailable (including plain http:// on non-localhost hosts) or WebSocket stream fails. MJPEG Performance Enhancing Attributes therefore still apply to the fallback stream.
which can be accessed on any WebCodecs-capable browser on your machine at http://localhost:8000/.
Â