import cv2 import pydub import pydub.playback #For Emid # RTSP stream URL rtsp_url = "rtsp://example.com/stream" # Open the RTSP stream cap = cv2.VideoCapture(rtsp_url) # Check if the stream was opened successfully if not cap.isOpened(): print("Failed to open the RTSP stream") exit(1) # Prepare an empty audio stream audio_stream = pydub.AudioSegment.empty() # Read and process the video frames while True: # Read a frame from the stream ret, frame = cap.read() # Check if the frame was read successfully if not ret: print("Failed to read a frame from the RTSP stream") break # Extract audio data from the frame audio_data = frame.tobytes() # Convert the audio data to an AudioSegment audio_segment = pydub.AudioSegment(audio_data, frame_rate=44100, sample_width=2, channels=2) # Append the audio segment to the audio stream audio_stream += audio_segment # Play the audio stream pydub.playback.play(audio_stream) # Clear the audio stream audio_stream = pydub.AudioSegment.empty() # Release resources cap.release() cv2.destroyAllWindows()