Untitled
unknown
plain_text
2 years ago
4.7 kB
6
Indexable
import pygame
import json
import datetime
import time
from moviepy.editor import VideoFileClip
import sys
def initialize_pygame():
pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
return screen
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(screen, file_path, end_time, schedule):
try:
clip = VideoFileClip(file_path, fps_source="fps")
fps = clip.fps
frame_interval = 1 / fps
start_time = time.time()
for i, frame in enumerate(clip.iter_frames(dtype='uint8')):
current_time = datetime.datetime.now().time()
if current_time >= end_time:
break
screen.fill((0, 0, 0)) # Llena la pantalla de negro
frame = pygame.surfarray.make_surface(frame.swapaxes(0, 1))
screen.blit(frame, (0, 0))
pygame.display.flip()
expected_time = start_time + i * frame_interval
delay = max(0, expected_time - time.time())
pygame.time.wait(int(delay * 1000))
if handle_pygame_events():
return
except Exception as e:
print(f"Error al reproducir el video: {e}")
raise
finally:
clip.close()
def play_image(screen, file_path, end_time, schedule):
try:
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.fill((0, 0, 0)) # Llena la pantalla de negro
screen.blit(image, image_rect)
pygame.display.update()
if handle_pygame_events():
return
except Exception as e:
print(f"Error al cargar la imagen: {e}")
raise
def play_default_image(screen, default_image_path, schedule):
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.fill((0, 0, 0)) # Llena la pantalla de negro
screen.blit(image, image_rect)
pygame.display.update()
if handle_pygame_events() or check_for_new_media(schedule):
break
except pygame.error as e:
print(f"Error: No se pudo cargar la imagen {default_image_path}. Detalles del error: {e}")
raise
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()
return False
def check_for_new_media(schedule):
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
def main_loop():
screen = initialize_pygame()
schedule = load_media_schedule()
if not schedule:
play_default_image(screen, 'default.png', schedule)
current_media = None
while True:
schedule = load_media_schedule() # Ensure the schedule is up-to-date
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(screen, item["file"], end_time, schedule)
elif item["type"] == "image":
play_image(screen, item["file"], end_time, schedule)
break
found_media = True
if not found_media:
current_media = None
play_default_image(screen, 'default.png', schedule)
time.sleep(1) # Small sleep to reduce CPU usage
if __name__ == "__main__":
while True:
try:
main_loop()
except Exception as e:
print(f"Se detectó un error no manejado: {e}. Reiniciando...")
pygame.quit()
time.sleep(5) # Wait a bit before restarting
initialize_pygame() # Reinitialize pygame after an error
Editor is loading...
Leave a Comment