#!/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()

[((202, 226), 'RPi.GPIO.setmode', 'GPIO.setmode', (['GPIO.BOARD'], {}), '(GPIO.BOARD)\n', (214, 226), True, 'import RPi.GPIO as GPIO\n'), ((227, 278), 'RPi.GPIO.setup', 'GPIO.setup', (['(11)', 'GPIO.IN'], {'pull_up_down': 'GPIO.PUD_DOWN'}), '(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)\n', (237, 278), True, 'import RPi.GPIO as GPIO\n'), ((281, 332), 'RPi.GPIO.setup', 'GPIO.setup', (['(13)', 'GPIO.IN'], {'pull_up_down': 'GPIO.PUD_DOWN'}), '(13, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)\n', (291, 332), True, 'import RPi.GPIO as GPIO\n'), ((503, 580), 'RPi.GPIO.add_event_detect', 'GPIO.add_event_detect', (['(11)', 'GPIO.FALLING'], {'callback': 'buttonPause', 'bouncetime': '(234)'}), '(11, GPIO.FALLING, callback=buttonPause, bouncetime=234)\n', (524, 580), True, 'import RPi.GPIO as GPIO\n'), ((585, 662), 'RPi.GPIO.add_event_detect', 'GPIO.add_event_detect', (['(13)', 'GPIO.FALLING'], {'callback': 'buttonNext', 'bouncetime': '(1234)'}), '(13, GPIO.FALLING, callback=buttonNext, bouncetime=1234)\n', (606, 662), True, 'import RPi.GPIO as GPIO\n'), ((715, 809), 'subprocess.Popen', 'Popen', (["['omxplayer', '--adev', 'both', '/home/pi/mp4museum.mp4']"], {'stdin': 'PIPE', 'stdout': 'FNULL'}), "(['omxplayer', '--adev', 'both', '/home/pi/mp4museum.mp4'], stdin=PIPE,\n    stdout=FNULL)\n", (720, 809), False, 'from subprocess import Popen, PIPE\n'), ((861, 888), 'glob.glob', 'glob.glob', (['"""/media/*/*.mp4"""'], {}), "('/media/*/*.mp4')\n", (870, 888), False, 'import glob\n'), ((903, 974), 'subprocess.Popen', 'Popen', (["['omxplayer', '--adev', 'both', files]"], {'stdin': 'PIPE', 'stdout': 'FNULL'}), "(['omxplayer', '--adev', 'both', files], stdin=PIPE, stdout=FNULL)\n", (908, 974), False, 'from subprocess import Popen, PIPE\n')]