-
Notifications
You must be signed in to change notification settings - Fork 8
/
mp4museum.py
107 lines (86 loc) · 3.07 KB
/
mp4museum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# mp4museum v6 unified - july 2023
# (c) julius schmiedel - http://mp4museum.org
import time, vlc, os, glob
import RPi.GPIO as GPIO
import subprocess
# read audio device config
audiodevice = "0"
if os.path.isfile('/boot/alsa.txt'):
f = open('/boot/alsa.txt', 'r')
audiodevice = f.read(1)
# 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
# with code to filter interference / static discharges
def buttonPause(channel):
inputfilter = 0
for x in range(0,200):
if GPIO.input(11):
inputfilter = inputfilter + 1
time.sleep(.001)
if (inputfilter > 50):
player.pause()
def buttonNext(channel):
inputfilter = 0
for x in range(0,200):
if GPIO.input(13):
inputfilter = inputfilter + 1
time.sleep(.001)
if (inputfilter > 50):
player.stop()
# play media with vlc
def vlc_play(source):
if("loop." in source):
vlc_instance = vlc.Instance('--input-repeat=999999999 -q -A alsa --alsa-audio-device hw:' + audiodevice)
else:
vlc_instance = vlc.Instance('-q -A alsa --alsa-audio-device hw:'+ audiodevice)
global player
player = vlc_instance.media_player_new()
media = vlc_instance.media_new(source)
player.set_media(media)
player.play()
time.sleep(1)
current_state = player.get_state()
while current_state == 3 or current_state == 4:
time.sleep(.01)
current_state = player.get_state()
media.release()
player.release()
# find a file, and if found, return its path (for sync)
def search_file(file_name):
# Use glob to find files matching the pattern in both directories
file_path_media = f'/media/*/{file_name}'
file_path_boot = f'/boot/{file_name}'
matching_files = glob.glob(file_path_media) + glob.glob(file_path_boot)
if matching_files:
# Return the first found file name
return matching_files[0]
# Return False if the file is not found
return False
# *** run player ****
# start player twice to make sure it is working
# seems weird but works
vlc_play("/home/pi/mp4museum-boot.mp4")
vlc_play("/home/pi/mp4museum-boot.mp4")
# please do not remove my logo screen
vlc_play("/home/pi/mp4museum.mp4")
# add event listener which reacts to GPIO signal
GPIO.add_event_detect(11, GPIO.RISING, callback = buttonPause, bouncetime = 234)
GPIO.add_event_detect(13, GPIO.RISING, callback = buttonNext, bouncetime = 1234)
# check for sync mode instructions
enableSync = search_file("sync-leader.txt")
syncFile = search_file("sync.mp4")
if syncFile and enableSync:
print("Sync Mode LEADER:" + syncFile)
subprocess.run(["omxplayer-sync", "-u", "-m", syncFile])
enableSync = search_file("sync-player.txt")
syncFile = search_file("sync.mp4")
if syncFile and enableSync:
print("Sync Mode PLAYER:" + syncFile)
subprocess.run(["omxplayer-sync", "-u", "-l", syncFile])
# the loop
while(1):
for file in sorted(glob.glob(r'/media/*/*.*')):
vlc_play(file)