# video_stream.py import cv2 import threading # URL kamera IP menggunakan RTSP rtsp_url = "rtsp://admin:LKAYXK@192.168.128.80:554" # Variabel untuk menyimpan proses streaming video video_capture = None video_lock = threading.Lock() def video_stream_thread(): global video_capture try: # Membuka stream video video_capture = cv2.VideoCapture(rtsp_url) video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 400) # Atur lebar frame menjadi 640 pixel video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 400) # Atur tinggi frame menjadi 480 pixel while True: ret, frame = video_capture.read() if not ret: break # Encode frame sebagai JPEG ret, jpeg = cv2.imencode('.jpg', frame) if not ret: continue # Mengirim frame sebagai byte frame_bytes = jpeg.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n') except Exception as e: print(f"Error streaming video: {e}") finally: # Membersihkan sumber daya if video_capture is not None: video_capture.release() video_capture = None # Fungsi untuk memulai streaming video dalam thread terpisah def start_video_stream(): global video_capture if video_capture is None: video_thread = threading.Thread(target=video_stream_thread) video_thread.start() # Fungsi untuk mendapatkan generator streaming video def get_video_stream(): return video_stream_thread()