#!/usr/bin/python

# mp4museum.org by julius schmiedel 2019

import os
import sys
import glob
from subprocess import Popen, PIPE
import RPi.GPIO as GPIO

FNULL = open(os.devnull, "w")

# setup GPIO pin
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(13, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

# functions to be called by event listener
def buttonPause(channel):
	player.stdin.write("p")

def buttonNext(channel):
	player.stdin.write("q")

# add event listener
GPIO.add_event_detect(11, GPIO.FALLING, callback = buttonPause, bouncetime = 234)
GPIO.add_event_detect(13, GPIO.FALLING, callback = buttonNext, bouncetime = 1234)

# please do not remove my logo screen
player = Popen(['omxplayer', '--adev', 'both', '/home/pi/mp4museum.mp4'],stdin=PIPE,stdout=FNULL)
player.wait()

# the loop
while(1):
	for files in sorted(glob.glob(r'/media/*/*.mp4')):
		player = Popen(['omxplayer','--adev', 'both',files],stdin=PIPE,stdout=FNULL)
		player.wait()

[((14, 0, 14, 24), 'RPi.GPIO.setmode', 'GPIO.setmode', ({(14, 13, 14, 23): 'GPIO.BOARD'}, {}), '(GPIO.BOARD)', True, 'import RPi.GPIO as GPIO\n'), ((15, 0, 15, 53), 'RPi.GPIO.setup', 'GPIO.setup', (), '', True, 'import RPi.GPIO as GPIO\n'), ((16, 0, 16, 53), 'RPi.GPIO.setup', 'GPIO.setup', (), '', True, 'import RPi.GPIO as GPIO\n'), ((26, 0, 26, 81), 'RPi.GPIO.add_event_detect', 'GPIO.add_event_detect', (), '', True, 'import RPi.GPIO as GPIO\n'), ((27, 0, 27, 81), 'RPi.GPIO.add_event_detect', 'GPIO.add_event_detect', (), '', True, 'import RPi.GPIO as GPIO\n'), ((30, 9, 30, 97), 'subprocess.Popen', 'Popen', (), '', False, 'from subprocess import Popen, PIPE\n'), ((35, 21, 35, 49), 'glob.glob', 'glob.glob', ({(35, 31, 35, 48): '"""/media/*/*.mp4"""'}, {}), "('/media/*/*.mp4')", False, 'import glob\n'), ((36, 11, 36, 78), 'subprocess.Popen', 'Popen', (), '', False, 'from subprocess import Popen, PIPE\n')]