Skip to content

Instantly share code, notes, and snippets.

@132ikl
Last active September 22, 2019 05:53
Show Gist options
  • Save 132ikl/4ce09fbb76fe5f3b605612263146e7e1 to your computer and use it in GitHub Desktop.
Save 132ikl/4ce09fbb76fe5f3b605612263146e7e1 to your computer and use it in GitHub Desktop.
Python snippet to get a dict of camera's YUYV available resolutions and corresponding FPS values
import subprocess
import re
sp_out = subprocess.run(
"v4l2-ctl -d /dev/video0 --list-formats-ext".split(), capture_output=True
)
# format and remove newlines/tabs
output = str(sp_out.stdout).replace("\\n", " ").replace("\\t", "")
# get only yuyv results
yuyv = re.search("\[\d+\]: 'YUYV' \(YUYV \d:\d:\d\) (.+)(\[\d+\]?|$)", output).group(1)
# group 1: resolution
# group 2: everything up until next instance of Size
modes = {}
for match in re.finditer("Size: \w+ (\d+x\d+) ", yuyv):
resolution = match.group(1)
# get everything from the current resolution to the next resolution
line = re.search(match.group(0) + "(.+?)Size", yuyv).group(1)
fpses = set()
for interval in re.finditer("nterval: \w+ \d+\.\d+s \((.+?) fps\)", line):
fpses.add(float(interval.group(1)))
modes[resolution] = list(fpses)
print(modes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment