Human friendly video for linux
Project description
v4l2py
Forked from aspotton/python-v4l2.
A two purpose API:
- raw python binding for the v4l2 (video4linux2) userspace API, using ctypes (don't even bother wasting your time here. You probably won't use it)
- high level Device API for humans to play with :-)
Only works on python 3 (probably >=3.6).
Installation
From within your favorite python environment:
$ pip install v4l2py
Usage
Without further ado:
>>> from v4l2py import Device
>>> cam = Device.from_id(0)
>>> for i, frame in enumerate(cam):
... print(f"frame #{i}: {len(frame)} bytes")
... if i > 9:
... break
...
frame #0: 54630 bytes
frame #1: 50184 bytes
frame #2: 44054 bytes
frame #3: 42822 bytes
frame #4: 42116 bytes
frame #5: 41868 bytes
frame #6: 41322 bytes
frame #7: 40896 bytes
frame #8: 40844 bytes
frame #9: 40714 bytes
frame #10: 40662 bytes
Getting information about the device:
>>> from v4l2py import Device
>>> cam = Device.from_id(0)
>>> cam.open()
>>> cam.capabilities
{'driver': b'uvcvideo',
'card': b'Integrated_Webcam_HD: Integrate',
'bus_info': b'usb-0000:00:14.0-6',
'version': 328798,
'capabilities': <STREAMING|META_CAPTURE|EXT_PIX_FORMAT|VIDEO_CAPTURE: 2225078273>}
>>> cam.supported_formats
[{'type': <BufferType.VIDEO_CAPTURE: 1>,
'flags': <ImageFormatFlag.COMPRESSED: 1>,
'description': b'Motion-JPEG',
'pixelformat': 1196444237},
{'type': <BufferType.VIDEO_CAPTURE: 1>,
'flags': <ImageFormatFlag.0: 0>,
'description': b'YUYV 4:2:2',
'pixelformat': 1448695129}]
>>> cam.get_format()
{'width': 640, 'height': 480, 'pixelformat': 'MJPG'}
Bonus track
You've been patient enough to read until here so, just for you, a 20 line gem: a flask web server displaying your device on the web:
$ pip install flask
# web.py
import flask
from v4l2py import Device
app = flask.Flask('basic-web-cam')
def gen_frames():
with Device.from_id(0) as cam:
cam.set_format(640, 480, 'MJPG')
for frame in cam:
yield b"--frame\r\nContent-Type: image/jpeg\r\n\r\n" + frame + b"\r\n"
@app.route("/")
def index():
return '<!DOCTYPE html><html><body><img src="/stream" /></body></html>'
@app.route("/stream")
def stream():
return flask.Response(
gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
app.run(host="0.0.0.0")
run with:
$ python web.py
Point your browser to 127.0.0.1:5000 and you should see your camera rolling!
See the linux/videodev2.h
header file for details.
Video for Linux Two Specification <http://linuxtv.org/downloads/v4l-dvb-apis/ch07s02.html>
_Reporting bugs <http://bugs.launchpad.net/python-v4l2>
_
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
v4l2py-0.3.0.tar.gz
(31.2 kB
view details)
File details
Details for the file v4l2py-0.3.0.tar.gz
.
File metadata
- Download URL: v4l2py-0.3.0.tar.gz
- Upload date:
- Size: 31.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.6.0.post20210108 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f2f5fe25e2cfd2f571d8d5086401e85527a2f6163ccb611512a2585a8dc79f3 |
|
MD5 | 1289b55194d71d57ed45567b4dc57604 |
|
BLAKE2b-256 | b7b124bef3014439fc9908390f7f4b41b44cf87135a41c0b503d34d2a9329629 |