Spaces:
Running on Zero
Running on Zero
File size: 2,123 Bytes
1b6515e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | """Prepare MMAudio once on CPU, and mux sound without re-encoding video frames."""
from pathlib import Path
import subprocess
import threading
_bundle = None
_prepare_lock = threading.Lock()
def prepare(cfg):
global _bundle
with _prepare_lock:
if _bundle is not None:
return _bundle
import torch
from mmaudio.model.networks import get_my_mmaudio
from mmaudio.model.utils.features_utils import FeaturesUtils
with torch.device("cpu"):
net = get_my_mmaudio(cfg.model_name)
net.load_weights(torch.load(cfg.model_path, map_location="cpu", weights_only=True))
features = FeaturesUtils(tod_vae_ckpt=cfg.vae_path, synchformer_ckpt=cfg.synchformer_ckpt,
enable_conditions=True, mode=cfg.mode,
bigvgan_vocoder_ckpt=cfg.bigvgan_16k_path, need_vae_encoder=False)
# The GPU path already used BF16. Keep the same precision, without
# retaining gradients or constructing/downloading models per clip.
net = net.eval().requires_grad_(False).to(dtype=torch.bfloat16)
features = features.eval().requires_grad_(False).to(dtype=torch.bfloat16)
_bundle = (cfg, net, features)
return _bundle
def ready_models():
if _bundle is None:
raise RuntimeError("MMAudio was not prepared at startup. Restart the Space and check its startup logs.")
return _bundle
def mux_audio(video_path, wav_path, output_path):
result = subprocess.run(
["ffmpeg", "-nostdin", "-hide_banner", "-loglevel", "error", "-y",
"-i", str(video_path), "-i", str(wav_path), "-map", "0:v:0", "-map", "1:a:0",
"-c:v", "copy", "-c:a", "aac", "-b:a", "192k", "-af", "apad", "-shortest",
"-movflags", "+faststart", str(output_path)], capture_output=True,
)
if result.returncode or not Path(output_path).is_file():
Path(output_path).unlink(missing_ok=True)
raise RuntimeError("Audio/video mux failed: " + result.stderr.decode("utf-8", "replace")[-1200:])
return str(output_path)
|