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 Hzbuffer_size (
int) – Size of frame buffergpu_acceleration (
bool) – Whether to use GPU acceleration if available
- 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]
- 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 videoheight (
int) – Height of output videofps (
float) – Frame rate of output videobitrate (
str) – Video bitrateformat (
str) – Output format (usually ‘flv’ for RTMP)vcodec (
str) – Video codecacodec (
str) – Audio codecaudio_sample_rate (
int) – Audio sample rate in Hzpreset (
str) – FFmpeg preset for encoding speed vs quality tradeoffgpu_acceleration (
bool) – Whether to use GPU acceleration if available
- 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 arrayaudio (
ndarray) – Audio samples as numpy array
- class fmusvid.stream.rtmp.StreamMonitor(metrics=None)[source]
Monitor health and performance of video streams.
This class provides metrics and alerting for stream health.
- attach_stream(stream, name=None)[source]
Attach a stream to monitor.
- Parameters:
stream (
Union[RTMPCapture,RTMPOutput]) – RTMP stream to monitor
- detach_stream(name)[source]
Detach a stream from monitoring.
- Parameters:
name (
str) – Name of the stream to detach