Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@asus4
Last active November 27, 2017 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asus4/2b68a37488b3e79f56b27232babc2917 to your computer and use it in GitHub Desktop.
Save asus4/2b68a37488b3e79f56b27232babc2917 to your computer and use it in GitHub Desktop.
Print webcam spec using v4l2 command
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Tag auth """
from __future__ import print_function
import commands
import re
def v4l2_ctl(options):
"""Run v4l2"""
status, output = commands.getstatusoutput('v4l2-ctl '+ options)
if status is not 0:
raise Exception
# print(output)
lines = output.split('\n')
return [[kv.strip() for kv in line.split(':')] for line in lines]
def parse_controls(items):
"""Parse v4l2 control"""
for item in items:
print('- ' + item[0])
def parse_formats(items):
"""Parse v4l2 format"""
# Find (*)
parens = re.compile(r"\(.+?\)")
parsing_interval = False
for item in items:
# print(item)
if item[0] == 'Pixel Format':
if parsing_interval:
print('|')
parsing_interval = False
print('\n__{}__\n'.format(item[1]))
print('| Size | FPS |\n|---|---|')
elif item[0] == 'Size':
if parsing_interval:
print('|')
parsing_interval = False
print('|{} |'.format(item[1].translate(None, 'Discrete')), end='')
elif item[0] == 'Interval':
res = parens.search(item[1]).group(0)
res = res.translate(None, '(').translate(None, ' fps)')
if parsing_interval:
print(', {}'.format(res), end='')
else:
print('{}'.format(res), end='')
parsing_interval = True
print('|\n')
def main():
"""Main function"""
print('### Controls')
result = v4l2_ctl('--list-ctrls')
parse_controls(result)
print('\n\n### Formats')
result = v4l2_ctl('--list-formats-ext')
parse_formats(result)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment