AI-KING-Studio / audio_tools.py
INTERNETCON's picture
AI KING Studio v2: real ZeroGPU generators (FLUX.1 schnell + LTX-Video), Audio Lab, stats, PWA, redesign
08e06c9 verified
Raw History Blame Contribute Delete
5.97 kB
"""AI KING©️ Audio Lab + Music Video Maker (CPU, ffmpeg). No models, no GPU."""
from __future__ import annotations
import os, shutil, subprocess, tempfile, time
OUT = os.path.join(tempfile.gettempdir(), "aiking_audio")
os.makedirs(OUT, exist_ok=True)
MAX_SECONDS = 900 # 15 min cap keeps the free CPU responsive
EFFECTS = ["πŸŒ™ Lofi warmth", "πŸ”Š Bass boost", "⚑ Sped up (nightcore)", "🐒 Slowed + reverb", "🏟️ Stadium echo", "🎧 8D rotate", "πŸ“ Loudness normalize (streaming -14 LUFS)"]
def _ff(args, timeout=900):
if not shutil.which("ffmpeg"):
raise RuntimeError("ffmpeg is not installed on this Space.")
p = subprocess.run(["ffmpeg", "-hide_banner", "-y", *args], capture_output=True, text=True, timeout=timeout)
if p.returncode != 0:
raise RuntimeError("ffmpeg: " + p.stderr[-500:])
def duration(path) -> float:
out = subprocess.run(["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=nw=1:nk=1", path],
capture_output=True, text=True).stdout.strip()
try:
return float(out)
except Exception:
return 0.0
def remix(track, effects=None, start=0.0, end=0.0, fade_in=0.0, fade_out=0.0, loop_minutes=0.0,
beat=None, beat_vol=0.6, fmt="mp3"):
if not track:
raise RuntimeError("Upload a song or sound first.")
effects = effects or []
total = duration(track)
start = max(0.0, float(start or 0))
end = float(end or 0)
if end <= 0 or end > total:
end = total
if end - start <= 0.2:
raise RuntimeError("Trim range is empty.")
seg = end - start
chain = []
if "⚑ Sped up (nightcore)" in effects:
chain += ["asetrate=44100*1.25", "aresample=44100"]
if "🐒 Slowed + reverb" in effects:
chain += ["asetrate=44100*0.85", "aresample=44100", "aecho=0.8:0.88:60|120:0.35|0.25"]
if "πŸŒ™ Lofi warmth" in effects:
chain += ["lowpass=f=3800", "highpass=f=60", "acompressor=threshold=0.2:ratio=3", "volume=1.1"]
if "πŸ”Š Bass boost" in effects:
chain += ["bass=g=9:f=90", "alimiter=limit=0.95"]
if "🏟️ Stadium echo" in effects:
chain += ["aecho=0.8:0.7:180|360:0.3|0.18"]
if "🎧 8D rotate" in effects:
chain += ["apulsator=hz=0.09"]
seg_len = seg / (1.25 if "⚑ Sped up (nightcore)" in effects else 0.85 if "🐒 Slowed + reverb" in effects else 1)
loops = 1
lm = float(loop_minutes or 0)
if lm > 0:
target = min(lm * 60, MAX_SECONDS)
loops = max(1, int(target // max(seg_len, 1)) + 1)
out_len = min(target, seg_len * loops)
else:
out_len = min(seg_len, MAX_SECONDS)
fi, fo = float(fade_in or 0), float(fade_out or 0)
post = []
if loops > 1:
post.append(f"aloop=loop={loops - 1}:size=2e9")
post.append(f"atrim=0:{out_len:.3f}")
if fi > 0:
post.append(f"afade=t=in:d={fi}")
if fo > 0:
post.append(f"afade=t=out:st={max(0, out_len - fo):.3f}:d={fo}")
if "πŸ“ Loudness normalize (streaming -14 LUFS)" in effects:
post.append("loudnorm=I=-14:TP=-1.5:LRA=11")
base = ",".join(["aformat=sample_rates=44100:channel_layouts=stereo", *chain, *post])
out = os.path.join(OUT, f"aiking-remix-{int(time.time()*1000)}.{fmt}")
codec = ["-c:a", "libmp3lame", "-b:a", "256k"] if fmt == "mp3" else ["-c:a", "pcm_s16le"]
if beat:
fc = (f"[0:a]{base}[a0];[1:a]aformat=sample_rates=44100:channel_layouts=stereo,aloop=loop=-1:size=2e9,"
f"atrim=0:{out_len:.3f},volume={float(beat_vol):.2f}[a1];[a0][a1]amix=inputs=2:duration=first:normalize=0,alimiter=limit=0.95[out]")
_ff(["-ss", f"{start}", "-t", f"{seg:.3f}", "-i", track, "-i", beat, "-filter_complex", fc, "-map", "[out]", *codec, out])
else:
_ff(["-ss", f"{start}", "-t", f"{seg:.3f}", "-i", track, "-af", base, *codec, out])
return out
VIDEO_SIZES = {"9:16 TikTok/Shorts (720x1280)": (720, 1280), "16:9 YouTube (1280x720)": (1280, 720), "1:1 Instagram (1080x1080)": (1080, 1080)}
def music_video(image, audio, size_key, motion="Slow zoom", waveform=True, max_minutes=10.0):
"""Still image (or short clip) + song -> MP4 with optional slow zoom and gold waveform."""
if not image or not audio:
raise RuntimeError("Add a cover image (or clip) and a song.")
w, h = VIDEO_SIZES.get(size_key, (720, 1280))
dur = min(duration(audio) or 30, float(max_minutes or 10) * 60, MAX_SECONDS)
fps = 24
is_video = str(image).lower().endswith((".mp4", ".mov", ".webm", ".m4v"))
out = os.path.join(OUT, f"aiking-musicvideo-{int(time.time()*1000)}.mp4")
if is_video:
vin = ["-stream_loop", "-1", "-i", image]
bg = f"[0:v]scale={w}:{h}:force_original_aspect_ratio=increase,crop={w}:{h},fps={fps},setsar=1[bg]"
else:
vin = ["-loop", "1", "-framerate", str(fps), "-i", image]
if motion == "Slow zoom":
frames = int(dur * fps) + fps
bg = (f"[0:v]scale={w*2}:{h*2}:force_original_aspect_ratio=increase,crop={w*2}:{h*2},"
f"zoompan=z='min(1+0.00025*on,1.25)':x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':d={frames}:s={w}x{h}:fps={fps},setsar=1[bg]")
else:
bg = f"[0:v]scale={w}:{h}:force_original_aspect_ratio=increase,crop={w}:{h},setsar=1[bg]"
if waveform:
wh = max(80, h // 7)
fc = (f"{bg};[1:a]aformat=channel_layouts=stereo,showwaves=s={w}x{wh}:mode=cline:rate={fps}:colors=0xF5C542|0x9FD8FF,"
f"format=rgba,colorchannelmixer=aa=0.85[wv];[bg][wv]overlay=0:{h - wh - h // 14}:format=auto,format=yuv420p[v]")
else:
fc = f"{bg};[bg]format=yuv420p[v]"
_ff([*vin, "-i", audio, "-filter_complex", fc, "-map", "[v]", "-map", "1:a", "-t", f"{dur:.2f}",
"-c:v", "libx264", "-preset", "veryfast", "-crf", "22", "-c:a", "aac", "-b:a", "192k",
"-movflags", "+faststart", "-shortest", out], timeout=1800)
return out