To have a certain function execute based on a play postion in a VLC file in python.
setup vlc
import time
import vlc
instance = vlc.Instance('--no-audio', '--fullscreen', '--loop')
player = instance.media_player_new()
media = instance.media_new("file:///home/expo/video/JoHi_20160308_7115.MPG")
player.set_media(media)
player.play()
player.set_fullscreen(True)loop to check seconds, there’s separate functions isFanOn and isSmokeOn to:
def isFanOn(seconds, timecode_s):
start = timecode_s["start"] - smoke_pre
end = timecode_s["end"] + smoke_post
if seconds >= start and seconds <= end:
return Truewhile the video is playing, for every item in the predefined time codes, check if the current time is within range.
while True:
time.sleep(0.1)
if not player.is_playing():
print("LOL")
player.stop()
player.set_time(0)
player.play()
seconds = (player.get_time()/1000)
fan_now_on = False
smoke_now_on = False
for s in timecodes_s:
if isFanOn(seconds, s):
fan_now_on = True
if isSmokeOn(seconds, s):
smoke_now_on = True
print(seconds)import time
import vlc
timecodes = [
{"start": "00:00:05:23", "end": "00:00:35:21"},
{"start": "00:04:15:23", "end": "00:04:45:21"},
{"start": "00:04:51:00", "end": "00:05:00:03"},
{"start": "00:05:16:15", "end": "00:05:26:06"},
{"start": "00:05:41:08", "end": "00:05:53:14"},
{"start": "00:06:12:08", "end": "00:06:23:20"},
{"start": "00:06:45:07", "end": "00:07:17:01"},
{"start": "00:07:37:24", "end": "00:08:00:15"},
{"start": "00:08:10:26", "end": "00:08:45:28"},
{"start": "00:09:01:19", "end": "00:09:11:07"},
{"start": "00:09:23:28", "end": "00:09:41:09"},
{"start": "00:10:41:29", "end": "00:10:50:22"},
{"start": "00:11:22:28", "end": "00:11:33:23"},
{"start": "00:11:55:10", "end": "00:12:00:08"},
{"start": "00:12:29:26", "end": "00:12:37:24"},
{"start": "00:13:16:04", "end": "00:13:35:23"}
]
timecodes_s = []
def getSeconds(str):
split = str.split(":")
value = 0
for i, s in enumerate(split):
if i == 0:
value += float(s) * 60 * 60
elif i == 1:
value += float(s) * 60
elif i == 2:
value += float(s)
elif i == 3:
value += float(s) * 0.01
return value
for tc in timecodes:
start_s = getSeconds(tc["start"])
end_s = getSeconds(tc["end"])
timecodes_s.append({
"start": start_s,
"end": end_s
})
print(timecodes_s)
fan_pin = 24
smoke_pin = 23
fan_on = False
smoke_on = False
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(fan_pin, GPIO.OUT)
GPIO.setup(smoke_pin, GPIO.OUT)
state = False
instance = vlc.Instance('--no-audio', '--fullscreen', '--loop')
player = instance.media_player_new()
media = instance.media_new("file:///home/expo/video/JoHi_20160308_7115.MPG")
player.set_media(media)
player.play()
player.set_fullscreen(True)
smoke_pre = 12
smoke_post = 3
fan_pre = 0
fan_post = 3
def isSmokeOn(seconds, timecode_s):
start = timecode_s["start"] - smoke_pre
end = timecode_s["end"] + smoke_post
if seconds >= start and seconds <= end:
return True
return False
def isFanOn(seconds, timecode_s):
start = timecode_s["start"] - fan_pre
end = timecode_s["end"] + fan_post
if seconds >= start and seconds <= end:
return True
return False
while True:
time.sleep(0.1)
if not player.is_playing():
print("LOL")
player.stop()
player.set_time(0)
player.play()
seconds = (player.get_time()/1000)
fan_now_on = False
smoke_now_on = False
for s in timecodes_s:
if isFanOn(seconds, s):
fan_now_on = True
if isSmokeOn(seconds, s):
smoke_now_on = True
print(seconds)
if fan_now_on is not fan_on:
fan_on = fan_now_on
GPIO.output(fan_pin, fan_now_on)
print(seconds, f"{fan_now_on=}")
if smoke_now_on is not smoke_on:
smoke_on = smoke_now_on
GPIO.output(smoke_pin, smoke_now_on)
print(seconds, f"{smoke_now_on=}")