0
1
mirror of https://github.com/radio95-rnt/RadioPlayer.git synced 2026-02-26 13:52:00 +01:00

some changes

This commit is contained in:
2025-12-27 21:45:18 +01:00
parent 9bd0d88d39
commit 82e8423178
5 changed files with 2248 additions and 122 deletions

View File

@@ -3,6 +3,7 @@ from collections.abc import Sequence
from subprocess import Popen
from dataclasses import dataclass
from pathlib import Path
import tinytag
@dataclass
class Track:
@@ -22,9 +23,6 @@ class Process:
duration: float
class ABC_ProcessManager(abc.ABC):
processes: list[Process]
@abc.abstractmethod
def _get_audio_duration(self, file_path): ...
@abc.abstractmethod
def play(self, track: Track) -> Process: ...
@abc.abstractmethod
@@ -61,13 +59,13 @@ class ProcmanCommunicator(BaseIMCModule):
if int(op) == 0: return {"op": 0, "arg": "pong"}
elif int(op) == 1:
if arg := data.get("arg"): return {"op": 1, "arg": self.procman._get_audio_duration(arg)}
if arg := data.get("arg"): return {"op": 1, "arg": tinytag.TinyTag().get(arg, tags=False).duration}
else: return
elif int(op) == 2:
self.procman.stop_all(data.get("timeout", None))
return {"op": 2}
elif int(op) == 3:
return {"op": 3, "arg": self.procman.processes}
raise NotImplementedError("This feature was removed.")
elif int(op) == 4:
return {"op": 4, "arg": self.procman.anything_playing()}
elif int(op) == 5:

72
modules/ffmpeg_procman.py Normal file
View File

@@ -0,0 +1,72 @@
from . import ABC_ProcessManager, Process, Track, Path, Popen, tinytag
from threading import Lock
import subprocess, time
class ProcessManager(ABC_ProcessManager):
def __init__(self) -> None:
self.lock = Lock()
self.processes: list[Process] = []
def _get_audio_duration(self, file_path: Path):
return tinytag.TinyTag().get(file_path, tags=False).duration
def play(self, track: Track) -> Process:
assert track.path.exists()
cmd = ['ffplay', '-nodisp', '-hide_banner', '-autoexit', '-loglevel', 'quiet']
duration = self._get_audio_duration(track.path.absolute())
if not duration: raise Exception("Failed to get file duration for", track.path)
if track.offset >= duration: track.offset = max(duration - 0.1, 0)
if track.offset > 0: cmd.extend(['-ss', str(track.offset)])
filters = []
if track.fade_in != 0: filters.append(f"afade=t=in:st=0:d={track.fade_in}")
if track.fade_out != 0: filters.append(f"afade=t=out:st={duration - track.fade_out}:d={track.fade_out}")
if filters: cmd.extend(['-af', ",".join(filters)])
cmd.append(str(track.path.absolute()))
pr = Process(Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True), track, time.monotonic(), duration - track.offset)
with self.lock: self.processes.append(pr)
return pr
def anything_playing(self) -> bool:
with self.lock:
self.processes = [p for p in self.processes if p.process.poll() is None]
return bool(self.processes)
def stop_all(self, timeout: float | None = None) -> None:
with self.lock:
for process in self.processes:
process.process.terminate()
try: process.process.wait(timeout)
except subprocess.TimeoutExpired: process.process.kill()
self.processes.clear()
def wait_all(self, timeout: float | None = None) -> None:
with self.lock:
for process in self.processes:
try: process.process.wait(timeout)
except subprocess.TimeoutExpired: process.process.terminate()
self.processes.clear()
procman = ProcessManager()
# This is free and unencumbered software released into the public domain.
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# For more information, please refer to <https://unlicense.org>