mx / scripts /bake_audio.py
Ethscriptions's picture
Step 5: 配乐音效与混音
68fdb58 verified
Raw History Blame Contribute Delete
976 Bytes
"""Render the procedural soundtrack in headless Chromium and bake it to frontend/public/audio/soundtrack.mp3"""
import asyncio, base64, lameenc
from playwright.async_api import async_playwright
URL = "http://localhost:5173/preview/production/ed5ba841-ed53-4eec-a8c5-93237d26b7ab/?adebug=1"
async def main():
async with async_playwright() as p:
b = await p.chromium.launch()
pg = await b.new_page()
pg.on("console", lambda m: print(m.text[:300]) if "INFO" in m.text else None)
await pg.goto(URL)
await pg.wait_for_function("window.__done === true", timeout=300000)
pcm = base64.b64decode(await pg.evaluate("window.__pcm"))
await b.close()
enc = lameenc.Encoder(); enc.set_bit_rate(192); enc.set_in_sample_rate(48000); enc.set_channels(2); enc.set_quality(2)
mp3 = enc.encode(pcm) + enc.flush()
open("frontend/public/audio/soundtrack.mp3", "wb").write(mp3)
print("bytes", len(mp3))
asyncio.run(main())