DVR#

QDVRWidget#

Composite DVR widget for recording and playing back video streams.

class QVideo.dvr.QDVRWidget.QDVRWidget(*args, source=None, filename='', **kwargs)[source]#

Bases: QFrame

Widget providing record, play, pause, stop and rewind controls for a digital video recorder.

Connects to a QVideoSource to capture incoming frames to a file, and can play back previously recorded files. Supported formats are determined by the Writer and Player class attributes; by default AVI (.avi), MKV (.mkv), and MP4 (.mp4) are supported; HDF5 (.h5) is also supported when h5py is installed. Requesting an unsupported extension is logged as an error and silently ignored.

Recording and playback stop automatically when the widget is closed or the application is about to quit.

Parameters:
  • source (QVideoSource or None) – Video source supplying frames to record.

  • filename (str or None) – Default file path for saving. If None, defaults to ~/default.mkv.

  • *args – Forwarded to QFrame.

  • **kwargs – Forwarded to QFrame.

  • Signals

  • ——-

  • newFrame(Image) – Emitted for each frame during playback.

  • recording(bool) – Emitted when recording starts (True) or stops (False).

  • playing(bool) – Emitted when playback starts (True) or stops (False).

newFrame#

Emitted for each frame during playback.

recording#

Emitted when recording starts (True) or stops (False).

playing#

Emitted when playback starts (True) or stops (False).

FILENAME = 'default.mkv'#
Writer: dict[str, type] = {'.avi': <class 'QVideo.dvr.QOpenCVWriter.QOpenCVWriter'>, '.mkv': <class 'QVideo.dvr.QOpenCVWriter.QOpenCVWriter'>, '.mp4': <class 'QVideo.dvr.QOpenCVWriter.QOpenCVWriter'>}#
Player: dict[str, type] = {'.avi': <class 'QVideo.dvr.QOpenCVReader.QOpenCVSource'>, '.mkv': <class 'QVideo.dvr.QOpenCVReader.QOpenCVSource'>, '.mp4': <class 'QVideo.dvr.QOpenCVReader.QOpenCVSource'>}#
FileGroups: dict[str, set[str]] = {'Lossless Video': {'.avi', '.mkv'}, 'Video': {'.mp4'}}#
isRecording()[source]#

Return True if recording is in progress.

Return type:

bool

isPlaying()[source]#

Return True if playback is in progress.

Return type:

bool

isPaused()[source]#

Return True if playback is paused.

Return type:

bool

getFileName(save=False)[source]#

Open a file dialog and update the filename fields.

Parameters:

save (bool) – If True, open a save dialog; otherwise open an open dialog.

Return type:

str

Returns:

str – Selected filename, or empty string if cancelled.

record()[source]#

Start recording, or stop if already recording.

Does nothing if source is None, if playback is active, or if the save filename has an unsupported extension.

Return type:

None

play()[source]#

Start playback, or resume if paused.

Does nothing if recording is active, if playback is already running, or if the playback filename has an unsupported extension.

Return type:

None

pause()[source]#

Pause or resume playback.

Return type:

None

rewind()[source]#

Rewind to the first frame and pause.

Return type:

None

stop()[source]#

Stop recording or playback.

Return type:

None

closeEvent(event)[source]#

Stop recording or playback when the widget is closed.

Return type:

None

setFrameNumber(framenumber)[source]#

Set the displayed frame number.

Return type:

None

stepFrameNumber()[source]#

Increment the displayed frame number.

Return type:

None

property source: QVideoSource | None#

The QVideoSource being recorded.

property player: QVideoSource | None#

The active playback source, or None when not playing.

filename#
pyqtProperty(type, fget=None, fset=None, freset=None, fdel=None, doc=None,

designable=True, scriptable=True, stored=True, user=False, constant=False, final=False, notify=None, revision=0) -> property attribute

type is the type of the property. It is either a type object or a string that is the name of a C++ type. freset is a function for resetting an attribute to its default value. designable sets the DESIGNABLE flag (the default is True for writable properties and False otherwise). scriptable sets the SCRIPTABLE flag. stored sets the STORED flag. user sets the USER flag. constant sets the CONSTANT flag. final sets the FINAL flag. notify is the NOTIFY signal. revision is the REVISION. The other parameters are the same as those required by the standard Python property type. Properties defined using pyqtProperty behave as both Python and Qt properties. Decorators can be used to define new properties or to modify existing ones.

playname#
pyqtProperty(type, fget=None, fset=None, freset=None, fdel=None, doc=None,

designable=True, scriptable=True, stored=True, user=False, constant=False, final=False, notify=None, revision=0) -> property attribute

type is the type of the property. It is either a type object or a string that is the name of a C++ type. freset is a function for resetting an attribute to its default value. designable sets the DESIGNABLE flag (the default is True for writable properties and False otherwise). scriptable sets the SCRIPTABLE flag. stored sets the STORED flag. user sets the USER flag. constant sets the CONSTANT flag. final sets the FINAL flag. notify is the NOTIFY signal. revision is the REVISION. The other parameters are the same as those required by the standard Python property type. Properties defined using pyqtProperty behave as both Python and Qt properties. Decorators can be used to define new properties or to modify existing ones.

property framenumber: int#

Current frame number displayed in the LCD.

QOpenCVWriter#

OpenCV-backed video file writer supporting AVI, MKV, and MP4.

class QVideo.dvr.QOpenCVWriter.QOpenCVWriter(*args, codec=None, **kwargs)[source]#

Bases: QVideoWriter

OpenCV-backed video file writer supporting AVI, MKV, and MP4.

Writes frames to a video file using cv2.VideoWriter. The file is opened lazily on the first frame so that frame dimensions and color mode can be determined automatically.

Codecs are selected based on the file extension using CODEC_MAP. When no codec is specified, the preference-ordered list for the extension is probed and the first one OpenCV accepts is used. Specifying codec explicitly bypasses probing.

If the shape of a subsequent frame differs from the first, recording stops immediately and finished is emitted.

Parameters:
  • filename (str) – Path to the output video file.

  • codec (str or None) – Four-character codec code passed to cv2.VideoWriter_fourcc. If None, codecs are chosen from CODEC_MAP based on the file extension.

  • *args – Forwarded to QVideoWriter.

  • **kwargs – Forwarded to QVideoWriter.

CODEC_MAP#

Maps file extensions to preference-ordered codec codes.

Type:

dict[str, tuple[str, …]]

CODEC_MAP: dict[str, tuple[str, ...]] = {'.avi': ('FFV1', 'HFYU'), '.mkv': ('FFV1', 'HFYU'), '.mp4': ('avc1', 'mp4v')}#
open(frame)[source]#

Open the video file using the first available codec.

Called automatically by write() on the first frame. Frame dimensions and color mode are determined from frame; codecs are probed via _getWriter().

Parameters:

frame (Image) – The first video frame, used to determine dimensions and color mode.

Return type:

bool

Returns:

boolTrue if a codec was found and the file was opened; False otherwise.

isOpen()[source]#

Return True if the video file is currently open.

Return type:

bool

close()[source]#

Release the video file and reset internal state.

Return type:

None

QOpenCVReader#

OpenCV video reader and threaded playback source.

class QVideo.dvr.QOpenCVReader.QOpenCVReader(filename)[source]#

Bases: QVideoReader

Video reader for common video file formats (AVI, MKV, MP4, etc.).

Reads frames from a video file using OpenCV’s VideoCapture. Frames are converted from BGR (OpenCV native) to RGB on read.

Parameters:

filename (str) – Path to the video file to read.

FRAMENUMBER = 1#
WIDTH = 3#
HEIGHT = 4#
LENGTH = 7#
FPS = 5#
read()[source]#

Read the next frame from the video file.

Frames are converted from BGR (OpenCV native) to RGB on read.

Return type:

tuple[bool, ndarray[tuple[Any, ...], dtype[ubyte]] | None]

Returns:

tuple[bool, ndarray or None](True, frame) on success, (False, None) at end-of-file or when the reader is not open.

seek(framenumber)[source]#

Seek to the specified frame number.

Return type:

None

property fps: float#

Frame rate reported by the video file [fps].

property length: int#

Total number of frames in the video file.

property framenumber: int#

Index of the next frame to be returned by read().

property width: int#

Frame width in pixels.

property height: int#

Frame height in pixels.

class QVideo.dvr.QOpenCVReader.QOpenCVSource(reader)[source]#

Bases: QVideoSource

Video source for common video file formats (AVI, MKV, MP4, etc.).

Parameters:

reader (str, Path, or QOpenCVReader) – Path to the video file to read, or an existing QOpenCVReader instance.

QHDF5Writer#

HDF5 video writer with per-frame timestamps.

class QVideo.dvr.QHDF5Writer.QHDF5Writer(*args, **kwargs)[source]#

Bases: QVideoWriter

Video writer for HDF5 files.

Writes frames to an HDF5 file as a group of timestamped datasets. Each frame is stored under a key equal to its elapsed time in seconds since recording began. A Timestamp attribute on the file records the absolute start time (UNIX epoch).

The file is created on the first frame and closed explicitly by close(). If the file cannot be created, open() returns False and no data are written.

Parameters:
  • filename (str) – Path to the output HDF5 file.

  • *args – Forwarded to QVideoWriter.

  • **kwargs – Forwarded to QVideoWriter.

open(frame)[source]#

Open the HDF5 file for writing.

Called automatically by write() on the first frame.

Parameters:

frame (Image) – The first video frame (used only to trigger file creation; dimensions are not required in advance for HDF5).

Return type:

bool

Returns:

boolTrue if the file was opened successfully; False if the file could not be created.

isOpen()[source]#

Return True if the HDF5 file is currently open.

Return type:

bool

close()[source]#

Close the HDF5 file and reset internal state.

Return type:

None

QHDF5Reader#

HDF5 video reader and threaded playback source.

class QVideo.dvr.QHDF5Reader.QHDF5Reader(filename)[source]#

Bases: QVideoReader

Video reader for HDF5 files.

Reads frames from an HDF5 file containing an images group of timestamped datasets, as written by QHDF5Writer.

Parameters:

filename (str) – Path to the HDF5 file to read.

read()[source]#

Read the next frame from the HDF5 file.

Return type:

tuple[bool, ndarray[tuple[Any, ...], dtype[ubyte]] | None]

Returns:

tuple[bool, ndarray or None](True, frame) on success, (False, None) when past the end.

seek(framenumber)[source]#

Advance playback to specified frame number.

Return type:

None

property fps: float#

Estimated frame rate derived from the recorded timestamps [fps].

Computed as (n_frames - 1) / total_duration. Falls back to 30 fps when fewer than two frames are present or the timestamps span zero time.

property length: int#

Total number of frames in the HDF5 file.

property framenumber: int#

Index of the next frame to be returned by read().

property width: int#

Frame width in pixels.

property height: int#

Frame height in pixels.

class QVideo.dvr.QHDF5Reader.QHDF5Source(reader)[source]#

Bases: QVideoSource

Video source for HDF5 files.

Parameters:

reader (str, Path, or QHDF5Reader) – Path to the HDF5 file to read, or an existing QHDF5Reader instance.