import cv2 import subprocess import threading import time def stream_mp4_as_rtsp(mp4_file, rtsp_port=8554): """Stream MP4 file as RTSP in a loop""" rtsp_url = f"rtsp://localhost:{rtsp_port}/stream" # FFmpeg command to stream MP4 as RTSP cmd = [ 'ffmpeg', '-re', # Read input at native frame rate '-stream_loop', '-1', # Loop infinitely '-i', mp4_file, '-c', 'copy', # Copy without re-encoding '-f', 'rtsp', rtsp_url ] print(f"Starting RTSP server...") print(f"RTSP URL: {rtsp_url}") try: process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return process, rtsp_url except Exception as e: print(f"Error starting RTSP server: {e}") return None, None if __name__ == "__main__": mp4_file = input("Enter MP4 file path: ") process, url = stream_mp4_as_rtsp(mp4_file) if process: print(f"RTSP stream running at: {url}") print("Press Ctrl+C to stop") try: process.wait() except KeyboardInterrupt: process.terminate() print("RTSP server stopped")