Facebook
From SMA, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 359
  1. import cv2
  2. import pydub
  3. import pydub.playback
  4. #For Emid
  5. # RTSP stream URL
  6. rtsp_url = "rtsp://example.com/stream"
  7.  
  8. # Open the RTSP stream
  9. cap = cv2.VideoCapture(rtsp_url)
  10.  
  11. # Check if the stream was opened successfully
  12. if not cap.isOpened():
  13.     print("Failed to open the RTSP stream")
  14.     exit(1)
  15.  
  16. # Prepare an empty audio stream
  17. audio_stream = pydub.AudioSegment.empty()
  18.  
  19. # Read and process the video frames
  20. while True:
  21.     # Read a frame from the stream
  22.     ret, frame = cap.read()
  23.  
  24.     # Check if the frame was read successfully
  25.     if not ret:
  26.         print("Failed to read a frame from the RTSP stream")
  27.         break
  28.  
  29.     # Extract audio data from the frame
  30.     audio_data = frame.tobytes()
  31.  
  32.     # Convert the audio data to an AudioSegment
  33.     audio_segment = pydub.AudioSegment(audio_data, frame_rate=44100, sample_width=2, channels=2)
  34.  
  35.     # Append the audio segment to the audio stream
  36.     audio_stream += audio_segment
  37.  
  38.     # Play the audio stream
  39.     pydub.playback.play(audio_stream)
  40.  
  41.     # Clear the audio stream
  42.     audio_stream = pydub.AudioSegment.empty()
  43.  
  44. # Release resources
  45. cap.release()
  46. cv2.destroyAllWindows()

Replies to Untitled rss

Title Name Language When
Re: Untitled Me text 1 Year ago.
Re: Untitled SMa text 1 Year ago.