import cv2
from multiprocessing.pool import ThreadPool
import multiprocessing
import subprocess
def show_stream(stream_name: str, win_name:str, sleep_time = -1):
print(f"show stream, name = {stream_name}, win_name = {win_name}, sleep_time = {sleep_time}")
filename = stream_name
cap = cv2.VideoCapture(filename)
assert cap.isOpened(), f'Failed to open {filename}'
w = int(cap.get(cv2.CAP_PROP_frame_WIDTH))
h = int(cap.get(cv2.CAP_PROP_frame_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS) # 30 FPS fallback
frames = int(cap.get(cv2.CAP_PROP_frame_COUNT)) # infinite stream fallback
m_sec = cap.get(cv2.CAP_PROP_POS_MSEC)
total_time = frames / fps
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
print(w, h, fps, frames, fourcc, m_sec/1000, total_time)
# cap.release()
n = 0
read = 1
t_start = time.time()
while cap.isOpened() and n < frames:
# lock.acquire()
# logger.info(f"========== {threading.current_thread().name}, {threading.current_thread().ident}")
t1 = time.time()
n += 1
# _, self.imgs[index] = cap.read()
cap.grab()
if n % read == 0:
success, im = cap.retrieve()
if success:
cv2.imshow(win_name, im)
cv2.waitKey(1)
imgs = im if success else imgs * 0
# lock.release()
if sleep_time < 0:
time.sleep(1.0 / fps) # wait time
t2 = time.time()
print(f"{n} th frame read time = {t2 - t1} s, thread = {current_thread().name}")
t_end = time.time()
print("total_time = ", t_end - t_start)
filename = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov"
thread1 = multiprocessing.Process(target=show_stream, args=(filename, "111", -1,), name="thread111")
thread2 = multiprocessing.Process(target=show_stream, args=(filename, "222", 0,), name = "thread222")
thread3 = multiprocessing.Process(target=show_stream, args=(filename, "333", 0.036,), name = "thread333")
thread1.start()
thread2.start()
thread3.start()
# while True:
# if thread1.is_alive():
# print("thread1 is alive")
# else:
# print("thread1 dead")
# break
# if thread2.is_alive():
# print("thread2 is alive")
# else:
# print("thread2 dead")
# break
# time.sleep(1)
print("main end")