Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.2 kB
1
Indexable
Never
import cv2
import json
import datetime
import time
import pygame
import numpy as np
import sys

# Inicializa Pygame
pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

def load_media_schedule():
    with open('schedule.json', 'r') as f:
        return json.load(f)

def play_media(file_path, media_type):
    if media_type == "video":
        cap = cv2.VideoCapture(file_path)
        if not cap.isOpened():
            print("Error: No se pudo abrir el video.")
        while cap.isOpened():
            ret, frame = cap.read()
            if ret:
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                frame = np.rot90(frame)
                frame = pygame.surfarray.make_surface(frame)
                for event in pygame.event.get():
                    if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                        cap.release()
                        cv2.destroyAllWindows()
                        pygame.quit()
                        sys.exit()
                screen.blit(frame, (0, 0))
                pygame.display.update()
            else:
                print("Fin del video, re-looping.")
                cap.set(cv2.CAP_PROP_POS_FRAMES, 0)  # Re-loop the video
    elif media_type == "image":
        image = pygame.image.load(file_path)
        image_rect = image.get_rect(center=(screen.get_width()//2, screen.get_height()//2))
        while True:
            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()
            screen.blit(image, image_rect)
            pygame.display.update()
            time.sleep(0.1)  # Update display every 0.1 seconds to keep the app responsive

def play_default_image():
    """Function to display a default image when no other media is scheduled."""
    default_image_path = 'default.png'
    image = pygame.image.load(default_image_path)
    image_rect = image.get_rect(center=(screen.get_width()//2, screen.get_height()//2))
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key is pygame.K_ESCAPE):
                pygame.quit()
                sys.exit()
        screen.blit(image, image_rect)
        pygame.display.update()
        time.sleep(0.1)  # Keep the display updated

schedule = load_media_schedule()

while True:
    current_time = datetime.datetime.now().time()
    found_media = False
    for item in schedule["media"]:
        start_time = datetime.datetime.strptime(item["start_time"], '%H:%M').time()
        end_time = datetime.datetime.strptime(item["end_time"], '%H:%M').time()
        if start_time <= current_time <= end_time:
            found_media = True
            print(f"Reproduciendo: {item['file']}")
            play_media(item["file"], item["type"])
            break
    if not found_media:
        print("No hay medios programados para este momento, mostrando imagen por defecto.")
        play_default_image()
    time.sleep(60)  # Espera 60 segundos antes de revisar el horario nuevamente
Leave a Comment