Untitled
unknown
plain_text
2 years ago
4.0 kB
7
Indexable
{
"media": [
{
"type": "video",
"file": "videoplayback.mp4",
"start_time": "11:00:00",
"end_time": "13:15:55"
},
{
"type": "image",
"file": "image1.png",
"start_time": "13:15:58",
"end_time": "13:50:00"
}
]
}
import cv2
import json
import datetime
import time
import pygame
import numpy as np
import sys
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
def load_media_schedule():
try:
with open('schedule.json', 'r') as f:
return json.load(f)
except FileNotFoundError:
print("Error: Archivo JSON no encontrado.")
return None
def play_video(file_path, fps, end_time):
cap = cv2.VideoCapture(file_path)
if not cap.isOpened():
print(f"Error: No se pudo abrir el video {file_path}.")
return
delay = int(1000 / fps)
while cap.isOpened() and datetime.datetime.now().time() < end_time:
ret, frame = cap.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = np.rot90(frame)
frame = pygame.surfarray.make_surface(frame)
screen.blit(frame, (0, 0))
pygame.display.update()
pygame.time.wait(delay)
else:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
if handle_pygame_events():
cap.release()
return # Ensure to exit the function when an event demands it
def play_image(file_path, end_time):
image = pygame.image.load(file_path)
image_rect = image.get_rect(center=(screen.get_width()//2, screen.get_height()//2))
while datetime.datetime.now().time() < end_time:
screen.blit(image, image_rect)
pygame.display.update()
if handle_pygame_events():
return # Exit the function on events like quit or escape
def handle_pygame_events():
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
sys.exit(0)
return False
def play_default_image(default_image_path):
try:
image = pygame.image.load(default_image_path)
image_rect = image.get_rect(center=(screen.get_width()//2, screen.get_height()//2))
while True:
screen.blit(image, image_rect)
pygame.display.update()
if handle_pygame_events() or check_for_new_media():
break
except pygame.error:
print(f"Error: No se pudo cargar la imagen {default_image_path}.")
pygame.quit()
sys.exit(0)
def check_for_new_media():
current_time = datetime.datetime.now().time()
for item in schedule["media"]:
start_time = datetime.datetime.strptime(item["start_time"], '%H:%M:%S').time()
if start_time > current_time:
return True
return False
schedule = load_media_schedule()
if not schedule:
play_default_image('default.png')
current_media = None
while True:
schedule = load_media_schedule() # Reload the schedule on each loop
current_time = datetime.datetime.now().time()
found_media = False
for item in schedule["media"]:
start_time = datetime.datetime.strptime(item["start_time"], '%H:%M:%S').time()
end_time = datetime.datetime.strptime(item["end_time"], '%H:%M:%S').time()
if start_time <= current_time <= end_time:
if current_media != item:
current_media = item
if item["type"] == "video":
play_video(item["file"], item.get("fps", 30), end_time)
elif item["type"] == "image":
play_image(item["file"], end_time)
break
found_media = True
if not found_media:
current_media = None
play_default_image('default.png')
time.sleep(1) # Small sleep to reduce CPU usageEditor is loading...
Leave a Comment