loops1, no mess with b1, goes to next rnd video, plays ful length

master
ezn 1 year ago
parent afa2710ff0
commit b936a4e2bf

243
p24.py

@ -0,0 +1,243 @@
import RPi.GPIO as GPIO
import os
import time
import mpv
import random # Import random module to handle random video selection
# Set up GPIO pins for Buttons and LEDs
GPIO.setmode(GPIO.BCM)
BUTTON_PIN_1 = 14
BUTTON_PIN_2 = 15
BUTTON_PIN_3 = 18
BUTTON_PIN_4 = 23
BUTTON_PIN_5 = 24
BUTTON_PIN_6 = 25
BUTTON_PIN_7 = 8
LED_PIN_1 = 2
LED_PIN_2 = 3
LED_PIN_3 = 4
LED_PIN_4 = 17
LED_PIN_5 = 27
LED_PIN_6 = 22
LED_PIN_7 = 10
buttons = [BUTTON_PIN_1, BUTTON_PIN_2, BUTTON_PIN_3, BUTTON_PIN_4, BUTTON_PIN_5, BUTTON_PIN_6, BUTTON_PIN_7]
leds = [LED_PIN_1, LED_PIN_2, LED_PIN_3, LED_PIN_4, LED_PIN_5, LED_PIN_6, LED_PIN_7]
# Set up buttons and LEDs
for button in buttons:
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Buttons connect to GND when pressed
for led in leds:
GPIO.setup(led, GPIO.OUT)
# Define the video and button actions (using custom video names here)
video_folder = "/home/ezn/sorgin"
video_paths = {
1: {"video": "0000-Herensugea.mp4", "leds": [LED_PIN_1], "buttons": [BUTTON_PIN_1], "next_videos": [2], "duration": 92},
2: {"video": "0001-Herensugea.mp4", "leds": [LED_PIN_1], "buttons": [BUTTON_PIN_1], "next_videos": [3], "duration": 150},
3: {"video": "0003-Herensugea.mp4", "leds": [LED_PIN_2, LED_PIN_3, LED_PIN_4, LED_PIN_5], "buttons": [BUTTON_PIN_2, BUTTON_PIN_3, BUTTON_PIN_4, BUTTON_PIN_5], "next_videos": [4, 15, 27, 39], "duration": 150},
4: {"video": "0101-Tartalo.mp4", "leds": [LED_PIN_6, LED_PIN_4], "buttons": [BUTTON_PIN_6, BUTTON_PIN_4], "next_videos": [6, 26], "duration": 352},
5: {"video": "0102B-Xarma.mp4", "leds": [LED_PIN_5, LED_PIN_2], "buttons": [BUTTON_PIN_5, BUTTON_PIN_2], "next_videos": [41, 7], "duration": 150},
6: {"video": "0102-Xarma.mp4", "leds": [LED_PIN_2], "buttons": [BUTTON_PIN_2], "next_videos": [7], "duration": 150},
7: {"video": "0103-Xarma-Tartalo.mp4", "leds": [LED_PIN_5, LED_PIN_3], "buttons": [BUTTON_PIN_5, BUTTON_PIN_3], "next_videos": [8, 32], "duration": 150},
8: {"video": "0104-IpariSuri-Maider.mp4", "leds": [LED_PIN_3, LED_PIN_2], "buttons": [BUTTON_PIN_3, BUTTON_PIN_2], "next_videos": [44, 10], "duration": 150},
9: {"video": "0105E-EYES-IpariSuri.mp4", "leds": [LED_PIN_4, LED_PIN_7, LED_PIN_1], "buttons": [BUTTON_PIN_4, BUTTON_PIN_7, BUTTON_PIN_1], "next_videos": [19, 11, 10], "duration": 150},
10: {"video": "0105-IpariSuri-Maider-Xarma-Tartalo.mp4", "leds": [LED_PIN_1, LED_PIN_4, LED_PIN_7], "buttons": [BUTTON_PIN_1, BUTTON_PIN_4, BUTTON_PIN_7], "next_videos": [9, 19, 11], "duration": 150},
11: {"video": "0106-Mairu.mp4", "leds": [LED_PIN_4, LED_PIN_7], "buttons": [BUTTON_PIN_4, BUTTON_PIN_7], "next_videos": [12, 22], "duration": 150},
12: {"video": "0107-Mairu-Oxik.mp4", "leds": [LED_PIN_1], "buttons": [BUTTON_PIN_1], "next_videos": [13], "duration": 150},
13: {"video": "0199-END.mp4", "leds": [], "buttons": [], "next_videos": [], "duration": 150},
14: {"video": "0201B-Maider.mp4", "leds": [LED_PIN_5], "buttons": [BUTTON_PIN_5], "next_videos": [16], "duration": 150},
15: {"video": "0201-Maider.mp4", "leds": [LED_PIN_5], "buttons": [BUTTON_PIN_5], "next_videos": [16], "duration": 150},
16: {"video": "0202-MaiderIpariSuri.mp4", "leds": [LED_PIN_2, LED_PIN_7], "buttons": [BUTTON_PIN_2, BUTTON_PIN_7], "next_videos": [17, 28], "duration": 150},
17: {"video": "0203-Tartalo.mp4", "leds": [LED_PIN_5], "buttons": [BUTTON_PIN_5], "next_videos": [18], "duration": 150},
18: {"video": "0204-Tartalo-IpariSuri-Maider.mp4", "leds": [LED_PIN_6, LED_PIN_4], "buttons": [BUTTON_PIN_6, BUTTON_PIN_4], "next_videos": [10, 19], "duration": 150},
19: {"video": "0205-Oxik.mp4", "leds": [LED_PIN_2, LED_PIN_7], "buttons": [BUTTON_PIN_2, BUTTON_PIN_7], "next_videos": [20, 43], "duration": 150},
20: {"video": "0206-Oxik-Tartalo.mp4", "leds": [LED_PIN_7, LED_PIN_3], "buttons": [BUTTON_PIN_7, BUTTON_PIN_3], "next_videos": [21, 35], "duration": 150},
21: {"video": "0207-XarmaMairu.mp4", "leds": [LED_PIN_2], "buttons": [BUTTON_PIN_2], "next_videos": [22], "duration": 150},
22: {"video": "0208-ALL.mp4", "leds": [LED_PIN_2, LED_PIN_1], "buttons": [BUTTON_PIN_2, BUTTON_PIN_1], "next_videos": [25, 23], "duration": 150},
23: {"video": "0208E-EYES-Mairu.mp4", "leds": [LED_PIN_1], "buttons": [BUTTON_PIN_1], "next_videos": [24], "duration": 150},
24: {"video": "0208E-EYES-Xarma.mp4", "leds": [LED_PIN_2], "buttons": [BUTTON_PIN_2], "next_videos": [25], "duration": 150},
25: {"video": "0299-END.mp4", "leds": [], "buttons": [], "next_videos": [], "duration": 150},
26: {"video": "0301B-Oxik.mp4", "leds": [LED_PIN_6, LED_PIN_7], "buttons": [BUTTON_PIN_6, BUTTON_PIN_7], "next_videos": [5, 29], "duration": 150},
27: {"video": "0301-Oxik.mp4", "leds": [LED_PIN_7, LED_PIN_6], "buttons": [BUTTON_PIN_7, BUTTON_PIN_6], "next_videos": [30, 40], "duration": 150},
28: {"video": "0302B-Mairu.mp4", "leds": [LED_PIN_4], "buttons": [BUTTON_PIN_4], "next_videos": [31], "duration": 150},
29: {"video": "0302C-Mairu.mp4", "leds": [LED_PIN_2, LED_PIN_4], "buttons": [BUTTON_PIN_2, BUTTON_PIN_4], "next_videos": [43, 31], "duration": 150},
30: {"video": "0302-Mairu.mp4", "leds": [LED_PIN_4], "buttons": [BUTTON_PIN_4], "next_videos": [31], "duration": 150},
31: {"video": "0303-Oxik-Mairu.mp4", "leds": [LED_PIN_6, LED_PIN_2], "buttons": [BUTTON_PIN_6, BUTTON_PIN_2], "next_videos": [32, 43], "duration": 150},
32: {"video": "0304-Xarma-Tartalo-Maider.mp4", "leds": [LED_PIN_7, LED_PIN_3], "buttons": [BUTTON_PIN_7, BUTTON_PIN_3], "next_videos": [34, 44], "duration": 150},
33: {"video": "0305E-EYES-Tartalo.mp4", "leds": [LED_PIN_6, LED_PIN_5, LED_PIN_1], "buttons": [BUTTON_PIN_6, BUTTON_PIN_5, BUTTON_PIN_1], "next_videos": [21, 35, 33], "duration": 150},
34: {"video": "0305-Xarma-Tartalo-Maider-Mairu-Oxik.mp4", "leds": [LED_PIN_6, LED_PIN_5, LED_PIN_1], "buttons": [BUTTON_PIN_6, BUTTON_PIN_5, BUTTON_PIN_1], "next_videos": [21, 35, 33], "duration": 150},
35: {"video": "0306-IpariSuri.mp4", "leds": [LED_PIN_5, LED_PIN_3], "buttons": [BUTTON_PIN_5, BUTTON_PIN_3], "next_videos": [45, 36], "duration": 150},
36: {"video": "0307-ALL.mp4", "leds": [LED_PIN_3, LED_PIN_1], "buttons": [BUTTON_PIN_3, BUTTON_PIN_1], "next_videos": [38, 37], "duration": 150},
37: {"video": "0307E-EYES-Maider.mp4", "leds": [LED_PIN_3, LED_PIN_1], "buttons": [BUTTON_PIN_3, BUTTON_PIN_1], "next_videos": [38, 36], "duration": 150},
38: {"video": "0399-END.mp4", "leds": [], "buttons": [], "next_videos": [], "duration": 150},
39: {"video": "0401-IpariSuri.mp4", "leds": [LED_PIN_2, LED_PIN_6], "buttons": [BUTTON_PIN_2, BUTTON_PIN_6], "next_videos": [17, 40], "duration": 150},
40: {"video": "0402-Xarma.mp4", "leds": [LED_PIN_2, LED_PIN_5], "buttons": [BUTTON_PIN_2, BUTTON_PIN_5], "next_videos": [5, 41], "duration": 150},
41: {"video": "0403-Xarma-IpariSuri.mp4", "leds": [LED_PIN_7], "buttons": [BUTTON_PIN_7], "next_videos": [43], "duration": 150},
42: {"video": "0404E-EYES-Oxik.mp4", "leds": [LED_PIN_1, LED_PIN_2, LED_PIN_3, LED_PIN_4], "buttons": [BUTTON_PIN_1, BUTTON_PIN_2, BUTTON_PIN_3, BUTTON_PIN_4], "next_videos": [43, 11, 44, 20], "duration": 150},
43: {"video": "0404-Oxik-Tartalo-Mairu.mp4", "leds": [LED_PIN_1, LED_PIN_2, LED_PIN_3, LED_PIN_4], "buttons": [BUTTON_PIN_1, BUTTON_PIN_2, BUTTON_PIN_3, BUTTON_PIN_4], "next_videos": [42, 11, 44, 20], "duration": 150},
44: {"video": "0405-Maider.mp4", "leds": [LED_PIN_5, LED_PIN_3], "buttons": [BUTTON_PIN_5, BUTTON_PIN_3], "next_videos": [36, 45], "duration": 150},
45: {"video": "0406-MaiderIpariSuri.mp4", "leds": [LED_PIN_4], "buttons": [BUTTON_PIN_4], "next_videos": [46], "duration": 150},
46: {"video": "0499-END.mp4", "leds": [], "buttons": [], "next_videos": [], "duration": 150},
}
# Initialize mpv player with fullscreen mode
player = mpv.MPV(ytdl=True, keep_open=True, fullscreen=True)
# Function to play a video using mpv
def play_video(video_name, loop=False):
video_path = os.path.join(video_folder, video_name)
if os.path.exists(video_path):
print(f"Now playing {video_name}")
player.play(video_path)
# Loop the video indefinitely if required
if loop:
player.loop = True # Set loop to True for looping video
else:
player.loop = False # Disable looping for non-looping videos
# Wait until the video finishes playing
while not player.idle:
time.sleep(0.1) # Wait until the video finishes playing
else:
print(f"Video {video_name} not found.")
# Function to activate buttons and LEDs for the next video
def activate_next_buttons_and_leds(buttons_to_enable, leds_to_enable):
# Reset all buttons and LEDs before enabling new ones
for button in buttons:
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Disable buttons (pull-down to avoid false presses)
for led in leds:
GPIO.output(led, GPIO.LOW) # Turn off all LEDs
# Enable the next buttons and LEDs
for button in buttons_to_enable:
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Enable next choices (pull-up to detect presses)
for led in leds_to_enable:
GPIO.output(led, GPIO.HIGH) # Turn on LEDs
# Function to handle the chain of videos
def handle_video_chain(video_id):
# Get the current video details from the dictionary
video_info = video_paths.get(video_id)
if not video_info:
print(f"Error: Video with ID {video_id} not found.")
return
print(f"Playing video: {video_info['video']}")
if video_id == 1: # Special case for video 0000-Herensugea.mp4 to loop
play_video(video_info["video"], loop=True) # Loop video 0000-Herensugea.mp4
else:
play_video(video_info["video"], loop=False) # Play without looping for other videos
activate_before_video_ends(video_info)
wait_for_button_press(video_info)
# Function to wait for a button press and transition to the next video
def wait_for_button_press(video_info):
last_button_press = None
no_press_count = 0 # This will track how many consecutive times we've had no button press
# Timeout period (2 minutes before the video ends)
timeout = 120 # 120 seconds for 2 minutes before the video ends
start_time = time.time()
print(f"Waiting for button press from {video_info['buttons']}...")
while True:
for button in video_info["buttons"]:
# Check if the button is pressed (GPIO input LOW means the button is pressed)
if GPIO.input(button) == GPIO.LOW: # Button pressed (low state due to pull-up)
if last_button_press != button: # Ensure the button press is not repeated continuously
last_button_press = button
print(f"Button {button} pressed.")
time.sleep(0.3) # Debounce delay to ensure one press is detected
break
else:
# If no button pressed yet, check the timeout logic
if time.time() - start_time >= timeout:
no_press_count += 1
print(f"No button pressed. Timeout count: {no_press_count}")
# If we've had two consecutive timeouts, restart from Video 1
if no_press_count >= 2:
print("Two consecutive timeouts detected. Restarting from Video 1.")
handle_video_chain(1) # Restart from Video 1
return # Exit after restarting the system
# Reset the timer for the next loop if there's only one timeout
start_time = time.time()
if last_button_press:
break # Exit the loop once a button has been pressed
time.sleep(0.1) # Check for button press every 0.1 seconds
# If button was pressed successfully, reset no_press_count and proceed
no_press_count = 0
# Proceed with the next video from the selected button
if video_info["buttons"]: # Ensure there are buttons to reference
button_index = video_info["buttons"].index(last_button_press)
next_video_id = video_info["next_videos"][button_index] if button_index < len(video_info["next_videos"]) else None
if next_video_id:
handle_video_chain(next_video_id)
else:
print("End of video chain reached. Restarting from Video 1.")
handle_video_chain(1) # Restart from Video 1 if no next video
else:
print("No buttons associated with this video. Restarting from Video 1.")
handle_video_chain(1) # Restart from Video 1
# Function to activate buttons and LEDs 2 minutes before the video ends
def activate_before_video_ends(video_info):
video_duration = video_info["duration"] # Get the duration from the video_info dictionary
# If it's the first video (0000-Herensugea.mp4), don't activate buttons until it's about to end
if video_info["video"] == "0000-Herensugea.mp4":
return # Don't activate buttons/LEDs for the first video
# Wait for the 2 minutes before the video ends
time_to_activate = video_duration - 120 # Time to activate next buttons and LEDs (2 minutes before end)
print(f"Video duration: {video_duration} seconds. Activating next buttons and LEDs at {time_to_activate} seconds.")
time.sleep(time_to_activate) # Wait until we're within 2 minutes of the video ending
# Once we're within 2 minutes of the video ending, activate buttons and LEDs
activate_next_buttons_and_leds(video_info["buttons"], video_info["leds"])
# Function to start looping Video 0000-Herensugea.mp4
def loop_video():
print("Starting Video 0000-Herensugea.mp4...")
GPIO.output(LED_PIN_1, GPIO.HIGH) # Turn on LED 1
# Ensure all other LEDs are off
for led in [LED_PIN_2, LED_PIN_3, LED_PIN_4, LED_PIN_5]:
GPIO.output(led, GPIO.LOW)
# Set loop behavior and start playing video 0000-Herensugea.mp4
play_video("0000-Herensugea.mp4", loop=True) # Loop the first video
handle_video_chain(1) # Start handling button presses after video 0000-Herensugea.mp4
# Main function to run the system
def start_system():
try:
loop_video() # Start by looping Video 0000-Herensugea.mp4
except KeyboardInterrupt:
print("Program interrupted, cleaning up GPIO.")
GPIO.cleanup()
# Start the video chain with the first video
#def start_system():
# try:
# print("Starting video system...")
# handle_video_chain(1) # Start with the first video
# except KeyboardInterrupt:
# print("Program interrupted, cleaning up GPIO.")
# GPIO.cleanup()
if __name__ == "__main__":
start_system()
Loading…
Cancel
Save