diff --git a/Dockerfile b/Dockerfile index b08cb05..5c8e9d4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM python:3.11-slim WORKDIR /app -RUN apt-get update && apt-get install -y tzdata && \ +RUN apt-get update && apt-get install -y tzdata ffmpeg && \ ln -snf /usr/share/zoneinfo/Europe/Warsaw /etc/localtime && echo Europe/Warsaw > /etc/timezone && \ apt-get clean diff --git a/main.py b/main.py index 9381688..137c0b5 100644 --- a/main.py +++ b/main.py @@ -612,6 +612,30 @@ def podcast_rss(station: str, slug_with_ext: str, request: Request, db: Session return Response(status_code=404) return Response(content=xml, media_type="application/xml") +def iter_stream_transcode(target_url): + import subprocess + cmd = [ + "ffmpeg", + "-reconnect", "1", + "-reconnect_streamed", "1", + "-reconnect_delay_max", "5", + "-loglevel", "error", + "-i", target_url, + "-c:a", "libmp3lame", + "-b:a", "320k", + "-f", "mp3", + "-" + ] + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) + try: + while True: + chunk = process.stdout.read(8192) + if not chunk: + break + yield chunk + finally: + process.kill() + def iter_stream(target_url): import requests with requests.get(target_url, stream=True, timeout=10) as r: @@ -634,8 +658,8 @@ def live_stream_redirect(station: str, db: Session = Depends(db_session)): if station == "radio357": config_url = db.query(Config).filter_by(key="r357_live_url").first() target_url = config_url.value if config_url else "https://stream.rcs.revma.com/ye5kghkgcm0uv" - # Proxy strumienia! Zwracamy jako audio/mpeg żeby ominąć unwrapper - return StreamingResponse(iter_stream(target_url), media_type="audio/mpeg") + # Proxy + Transkodowanie AAC -> MP3 w locie! + return StreamingResponse(iter_stream_transcode(target_url), media_type="audio/mpeg") elif station == "rns": config_url = db.query(Config).filter_by(key="rns_live_url").first()