Getting source capabilities

Question:

Using gst-launch and debug flags, I am able to query the capabilities of my webcam.
E.g.

➜ GST_DEBUG=v4l2src:6 gst-launch-1.0 v4l2src device=/dev/video0 ! xvimagesink
Setting pipeline to PAUSED ...
Pipeline is live and does not need PREROLL ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock
0:00:00.173516041 29482 0x559bc3ff6ca0 DEBUG                v4l2src gstv4l2src.c:512:gst_v4l2src_negotiate:<v4l2src0> caps of src: video/x-raw, format=(string)YUY2, width=(int)1280, height=(int)720, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)10/1; video/x-raw, format=(string)YUY2, width=(int)960, height=(int)540, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)15/1; video/x-raw, format=(string)YUY2, width=(int)848, height=(int)480, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)20/1; video/x-raw, format=(string)YUY2, width=(int)640, height=(int)480, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; video/x-raw, format=(string)YUY2, width=(int)640, height=(int)360, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; video/x-raw, format=(string)YUY2, width=(int)424, height=(int)240, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; video/x-raw, format=(string)YUY2, width=(int)352, height=(int)288, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; video/x-raw, format=(string)YUY2, width=(int)320, height=(int)240, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; video/x-raw, format=(string)YUY2, width=(int)320, height=(int)180, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; image/jpeg, width=(int)1280, height=(int)720, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; image/jpeg, width=(int)960, height=(int)540, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; image/jpeg, width=(int)848, height=(int)480, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; image/jpeg, width=(int)640, height=(int)480, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; image/jpeg, width=(int)640, height=(int)360, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; image/jpeg, width=(int)424, height=(int)240, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; image/jpeg, width=(int)352, height=(int)288, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; image/jpeg, width=(int)320, height=(int)240, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }; image/jpeg, width=(int)320, height=(int)180, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }
...

In a more readable fashion, it says that caps are:

 video/x-raw, format=(string)YUY2, width=(int)1280, height=(int)720, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)10/1
 video/x-raw, format=(string)YUY2, width=(int)960, height=(int)540, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction)15/1
...
 image/jpeg, width=(int)1280, height=(int)720, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }
 image/jpeg, width=(int)960, height=(int)540, pixel-aspect-ratio=(fraction)1/1, framerate=(fraction){ 30/1, 15/1 }
...

I would like to make a python program that extracts this information.

I have been working with this example program, and I can extract the capabilities of v4l2src and the negotiated caps.

I am unable to extract the actual caps available from the physical hardware.

Doing something like this


source_factory = Gst.ElementFactory.find("v4l2src")
pads = source_factory.get_static_pad_templates()
print("*** v4l2src has a lot of capabilities ****")
print_pad_templates_information(source_factory)

source = source_factory.create("source")
source.set_property('device', "/dev/video0")
print("*** Physical v4l2src device has no capabilities ****")
print_pad_capabilities(source, "src")


show a lot of caps on v4l2src and none of the actual element.

print functions are from the tutorials file](https://github.com/gkralik/python-gst-tutorial/blob/master/basic-tutorial-6.py)

When I put it into a pipeline, I get the list of capabilities that is compatible with the pipeline, not the entire list.

So back to the question: How do I make a small python program that shows me the capabilities of my hardware?

Links or code snippet will be appreciate.


Update:
A better example is

#!/usr/bin/env python3

import sys
import gi 
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GLib
Gst.init(sys.argv)
source_factory = Gst.ElementFactory.find("v4l2src")
source = source_factory.create("source")
source.set_property('device', "/dev/video0")
pad_name="src"
pad = source.get_static_pad(pad_name)
caps = pad.get_current_caps()

if not caps:
    print("no current caps")
    caps = pad.get_allowed_caps()

if not caps:
    print("no allowed caps")
    exit(-1)
Asked By: moozer

||

Answers:

You can inspect your webcam as a Gst.Device and get all its available caps that way. GStreamer’s V4L2 support provides a device provider factory that is able to give you all V4L2 webcams on your system.

device_provider = Gst.DeviceProviderFactory.get_by_name("v4l2deviceprovider")
devices = device_provider.get_devices()

Next, you need to find the device in the list that you are interested in. I do this by matching the device object’s “device.path” property with a known path, like “/dev/video0”. Once you do this, you can get the device’s available caps by calling get_caps().

for device in devices:
    path = device.get_properties().get_string("device.path")
    if path == my_path:
        caps = device.get_caps()
Answered By: Velovix
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.