Stream Module

This module contains streaming functionality.

RTMP Streaming

RTMP streaming module for FMUS-VID.

This module provides classes for capturing from RTMP streams, outputting to RTMP streams, and monitoring stream health.

class fmusvid.stream.rtmp.RTMPCapture(rtmp_url, width=None, height=None, fps=None, audio_sample_rate=16000, buffer_size=10, gpu_acceleration=True)[source]

Capture frames and audio from an RTMP stream.

This class uses FFmpeg to read from an RTMP stream and provides methods to access frames and audio data.

__init__(rtmp_url, width=None, height=None, fps=None, audio_sample_rate=16000, buffer_size=10, gpu_acceleration=True)[source]

Initialize RTMP capture.

Parameters:
  • rtmp_url (str) – RTMP URL to capture from (e.g., ‘rtmp://localhost/live/stream’)

  • width (Optional[int]) – Output width (None to use source width)

  • height (Optional[int]) – Output height (None to use source height)

  • fps (Optional[float]) – Output frame rate (None to use source frame rate)

  • audio_sample_rate (int) – Audio sample rate in Hz

  • buffer_size (int) – Size of frame buffer

  • gpu_acceleration (bool) – Whether to use GPU acceleration if available

async start()[source]

Start capturing from the RTMP stream.

async stop()[source]

Stop capturing from the RTMP stream.

async get_frame()[source]

Get the next video frame from the stream.

Returns:

RGB image as numpy array

Return type:

np.ndarray

Raises:

RuntimeError – If the stream is not running

async get_audio(duration=0.1)[source]

Get audio samples for a specified duration.

Parameters:

duration (float) – Duration of audio to get in seconds

Returns:

Audio samples as numpy array

Return type:

np.ndarray

async get_frame_and_audio()[source]

Get the next video frame and corresponding audio.

Returns:

RGB image and audio samples

Return type:

Tuple[np.ndarray, np.ndarray]

async aiter_frames()[source]

Async iterator for video frames.

Yields:

np.ndarray – RGB image as numpy array

Return type:

AsyncGenerator[ndarray, None]

async aiter_frames_with_audio()[source]

Async iterator for video frames with corresponding audio.

Yields:

Tuple[np.ndarray, np.ndarray] – RGB image and audio samples

Return type:

AsyncGenerator[Tuple[ndarray, ndarray], None]

iter_frames()[source]

Iterator for video frames (non-async version).

Yields:

np.ndarray – RGB image as numpy array

Return type:

Generator[ndarray, None, None]

iter_frames_with_audio()[source]

Iterator for video frames with corresponding audio (non-async version).

Yields:

Tuple[np.ndarray, np.ndarray] – RGB image and audio samples

Return type:

Generator[Tuple[ndarray, ndarray], None, None]

__del__()[source]

Clean up resources when the object is garbage collected.

class fmusvid.stream.rtmp.RTMPOutput(rtmp_url, width, height, fps=30.0, bitrate='2500k', format='flv', vcodec='libx264', acodec='aac', audio_sample_rate=44100, preset='ultrafast', gpu_acceleration=True)[source]

Output frames to an RTMP stream.

This class uses FFmpeg to send frames to an RTMP stream.

__init__(rtmp_url, width, height, fps=30.0, bitrate='2500k', format='flv', vcodec='libx264', acodec='aac', audio_sample_rate=44100, preset='ultrafast', gpu_acceleration=True)[source]

Initialize RTMP output.

Parameters:
  • rtmp_url (str) – RTMP URL to output to (e.g., ‘rtmp://localhost/live/stream’)

  • width (int) – Width of output video

  • height (int) – Height of output video

  • fps (float) – Frame rate of output video

  • bitrate (str) – Video bitrate

  • format (str) – Output format (usually ‘flv’ for RTMP)

  • vcodec (str) – Video codec

  • acodec (str) – Audio codec

  • audio_sample_rate (int) – Audio sample rate in Hz

  • preset (str) – FFmpeg preset for encoding speed vs quality tradeoff

  • gpu_acceleration (bool) – Whether to use GPU acceleration if available

async start()[source]

Start the RTMP output stream.

async stop()[source]

Stop the RTMP output stream.

async write_frame(frame)[source]

Write a video frame to the RTMP stream.

Parameters:

frame (ndarray) – RGB image as numpy array

Raises:

RuntimeError – If the stream is not running

async write_frame_with_audio(frame, audio)[source]

Write a video frame with audio to the RTMP stream.

Note: This is a simplified implementation. In practice, handling audio in this way is complex and would require more sophisticated audio buffering and synchronization.

Parameters:
  • frame (ndarray) – RGB image as numpy array

  • audio (ndarray) – Audio samples as numpy array

async write_frame_async(frame)[source]

Alias for write_frame to maintain naming consistency with other async methods.

Parameters:

frame (ndarray) – RGB image as numpy array

async close()[source]

Alias for stop().

__del__()[source]

Clean up resources when the object is garbage collected.

class fmusvid.stream.rtmp.StreamMonitor(metrics=None)[source]

Monitor health and performance of video streams.

This class provides metrics and alerting for stream health.

__init__(metrics=None)[source]

Initialize stream monitor.

Parameters:

metrics (Optional[List[str]]) – List of metrics to monitor (e.g., [“bitrate”, “fps”, “latency”])

attach_stream(stream, name=None)[source]

Attach a stream to monitor.

Parameters:
detach_stream(name)[source]

Detach a stream from monitoring.

Parameters:

name (str) – Name of the stream to detach

get_metric(metric, stream_name=None)[source]

Get the current value of a metric.

Parameters:
  • metric (str) – Name of the metric

  • stream_name (Optional[str]) – Name of the stream (None for average across all streams)

Returns:

Current value of the metric

Return type:

float

get_alerts(stream_name=None)[source]

Get active alerts for streams.

Parameters:

stream_name (Optional[str]) – Name of the stream (None for all streams)

Returns:

List of alert dictionaries

Return type:

List[Dict]

__del__()[source]

Clean up resources when the object is garbage collected.