Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import asyncio
|
| 4 |
+
import logging
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
import httpx
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from fastapi import BackgroundTasks
|
| 9 |
+
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
+
|
| 13 |
+
class ProcessRequest(BaseModel):
|
| 14 |
+
m3u8_url: str
|
| 15 |
+
chat_id: int
|
| 16 |
+
bot_token: str
|
| 17 |
+
show_title: str
|
| 18 |
+
ep_number: int
|
| 19 |
+
ep_title: str
|
| 20 |
+
|
| 21 |
+
async def process_and_upload(data: ProcessRequest):
|
| 22 |
+
temp_file = f"ep_{data.ep_number}_{int(time.time())}.mp3"
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
logger.info(f"Processing stream: {data.show_title} - Ep {data.ep_number}")
|
| 26 |
+
|
| 27 |
+
# FFmpeg stitches the .m3u8 playlist into an MP3 file
|
| 28 |
+
cmd = [
|
| 29 |
+
"ffmpeg", "-y",
|
| 30 |
+
"-i", data.m3u8_url,
|
| 31 |
+
"-c:a", "libmp3lame", "-b:a", "64k",
|
| 32 |
+
temp_file
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
proc = await asyncio.create_subprocess_exec(
|
| 36 |
+
*cmd,
|
| 37 |
+
stdout=asyncio.subprocess.DEVNULL,
|
| 38 |
+
stderr=asyncio.subprocess.DEVNULL
|
| 39 |
+
)
|
| 40 |
+
await proc.wait()
|
| 41 |
+
|
| 42 |
+
if proc.returncode != 0 or not os.path.exists(temp_file):
|
| 43 |
+
logger.error("FFmpeg execution failed.")
|
| 44 |
+
return
|
| 45 |
+
|
| 46 |
+
# Upload directly to Telegram to save bandwidth
|
| 47 |
+
tg_url = f"https://api.telegram.org/bot{data.bot_token}/sendAudio"
|
| 48 |
+
caption = f"🎧 <b>{data.show_title}</b>\n🔹 Episode {data.ep_number} - {data.ep_title}"
|
| 49 |
+
|
| 50 |
+
async with httpx.AsyncClient(timeout=300.0) as client:
|
| 51 |
+
with open(temp_file, "rb") as f:
|
| 52 |
+
files = {"audio": (f"{data.show_title}_Ep_{data.ep_number}.mp3", f, "audio/mpeg")}
|
| 53 |
+
payload = {
|
| 54 |
+
"chat_id": data.chat_id,
|
| 55 |
+
"caption": caption,
|
| 56 |
+
"performer": "Pocket FM",
|
| 57 |
+
"title": f"Episode {data.ep_number}",
|
| 58 |
+
"parse_mode": "HTML"
|
| 59 |
+
}
|
| 60 |
+
resp = await client.post(tg_url, data=payload, files=files)
|
| 61 |
+
logger.info(f"Telegram status: {resp.status_code}")
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
logger.error(f"Worker task error: {e}")
|
| 65 |
+
|
| 66 |
+
finally:
|
| 67 |
+
# Immediately delete the MP3 to keep disk storage clean
|
| 68 |
+
if os.path.exists(temp_file):
|
| 69 |
+
try:
|
| 70 |
+
os.remove(temp_file)
|
| 71 |
+
except Exception:
|
| 72 |
+
pass
|
| 73 |
+
|
| 74 |
+
# Minimal Gradio interface
|
| 75 |
+
with gr.Blocks(title="Pocket FM Audio Engine") as demo:
|
| 76 |
+
gr.Markdown("## ⚡ Pocket FM Cloud FFmpeg Worker is Running!")
|
| 77 |
+
gr.Markdown("This Space processes .m3u8 streams and delivers episodes to Telegram.")
|
| 78 |
+
|
| 79 |
+
# Expose the FastAPI backend
|
| 80 |
+
app = demo.app
|
| 81 |
+
|
| 82 |
+
@app.post("/convert")
|
| 83 |
+
async def convert_episode(req: ProcessRequest, background_tasks: BackgroundTasks):
|
| 84 |
+
background_tasks.add_task(process_and_upload, req)
|
| 85 |
+
return {"status": "queued", "ep": req.ep_number}
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|