-rw-r--r-- 1 dede users 10001397 22. Mär
11:57 20250322-115319.m4a -rwxr-xr-x 1 dede users 375493227 22. Mär 11:27 DJI_0002.MP4 -rwxr-xr-x 1 dede users 4241722 22. Mär 11:27 DJI_0003.JPG -rwxr-xr-x 1 dede users 4136838 22. Mär 11:27 DJI_0004.JPG -rwxr-xr-x 1 dede users 4780946 22. Mär 11:27 DJI_0005.JPG -rwxr-xr-x 1 dede users 4677588 22. Mär 11:27 DJI_0006.JPG -rwxr-xr-x 1 dede users 244409158 22. Mär 11:28 DJI_0007.MP4 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# play_on_audio.py
#
# Detlev Ahlgrimm 22.03.2025
import os
import sys
import time
import datetime
import threading
import subprocess
import queue
audio_file = "/10TB2/DJI/2025-03-22/20250322-115319.m4a"
seq = {
"00:00:00.0": ("mplayer:pause", "DJI_0002.MP4"),
"00:01:02.3": ("mplayer:unpause", "..."),
"00:02:21.5": ("ristretto", "DJI_0003.JPG"),
"00:02:26.9": ("ristretto", "DJI_0004.JPG"),
"00:02:30.3": ("ristretto", "DJI_0005.JPG"),
"00:02:33.5": ("ristretto", "DJI_0006.JPG"),
"00:02:42.0": ("mplayer:start", "DJI_0007.MP4"),
"00:03:34.6": None,
}
# ------------------------------------------------------------------
# Wandelt z.B. "00:01:02.3" nach 62.3.
def time_to_secs(t):
h, m, s = t.split(":")
return int(h)*3600 + int(m)+60 + float(s)
# ------------------------------------------------------------------
# Wandelt z.B. 62.3 nach "00:01:02.3".
def secs_to_time(s):
h = int(s / 3600)
m = int((s - h*3600) / 60)
s = s - h*3600 - m*60
if s < 10:
return f"{h:02}:{m:02}:0{s:3.1f}"
return f"{h:02}:{m:02}:{s:4.1f}"
# ------------------------------------------------------------------
# Läuft als Thread.
# Liest aus der stdout-pipe vom mplayer und schreibt Positions-
# Antworten in die audio_pos_queue.
def pipe_print(pipe):
global audio_is_up, audio_length
for line in iter(pipe.readline, b''):
ln = line.decode("utf-8").strip()
#print(">>>>", ln)
if ln.startswith("ANS_LENGTH"):
_, lng = ln.split("=")
audio_length = float(lng)
if ln.startswith("ANS_TIME_POSITION"):
_, pos = ln.split("=")
audio_pos_queue.put(float(pos))
elif ln.startswith("Exiting... (Quit)"):
audio_is_up = False
print("mplayer geschlossen")
elif ln.startswith("Exiting... (End of file)"):
audio_is_up = False
print("mplayer am Ende der Playlist")
elif "Starting playback" in ln:
audio_is_up = True
time.sleep(0.1)
# ----------------------------------------------------------------------
# Öffnet ristretto mit "fn" und liefert die ProzessID zurück.
def open_ristretto(fn):
cmd = ["/usr/bin/ristretto", "-f", fn]
return subprocess.Popen(cmd)
# ----------------------------------------------------------------------
# Öffnet mplayer mit "fn", pausiert das Video sofort und liefert die
# ProzessID zurück.
def open_mplayer_paused(fn):
cmd = ["/usr/bin/mplayer", "-slave", "-quiet", "-fs", fn]
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write(b"pause\n")
p.stdin.flush()
return p
# ----------------------------------------------------------------------
# Setzt das Video des pausierten mplayer-Prozesses fort.
def unpause_mplayer(proc):
proc.stdin.write(b"pause\n")
proc.stdin.flush()
# ----------------------------------------------------------------------
# Öffnet mplayer mit "fn" und liefert die ProzessID zurück.
def start_mplayer(fn):
cmd = ["/usr/bin/mplayer", "-slave", "-quiet", "-fs", fn]
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
return p
# ----------------------------------------------------------------------
# Verteilt seq auf die jeweils zuständigen Funktionen.
def exec_cmd(seq, proc=None):
if seq is None:
return
prog, filename = seq
if prog.startswith("mplayer"):
_, cmd = prog.split(":")
if cmd == "pause":
proc2 = open_mplayer_paused(filename)
time.sleep(.3)
if proc is not None:
proc.kill()
proc = proc2
elif cmd == "unpause":
unpause_mplayer(proc)
elif cmd == "start":
proc2 = start_mplayer(filename)
time.sleep(.3)
if proc is not None:
proc.kill()
proc = proc2
elif prog == "ristretto":
proc2 = open_ristretto(filename)
time.sleep(.3)
if proc is not None:
proc.kill()
proc = proc2
return proc
# ----------------------------------------------------------------------
#
if __name__=="__main__":
"""
p = open_mplayer_paused("DJI_0002.MP4")
time.sleep(10)
unpause_mplayer(p)
sys.exit()
p = open_ristretto("DJI_0003.JPG")
time.sleep(3)
p.kill()
p = open_ristretto("DJI_0004.JPG")
time.sleep(3)
p.kill()
sys.exit()
"""
cmd = ["/usr/bin/mplayer", "-slave", "-quiet", audio_file]
audio_pos_queue = queue.Queue()
audio_is_up = False
audio_process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
worker = threading.Thread(target=pipe_print, name="pipe-print", args=(audio_process.stdout,))
worker.setDaemon(True)
worker.start()
while not audio_is_up:
time.sleep(0.1)
print("running...")
audio_process.stdin.write(b"get_time_length\n")
audio_process.stdin.flush()
pos, audio_length = 0, 10
proc = None
while pos < audio_length:
#print(pos, "/", audio_length, flush=True)
try:
pos = audio_pos_queue.get(timeout=.1)
except queue.Empty:
pass
t = secs_to_time(pos)
if t in seq:
print(pos, t, seq[t])
proc = exec_cmd(seq[t], proc)
time.sleep(0.1) # Sorgt dafür, dass t nur einmal in seq gefunden wird.
if audio_process:
try:
audio_process.stdin.write(b"get_time_pos\n")
audio_process.stdin.flush()
except BrokenPipeError:
break
time.sleep(0.01)
print("killing audio-player")
if audio_process:
audio_process.kill()