camera.ffmpeg
#
Description#
Plugin to interact with a camera over FFmpeg.
- param opts:
Camera options - see constructor of CameraPlugin.
Configuration#
camera.ffmpeg:
# [Optional]
# Path to the camera device (default: ``/dev/video0``).
# device: /dev/video0 # type=Optional[str]
# [Optional]
# FFmpeg input format for the camera device (default: ``v4l2``).
# input_format: v4l2 # type=str
# [Optional]
# Extra options to be passed to the FFmpeg executable.
# ffmpeg_args: () # type=Iterable[str]
# [Optional]
# Default resolution, as a tuple of two integers.
# resolution: (640, 480) # type=Tuple[int, int]
# [Optional]
# Directory where the camera frames will be stored
# (default: ``~/.local/share/platypush/<plugin.name>/frames``)
# frames_dir: # type=Optional[str]
# [Optional]
# Cameras usually take a while to adapt their
# luminosity and focus to the environment when taking a picture.
# This parameter allows you to specify the number of "warmup" frames
# to capture upon picture command before actually capturing a frame
# (default: 5 but you may want to calibrate this parameter for your
# camera)
# warmup_frames: 5 # type=int
# [Optional]
# Number of seconds to wait before a picture is
# taken or the first frame of a video/sequence is captured (default:
# 0).
# warmup_seconds: 0.0 # type=Optional[float]
# [Optional]
# Maximum number of seconds to wait between the
# programmed termination of a capture session and the moment the
# device is released.
# capture_timeout: 20.0 # type=Optional[float]
# [Optional]
# If set, the images will be scaled along the x-axis by
# the specified factor
# scale_x: # type=Optional[float]
# [Optional]
# If set, the images will be scaled along the y-axis by
# the specified factor
# scale_y: # type=Optional[float]
# [Optional]
# If set, the images will be rotated by the specified
# number of degrees
# rotate: # type=Optional[float]
# [Optional]
# Whether the output should be converted to grayscale.
# grayscale: # type=Optional[bool]
# [Optional]
# Color transformation to apply to the images.
# color_transform: # type=Union[str, int, NoneType]
# [Optional]
# Frames per second (default: 25).
# fps: 16 # type=float
# [Optional]
# If set, the images will be flipped on the
# horizontal axis.
# horizontal_flip: False # type=bool
# [Optional]
# If set, the images will be flipped on the vertical
# axis.
# vertical_flip: False # type=bool
# [Optional]
# Plugin-specific format/type for the output videos.
# output_format: # type=Optional[str]
# [Optional]
# Default format for the output when streamed to a
# network device. Available:
#
# - ``mjpeg`` (default)
# - ``h264`` (over ``ffmpeg``)
# - ``h265`` (over ``ffmpeg``)
# - ``mkv`` (over ``ffmpeg``)
# - ``mp4`` (over ``ffmpeg``)
# stream_format: mjpeg # type=str
# [Optional]
# Default port to be used for streaming over TCP
# (default: 5000).
# listen_port: 5000 # type=Optional[int]
# [Optional]
# Default bind address for TCP streaming (default:
# 0.0.0.0, accept connections on any network interface).
# bind_address: 0.0.0.0 # type=str
# [Optional]
# If set, the camera will start streaming on the
# specified ``bind_address`` and ``listen_port`` as soon as the plugin
# is started. Otherwise, the stream will be started only when the
# `CameraPlugin.start_streaming <https://docs.platypush.tech/platypush/plugins/camera.html#platypush.plugins.camera.CameraPlugin.start_streaming>`_ method is called. Default: False.
# stream_on_start: False # type=bool
# [Optional]
# Path to the ffmpeg binary (default: ``ffmpeg``).
# ffmpeg_bin: ffmpeg # type=str
# [Optional]
# Specify the ffmpeg video codec (``-vcodec``) used
# for the input.
# input_codec: # type=Optional[str]
# [Optional]
# Specify the ffmpeg video codec (``-vcodec``) to be
# used for encoding the output. For some ffmpeg output formats (e.g.
# ``h264`` and ``rtp``) this may default to ``libxvid``.
# output_codec: # type=Optional[str]
# [Optional]
# How often the `RunnablePlugin.loop <https://docs.platypush.tech/platypush/plugins/.html#platypush.plugins.RunnablePlugin.loop>`_ function should be
# executed (default: 15 seconds). *NOTE*: For back-compatibility
# reasons, the `poll_seconds` argument is also supported, but it's
# deprecated.
# poll_interval: 15 # type=Optional[float]
# [Optional]
# How long we should wait for any running
# threads/processes to stop before exiting (default: 5 seconds).
# stop_timeout: 5 # type=Optional[float]
# [Optional]
# If set to True then the plugin will not monitor
# for new events. This is useful if you want to run a plugin in
# stateless mode and only leverage its actions, without triggering any
# events. Defaults to False.
# disable_monitor: False # type=bool
Dependencies#
pip
pip install numpy Pillow
Alpine
apk add ffmpeg py3-pillow py3-numpy
Debian
apt install python3-numpy ffmpeg python3-pillow
Fedora
yum install ffmpeg python-pillow python-numpy
Arch Linux
pacman -S ffmpeg python-pillow python-numpy
Actions#
Module reference#
- class platypush.plugins.camera.ffmpeg.CameraFfmpegPlugin(device: str | None = '/dev/video0', input_format: str = 'v4l2', ffmpeg_args: Iterable[str] = (), **opts)[source]#
Bases:
CameraPlugin
Plugin to interact with a camera over FFmpeg.
- __init__(device: str | None = '/dev/video0', input_format: str = 'v4l2', ffmpeg_args: Iterable[str] = (), **opts)[source]#
- Parameters:
device – Path to the camera device (default:
/dev/video0
).input_format – FFmpeg input format for the camera device (default:
v4l2
).ffmpeg_args – Extra options to be passed to the FFmpeg executable.
opts – Camera options - see constructor of
platypush.plugins.camera.CameraPlugin
.
- capture_frame(device: Camera, *_, **__) Image | None [source]#
Capture a frame from a device using the plugin-specific logic - to be implemented by the derived classes.
- Parameters:
device – An initialized
platypush.plugins.camera.Camera
object.
- capture_image(image_file: str, device: str | int | None = None, preview: bool = False, **camera) str #
Capture an image.
- Parameters:
image_file – Path where the output image will be stored.
device – Name/path/ID of the device to capture from (default: None, use the default device).
camera – Camera parameters override - see constructors parameters.
preview – Show a preview of the camera frames.
- Returns:
The local path to the saved image.
- capture_preview(device: str | int | None = None, duration: float | None = None, n_frames: int | None = None, **camera) dict #
Start a camera preview session.
- Parameters:
device – Name/path/ID of the device to capture from (default: None, use the default device).
duration – Preview duration (default: until
stop_capture()
is called).n_frames – Number of frames to display before closing (default: until
stop_capture()
is called).camera – Camera object properties.
- Returns:
The status of the device.
- capture_sequence(device: str | int | None = None, duration: float | None = None, n_frames: int | None = None, preview: bool = False, **camera) str | None #
Capture a sequence of frames from a camera and store them to a directory.
- Parameters:
device – Name/path/ID of the device to capture from (default: None, use the default device).
duration – Duration of the sequence in seconds (default: until
stop_capture()
is called).n_frames – Number of images to be captured (default: until
stop_capture()
is called).camera – Camera parameters override - see constructors parameters.
frames_dir
andfps
in particular can be specifically tuned forcapture_sequence
.preview – Show a preview of the camera frames.
- Returns:
The directory where the image files have been stored.
- capture_video(device: str | int | None = None, duration: float | None = None, video_file: str | None = None, preview: bool = False, **camera) str | dict | None #
Capture a video.
- Parameters:
device – Name/path/ID of the device to capture from (default: None, use the default device).
duration – Record duration in seconds (default: None, record until
stop_capture
).video_file – If set, the stream will be recorded to the specified video file (default: None).
camera – Camera parameters override - see constructors parameters.
preview – Show a preview of the camera frames.
- Returns:
If duration is specified, the method will wait until the recording is done and return the local path to the recorded resource. Otherwise, it will return the status of the camera device after starting it.
- capturing_thread(camera: Camera, duration: float | None = None, video_file: str | None = None, image_file: str | None = None, n_frames: int | None = None, preview: bool = False, **kwargs)#
Camera capturing thread.
- Parameters:
camera – An initialized
platypush.plugins.camera.Camera
object.duration – Capturing session duration in seconds (default: until
stop_capture()
is called).video_file – If set, the session will be recorded to this output video file (video capture mode).
image_file – If set, the output of the session will be a single image file (photo mode).
n_frames – Number of frames to be captured (default: until
stop_capture()
is called).preview – Start a preview window.
kwargs – Extra arguments to be passed to
capture_frame()
.
- static encode_frame(frame, encoding: str = 'jpeg') bytes #
Encode a frame to a target type. The default implementation assumes that frame is a
PIL.Image
object.- Parameters:
frame – Image frame (default: a
PIL.Image
object).encoding – Image encoding (e.g.
jpeg
).
- static flip_frame(frame, horizontal_flip: bool = False, vertical_flip: bool = False)#
Frame flip logic. Does nothing unless implemented by a derived plugin.
- Parameters:
frame – Image frame (default: a
PIL.Image
object).horizontal_flip – Flip along the horizontal axis.
vertical_flip – Flip along the vertical axis.
- main()#
Implementation of the main loop of the plugin.
- open(device: str | int | None = None, stream: bool = False, redis_queue: str | None = None, **info) Generator[Camera, None, None] #
Initialize and open a device using a context manager pattern.
- Parameters:
device – Capture device by name, path or ID.
stream – If set, the frames will be streamed to
camera.stream
.redis_queue – If set, the frames will be streamed to
redis_queue
.info – Camera parameters override - see constructors parameters.
- Returns:
The initialized
platypush.plugins.camera.Camera
object.
- open_device(device: str | int | None = None, stream: bool = False, redis_queue: str | None = None, ctx: dict | None = None, **params) Camera #
Initialize and open a device.
- Returns:
The initialized camera device.
- Raises:
platypush.plugins.camera.CaptureSessionAlreadyRunningException
- prepare_device(device: Camera, **_) Popen [source]#
Prepare a device using the plugin-specific logic - to be implemented by the derived classes.
- Parameters:
device – An initialized
platypush.plugins.camera.Camera
object.
- release_device(device: Camera)[source]#
Release a device using the plugin-specific logic - to be implemented by the derived classes.
- Parameters:
device – An initialized
platypush.plugins.camera.Camera
object.
- static rotate_frame(frame, rotation: float | int | None = None)#
Frame rotation logic. The default implementation assumes that frame is a
PIL.Image
object.- Parameters:
frame – Image frame (default: a
PIL.Image
object).rotation – Rotation angle in degrees.
- static scale_frame(frame, scale_x: float | None = None, scale_y: float | None = None)#
Frame scaling logic. The default implementation assumes that frame is a
PIL.Image
object.- Parameters:
frame – Image frame (default: a
PIL.Image
object).scale_x – X-scale factor.
scale_y – Y-scale factor.
- start()#
Start the plugin.
- start_camera(camera: Camera, *args, preview: bool = False, **kwargs)[source]#
Start a camera capture session.
- Parameters:
camera – An initialized
platypush.plugins.camera.Camera
object.preview – Show a preview of the camera frames.
- start_streaming(device: str | int | None = None, duration: float | None = None, stream_format: str | None = None, **camera) dict #
Expose the video stream of a camera over a TCP connection.
When the streaming is started, the plugin will listen on the specified
bind_address
andlisten_port
and stream camera frames to connected clients. Ifstream_format
is a video format (H264, H265, MKV, MP4 etc.) then the camera stream can be viewed using a video player - for example, usingvlc
:vlc tcp://<host>:<port>
- Parameters:
device – Name/path/ID of the device to capture from (default: None, use the default device).
duration – Streaming thread duration (default: until
stop_streaming()
is called).stream_format – Format of the output stream - e.g.
h264
,mjpeg
,mkv
etc. If not specified, thestream_format
configured on the plugin will be used.camera – Camera object properties - see constructor parameters.
- Returns:
The status of the device.
- status(device: str | int | None = None)#
Returns the status of the specified camera or all the active cameras if
device
isNone
.
- stop()#
Stop the plugin.
- stop_capture(device: str | int | None = None)#
Stop any capturing session on the specified device.
- Parameters:
device – Name/path/ID of the device to stop (default: all the active devices).
- stop_streaming(device: str | int | None = None)#
Stop a camera over TCP session.
- Parameters:
device – Name/path/ID of the device to stop (default: all the active devices).
- static store_frame(frame, filepath: str, format: str | None = None)#
Capture a frame to the filesystem using the
PIL
library - it can be overridden by derived classes.- Parameters:
frame – Frame object (default: a byte-encoded object or a
PIL.Image
object).filepath – Destination file.
format – Output format.
- take_picture(image_file: str, device: str | int | None = None, **camera) str #
Alias for
capture_image()
.- Parameters:
image_file – Path where the output image will be stored.
device – Name/path/ID of the device to capture from (default: None, use the default device).
camera – Camera parameters override - see constructors parameters.
- Returns:
The local path to the saved image.
- to_grayscale(frame)#
Convert a frame to grayscale. The default implementation assumes that frame is a
PIL.Image
object.- Parameters:
frame – Image frame (default: a
PIL.Image
object).
- static transform_frame(frame, color_transform)#
Frame color space (e.g.
RGB24
,YUV
etc.) transform logic. Does nothing unless implemented by a derived plugin.
- wait_capture(camera: Camera)[source]#
Wait until a capture session terminates.
- Parameters:
camera – Camera object.
camera.info.capture_timeout
is used as a capture thread termination timeout if set.
- wait_stop(timeout=None)#
Wait until a stop event is received.