CamGear API Usage Examples:¶
After going through following Usage Examples, Checkout more of its advanced configurations here ➶
Bare-Minimum Usage¶
Following is the bare-minimum code you need to get started with CamGear API:
Using Camgear with Streaming Websites¶
CamGear internally implements yt_dlp
backend class for seamlessly pipelining live video-frames and metadata from various streaming services like Twitch, Vimeo, Dailymotion, and many more ➶. All you have to do is to provide the desired Video's URL to its source
parameter, and enable its stream_mode
parameter.
The complete usage example for Dailymotion and Twitch URLs are as follows:
Bug in OpenCV's FFmpeg
To workaround a FFmpeg bug that causes video to freeze frequently in OpenCV, It is advised to always use GStreamer backend for Livestream videos.
Checkout this FAQ ➶ for compiling OpenCV with GStreamer support.
Not all resolutions are supported with GStreamer Backend. See issue #244
Exclusive CamGear Attributes for yt_dlp
backend
CamGear also provides exclusive attributes:
STREAM_RESOLUTION
(for specifying stream resolution)STREAM_PARAMS
(for specifyingyt_dlp
parameters)
with its options
dictionary parameter. More information can be found here ➶
Supported Streaming Websites
The list of all supported Streaming Websites URLs can be found here ➶
Accessing Stream's Metadata
CamGear now provides ytv_metadata
global parameter for accessing given Video's metadata as JSON Object. It can used as follows:
New in v0.2.4
ytv_metadata
global parameter was added in v0.2.4
.
# import required libraries
from vidgear.gears import CamGear
# Add YouTube Video URL as input source (for e.g https://www.dailymotion.com/video/x2yrnum)
# and enable Stream Mode (`stream_mode = True`)
stream = CamGear(
source="https://www.dailymotion.com/video/x2yrnum", stream_mode=True, logging=True, **options
).start()
# get Video's metadata as JSON object
video_metadata = stream.ytv_metadata
# print all available keys
print(video_metadata.keys())
# get data like `title`
print(video_metadata["title"])
If Twitch user is offline, CamGear will throw ValueError.
Using Camgear with Youtube Videos¶
CamGear API also provides out-of-the-box support for pipelining live video-frames and metadata from YouTube (Livestream + Normal) Videos.
YouTube Playlists are not supported yet.
The complete usage example is as follows:
Bug in OpenCV's FFmpeg
To workaround a FFmpeg bug that causes video to freeze frequently in OpenCV, It is advised to always use GStreamer backend for Livestream videos.
Checkout this FAQ ➶ for compiling OpenCV with GStreamer support.
Not all resolutions are supported with GStreamer Backend. See issue #244
Exclusive CamGear Attributes for yt_dlp
backend
CamGear also provides exclusive attributes:
STREAM_RESOLUTION
(for specifying stream resolution)STREAM_PARAMS
(for specifyingyt_dlp
parameters)
with its options
dictionary parameter. More information can be found here ➶
Accessing Stream's Metadata
CamGear now provides ytv_metadata
global parameter for accessing given Video's metadata as JSON Object. It can used as follows:
New in v0.2.4
ytv_metadata
global parameter was added in v0.2.4
.
# import required libraries
from vidgear.gears import CamGear
# Add YouTube Video URL as input source (for e.g https://youtu.be/uCy5OuSQnyA)
# and enable Stream Mode (`stream_mode = True`)
stream = CamGear(
source="https://youtu.be/uCy5OuSQnyA", stream_mode=True, logging=True, **options
).start()
# get Video's metadata as JSON object
video_metadata = stream.ytv_metadata
# print all available keys
print(video_metadata.keys())
# get data like `title`
print(video_metadata["title"])
Using CamGear with Variable Camera Properties¶
CamGear API also flexibly support various Source Tweak Parameters available within OpenCV's VideoCapture API. These tweak parameters can be used to transform input source Camera-Device properties (such as its brightness, saturation, framerate, resolution, gain etc.) seamlessly, and can be easily applied in CamGear API through its options
dictionary parameter by formatting them as its attributes.
The complete usage example is as follows:
All the supported Source Tweak Parameters can be found here ➶
Using Camgear with Direct Colorspace Manipulation¶
CamGear API also supports Direct Colorspace Manipulation, which is ideal for changing source colorspace on the run.
A more detailed information on colorspace manipulation can be found here ➶
In following example code, we will start with HSV as source colorspace, and then we will switch to GRAY colorspace when W key is pressed, and then LAB colorspace when E key is pressed, finally default colorspace (i.e. BGR) when S key is pressed. Also, quit when Q key is pressed:
Any incorrect or None-type value, will immediately revert the colorspace to default i.e. BGR
.