初始发布: 21 个 skills (Claude Code / Codex / DSH)
This commit is contained in:
Executable
+184
@@ -0,0 +1,184 @@
|
||||
#!/usr/bin/env python3
|
||||
"""MiniMax 音乐生成 CLI(Python 3 标准库,无需安装依赖)。
|
||||
|
||||
用法示例:
|
||||
# 带唱
|
||||
python3 generate.py "独立民谣,忧郁内省,木吉他+弦乐" -l @lyrics.txt
|
||||
# 纯音乐
|
||||
python3 generate.py "中世纪奇幻大地图,鲁特琴+竖琴,无鼓" --instrumental
|
||||
# 自动作词
|
||||
python3 generate.py "抒情流行,夏夜告别" --auto-lyrics
|
||||
# 翻唱
|
||||
python3 generate.py "" --cover ref.mp3 -l "[Verse]\n新歌词..."
|
||||
"""
|
||||
import argparse, base64, json, os, sys, time, urllib.request, urllib.error
|
||||
|
||||
API_URL = "https://api.minimaxi.com/v1/music_generation"
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
PAID = {"music-3.0", "music-2.6", "music-cover"}
|
||||
FREE = {"music-3.0-free", "music-2.6-free", "music-cover-free"}
|
||||
MODELS = sorted(PAID | FREE)
|
||||
|
||||
# base_resp.status_code -> 人话
|
||||
ERRORS = {
|
||||
1002: "触发限流(付费模型 RPM 120,free 模型 RPM 3)。等一会儿再试,别并发。",
|
||||
1004: "鉴权失败:API Key 无效。检查 MINIMAX_API_KEY 或 key.txt。",
|
||||
1008: "账户余额不足,去控制台充值。",
|
||||
1026: "命中敏感内容审核,改一下 prompt 或歌词。",
|
||||
2013: "参数不合法(看 status_msg 里的具体字段)。",
|
||||
2049: "API Key 格式不对。",
|
||||
}
|
||||
|
||||
|
||||
def load_key():
|
||||
k = os.environ.get("MINIMAX_API_KEY", "").strip()
|
||||
if k:
|
||||
return k
|
||||
config_path = os.path.expanduser("~/.config/minimax_api_key")
|
||||
if os.path.exists(config_path):
|
||||
k = open(config_path).read().strip()
|
||||
if k:
|
||||
return k
|
||||
p = os.path.join(HERE, "key.txt")
|
||||
if os.path.exists(p):
|
||||
k = open(p).read().strip()
|
||||
if k:
|
||||
return k
|
||||
sys.exit("没有 API Key。设置环境变量 MINIMAX_API_KEY,或写入 %s" % p)
|
||||
|
||||
|
||||
def read_maybe_file(v):
|
||||
"""支持 @path 从文件读取。"""
|
||||
if v and v.startswith("@"):
|
||||
return open(os.path.expanduser(v[1:]), encoding="utf-8").read()
|
||||
return v
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description="MiniMax 音乐生成")
|
||||
ap.add_argument("prompt", nargs="?", default="",
|
||||
help="曲风/情绪/场景描述,<=2000 字。纯音乐时必填")
|
||||
ap.add_argument("-l", "--lyrics", default="",
|
||||
help="歌词,<=3500 字,用 \\n 分行;支持 @文件路径。带唱时必填")
|
||||
ap.add_argument("--instrumental", action="store_true", help="生成纯音乐(无人声)")
|
||||
ap.add_argument("--auto-lyrics", action="store_true",
|
||||
help="让模型按 prompt 自动作词(lyrics_optimizer)")
|
||||
ap.add_argument("--cover", default="",
|
||||
help="翻唱参考音频:本地文件路径 或 http(s) URL(6s-6min,<=50MB)")
|
||||
ap.add_argument("--cover-feature-id", default="",
|
||||
help="翻唱预处理接口拿到的 feature_id(24 小时有效)")
|
||||
ap.add_argument("-m", "--model", default="music-3.0", choices=MODELS)
|
||||
ap.add_argument("--free", action="store_true",
|
||||
help="改用对应的 -free 免费模型(RPM 3,不计费)")
|
||||
ap.add_argument("-o", "--output", default="", help="输出路径,默认按时间戳命名")
|
||||
ap.add_argument("--sample-rate", type=int, default=44100,
|
||||
choices=[16000, 24000, 32000, 44100])
|
||||
ap.add_argument("--bitrate", type=int, default=256000,
|
||||
choices=[32000, 64000, 128000, 256000])
|
||||
ap.add_argument("--format", default="mp3", choices=["mp3", "wav", "pcm"])
|
||||
ap.add_argument("--watermark", action="store_true", help="加 AIGC 水印")
|
||||
ap.add_argument("--hex", action="store_true",
|
||||
help="用 hex 返回而非 url(url 链接 24 小时过期)")
|
||||
ap.add_argument("--json", action="store_true", help="打印完整 JSON 返回")
|
||||
a = ap.parse_args()
|
||||
|
||||
model = a.model
|
||||
if a.free and not model.endswith("-free"):
|
||||
model += "-free"
|
||||
|
||||
lyrics = read_maybe_file(a.lyrics)
|
||||
is_cover = bool(a.cover or a.cover_feature_id)
|
||||
if is_cover and not model.startswith("music-cover"):
|
||||
model = "music-cover-free" if model.endswith("-free") else "music-cover"
|
||||
|
||||
# ---- 本地前置校验:省得白花钱 ----
|
||||
if a.cover and a.cover_feature_id:
|
||||
sys.exit("--cover 和 --cover-feature-id 互斥,只能给一个")
|
||||
if a.instrumental and not a.prompt.strip():
|
||||
sys.exit("纯音乐模式下 prompt 必填(要靠它定曲风和配器)")
|
||||
if not a.instrumental and not is_cover and not a.auto_lyrics and not lyrics.strip():
|
||||
sys.exit("带唱模式下 lyrics 必填。要么给 -l,要么加 --instrumental,"
|
||||
"要么加 --auto-lyrics 让模型自己写")
|
||||
if a.cover_feature_id and not (10 <= len(lyrics.strip()) <= 1000):
|
||||
sys.exit("带 feature_id 的翻唱要求歌词 10-1000 字,当前 %d 字" % len(lyrics.strip()))
|
||||
if len(a.prompt) > 2000:
|
||||
sys.exit("prompt 超长:%d > 2000 字" % len(a.prompt))
|
||||
if len(lyrics) > 3500:
|
||||
sys.exit("lyrics 超长:%d > 3500 字" % len(lyrics))
|
||||
|
||||
body = {
|
||||
"model": model,
|
||||
"output_format": "hex" if a.hex else "url",
|
||||
"audio_setting": {"sample_rate": a.sample_rate, "bitrate": a.bitrate,
|
||||
"format": a.format},
|
||||
}
|
||||
if a.prompt.strip():
|
||||
body["prompt"] = a.prompt
|
||||
if lyrics.strip():
|
||||
body["lyrics"] = lyrics
|
||||
if a.instrumental:
|
||||
body["is_instrumental"] = True
|
||||
if a.auto_lyrics:
|
||||
body["lyrics_optimizer"] = True
|
||||
if a.watermark:
|
||||
body["aigc_watermark"] = True
|
||||
if a.cover:
|
||||
if a.cover.startswith("http"):
|
||||
body["audio_url"] = a.cover
|
||||
else:
|
||||
with open(os.path.expanduser(a.cover), "rb") as f:
|
||||
body["audio_base64"] = base64.b64encode(f.read()).decode()
|
||||
if a.cover_feature_id:
|
||||
body["cover_feature_id"] = a.cover_feature_id
|
||||
|
||||
cost = "免费" if model.endswith("-free") else "¥1.0"
|
||||
print("模型 %s(%s)· 提交中…" % (model, cost), flush=True)
|
||||
|
||||
req = urllib.request.Request(
|
||||
API_URL, data=json.dumps(body, ensure_ascii=False).encode("utf-8"),
|
||||
headers={"Authorization": "Bearer " + load_key(),
|
||||
"Content-Type": "application/json"}, method="POST")
|
||||
t = time.time()
|
||||
try:
|
||||
r = json.loads(urllib.request.urlopen(req, timeout=600).read().decode("utf-8"))
|
||||
except urllib.error.HTTPError as e:
|
||||
sys.exit("HTTP %d %s" % (e.code, e.read().decode("utf-8", "replace")))
|
||||
except urllib.error.URLError as e:
|
||||
sys.exit("网络错误:%s" % e)
|
||||
|
||||
if a.json:
|
||||
print(json.dumps(r, ensure_ascii=False, indent=2))
|
||||
|
||||
# HTTP 200 不代表成功,一律看 base_resp.status_code
|
||||
base = r.get("base_resp") or {}
|
||||
code = base.get("status_code")
|
||||
if code != 0:
|
||||
sys.exit("生成失败 %s: %s\n%s" % (code, base.get("status_msg"),
|
||||
ERRORS.get(code, "")))
|
||||
|
||||
info = r.get("extra_info") or {}
|
||||
dur = info.get("music_duration", 0) / 1000.0
|
||||
print("成功 · 时长 %.1fs · %s Hz · %s 声道 · %s bps · 耗时 %.0fs"
|
||||
% (dur, info.get("music_sample_rate"), info.get("music_channel"),
|
||||
info.get("bitrate"), time.time() - t), flush=True)
|
||||
|
||||
audio = (r.get("data") or {}).get("audio")
|
||||
if not audio:
|
||||
sys.exit("返回里没有音频数据")
|
||||
|
||||
out = a.output or "minimax-music-%s.%s" % (time.strftime("%Y%m%d-%H%M%S"), a.format)
|
||||
out = os.path.expanduser(out)
|
||||
d = os.path.dirname(os.path.abspath(out))
|
||||
if d:
|
||||
os.makedirs(d, exist_ok=True)
|
||||
if audio.startswith("http"):
|
||||
urllib.request.urlretrieve(audio, out)
|
||||
else:
|
||||
with open(out, "wb") as f:
|
||||
f.write(bytes.fromhex(audio))
|
||||
print("已保存: %s (%.1f MB)" % (out, os.path.getsize(out) / 1e6))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+220
@@ -0,0 +1,220 @@
|
||||
#!/usr/bin/env python3
|
||||
"""把生成的音乐裁成指定长度的无缝循环 BGM,并做客观验收。
|
||||
|
||||
MiniMax 接口既没有 duration 参数、也没有循环淡化,游戏/短片要定长循环 BGM
|
||||
只能后期做。本脚本负责:
|
||||
1. 测素材实际速度,把循环长度对齐到整数小节(只对齐电平不对齐节奏,
|
||||
循环起来会丢拍)
|
||||
2. 扫描起点,选首尾 2 秒在 RMS/频谱质心/低频占比上最接近的窗口,
|
||||
并避开渐入、渐出和能量凹陷
|
||||
3. 用 qsin 等功率曲线做尾→头交叉淡化
|
||||
4. 验收:峰值、静音、接缝跳变、立体声宽度
|
||||
|
||||
依赖:ffmpeg/ffprobe + numpy
|
||||
用法:
|
||||
python3 loopify.py raw.mp3 -o bgm_loop.mp3 -L 45 --preview 3
|
||||
"""
|
||||
import argparse, os, shutil, subprocess, sys
|
||||
|
||||
try:
|
||||
import numpy as np
|
||||
except ImportError:
|
||||
sys.exit("需要 numpy:pip3 install numpy")
|
||||
|
||||
if not shutil.which("ffmpeg"):
|
||||
sys.exit("需要 ffmpeg:brew install ffmpeg")
|
||||
|
||||
ANALYZE_SR = 22050
|
||||
HOP = 512
|
||||
|
||||
|
||||
def decode(path, sr, ch=1):
|
||||
r = subprocess.run(["ffmpeg", "-v", "error", "-i", path, "-ac", str(ch),
|
||||
"-ar", str(sr), "-f", "f32le", "-"],
|
||||
capture_output=True)
|
||||
if r.returncode != 0:
|
||||
sys.exit("解码失败:%s" % r.stderr.decode("utf-8", "replace")[:400])
|
||||
a = np.frombuffer(r.stdout, dtype=np.float32)
|
||||
return a.reshape(-1, ch) if ch > 1 else a
|
||||
|
||||
|
||||
def beat_period(x):
|
||||
"""谱通量 + 自相关,估计节拍周期(秒)。"""
|
||||
win = 1024
|
||||
n = (len(x) - win) // HOP
|
||||
if n < 64:
|
||||
return None
|
||||
idx = np.arange(n)[:, None] * HOP + np.arange(win)
|
||||
S = np.abs(np.fft.rfft(x[idx] * np.hanning(win), axis=1))
|
||||
flux = np.maximum(0, np.diff(S, axis=0)).sum(axis=1)
|
||||
flux = flux - flux.mean()
|
||||
fps = ANALYZE_SR / HOP
|
||||
ac = np.correlate(flux, flux, "full")[len(flux) - 1:]
|
||||
lo, hi = int(fps * 60 / 160), int(fps * 60 / 60)
|
||||
if hi >= len(ac):
|
||||
return None
|
||||
return (lo + int(np.argmax(ac[lo:hi]))) / fps
|
||||
|
||||
|
||||
def pick_window(x, dur, target, tol, fade):
|
||||
"""返回 (t0, L, score, 说明)。"""
|
||||
beat = beat_period(x)
|
||||
cands = []
|
||||
if beat:
|
||||
for bpb in (3, 4, 6, 8):
|
||||
bar = beat * bpb
|
||||
k = 1
|
||||
while bar * k <= target + tol:
|
||||
L = bar * k
|
||||
if target - tol <= L <= target + tol:
|
||||
cands.append((L, "%d 小节 × %d 拍 @ %.1f BPM"
|
||||
% (k, bpb, 60 / beat)))
|
||||
k += 1
|
||||
if not cands:
|
||||
cands = [(float(target), "未测出稳定节拍,用目标长度")]
|
||||
|
||||
def feat(t):
|
||||
a = x[int(t * ANALYZE_SR):int((t + fade) * ANALYZE_SR)]
|
||||
if len(a) < ANALYZE_SR // 2:
|
||||
return None
|
||||
sp = np.abs(np.fft.rfft(a * np.hanning(len(a))))
|
||||
fr = np.fft.rfftfreq(len(a), 1 / ANALYZE_SR)
|
||||
e = sp.sum() + 1e-9
|
||||
return np.array([20 * np.log10(np.sqrt((a ** 2).mean()) + 1e-9),
|
||||
(sp * fr).sum() / e / 1000.0,
|
||||
sp[fr < 300].sum() / e * 20])
|
||||
|
||||
best = None
|
||||
for L, why in cands:
|
||||
if L + fade + 1.5 > dur:
|
||||
continue
|
||||
t0 = 1.0
|
||||
while t0 + L + fade <= dur - 0.5:
|
||||
h, t = feat(t0), feat(t0 + L)
|
||||
if h is not None and t is not None:
|
||||
seg = x[int(t0 * ANALYZE_SR):int((t0 + L) * ANALYZE_SR)]
|
||||
k = int(ANALYZE_SR * 0.5)
|
||||
quietest = min(np.sqrt((seg[i:i + k] ** 2).mean())
|
||||
for i in range(0, max(1, len(seg) - k), k))
|
||||
penalty = max(0.0, -20 * np.log10(quietest + 1e-9) - 40) * 0.5
|
||||
d = float(np.abs(h - t).sum()) + penalty
|
||||
if best is None or d < best[2]:
|
||||
best = (t0, L, d, why)
|
||||
t0 += 0.05
|
||||
if best is None:
|
||||
sys.exit("素材太短,做不出 %.1fs 的循环(需要至少 %.1fs)"
|
||||
% (target, target + fade + 2.5))
|
||||
return best
|
||||
|
||||
|
||||
def build(src, out, t0, L, fade, bitrate, peak_dbfs):
|
||||
wav = os.path.splitext(out)[0] + ".wav"
|
||||
fc = ("[0:a]atrim=start=%.4f:duration=%.4f,asetpts=PTS-STARTPTS[tail];"
|
||||
"[1:a]atrim=start=%.4f:duration=%.4f,asetpts=PTS-STARTPTS[body];"
|
||||
"[tail][body]acrossfade=d=%.2f:c1=qsin:c2=qsin[out]"
|
||||
% (t0 + L, fade, t0, L, fade))
|
||||
subprocess.run(["ffmpeg", "-hide_banner", "-v", "error", "-y",
|
||||
"-i", src, "-i", src, "-filter_complex", fc,
|
||||
"-map", "[out]", "-c:a", "pcm_s24le", wav], check=True)
|
||||
|
||||
# MiniMax 的输出电平不稳定(实测有 -2.3 dBFS 的,也有 0.0 dBFS 顶格的)。
|
||||
# 顶格素材经交叉淡化两路叠加必然溢出,所以这里统一压到目标峰值。
|
||||
target = 10 ** (peak_dbfs / 20.0)
|
||||
p = float(np.abs(decode(wav, 44100, 2)).max())
|
||||
if p > target:
|
||||
g = target / max(p, 1e-9)
|
||||
tmp = wav + ".tmp.wav"
|
||||
subprocess.run(["ffmpeg", "-hide_banner", "-v", "error", "-y", "-i", wav,
|
||||
"-af", "volume=%.6f" % g, "-c:a", "pcm_s24le", tmp],
|
||||
check=True)
|
||||
os.replace(tmp, wav)
|
||||
print("留余量: 峰值 %.1f → %.1f dBFS(衰减 %.1f dB)"
|
||||
% (20 * np.log10(p + 1e-9), peak_dbfs, 20 * np.log10(g)))
|
||||
|
||||
subprocess.run(["ffmpeg", "-hide_banner", "-v", "error", "-y", "-i", wav,
|
||||
"-c:a", "libmp3lame", "-b:a", bitrate, out], check=True)
|
||||
return wav
|
||||
|
||||
|
||||
def verify(wav, sr=44100):
|
||||
x = decode(wav, sr, 2)
|
||||
ok = True
|
||||
print("\n=== 验收 ===")
|
||||
print("时长 %.3fs · %d 声道 · %d Hz" % (len(x) / sr, x.shape[1], sr))
|
||||
|
||||
peak = float(np.abs(x).max())
|
||||
good = peak < 0.999
|
||||
ok &= good
|
||||
print("峰值 %.4f (%.1f dBFS) %s" % (peak, 20 * np.log10(peak + 1e-9),
|
||||
"OK" if good else "削波!"))
|
||||
|
||||
def rms_db(a):
|
||||
return 20 * np.log10(np.sqrt((a ** 2).mean()) + 1e-9)
|
||||
|
||||
k = sr // 2
|
||||
q = min(rms_db(x[i:i + k]) for i in range(0, len(x) - k, k // 2))
|
||||
good = q > -45
|
||||
ok &= good
|
||||
print("最静 0.5s %.1f dB %s" % (q, "OK" if good else "有静音段!"))
|
||||
|
||||
mono = x.mean(axis=1)
|
||||
typ = float(np.percentile(np.abs(np.diff(mono)), 99.9))
|
||||
seam = float(abs(mono[0] - mono[-1]))
|
||||
good = seam <= typ
|
||||
ok &= good
|
||||
print("接缝跳变 %.6f vs 曲内 99.9 分位 %.6f(比值 %.2f)%s"
|
||||
% (seam, typ, seam / (typ + 1e-12), "OK" if good else "有咔哒声!"))
|
||||
|
||||
d = abs(rms_db(mono[-k:]) - rms_db(mono[:k]))
|
||||
good = d < 3
|
||||
ok &= good
|
||||
print("接缝前后 RMS 差 %.1f dB %s" % (d, "OK" if good else "电平不匹配"))
|
||||
|
||||
w = float(np.abs(x[:, 0] - x[:, 1]).mean() / (np.abs(x).mean() + 1e-9))
|
||||
print("立体声宽度 %.3f %s" % (w, "有空间感" if w > 0.1 else "接近单声道"))
|
||||
print("=== %s ===" % ("全项通过" if ok else "有项未通过,见上"))
|
||||
return ok
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description="裁成无缝循环 BGM")
|
||||
ap.add_argument("input")
|
||||
ap.add_argument("-o", "--output", default="bgm_loop.mp3")
|
||||
ap.add_argument("-L", "--length", type=float, default=45.0, help="目标秒数")
|
||||
ap.add_argument("--tol", type=float, default=1.0, help="长度容差秒")
|
||||
ap.add_argument("--fade", type=float, default=2.0, help="交叉淡化秒")
|
||||
ap.add_argument("--bitrate", default="256k")
|
||||
ap.add_argument("--peak", type=float, default=-1.0,
|
||||
help="目标峰值 dBFS,超了自动衰减留余量")
|
||||
ap.add_argument("--preview", type=int, default=0,
|
||||
help="额外导出连播 N 遍的试听文件,用来听接缝")
|
||||
ap.add_argument("--keep-wav", action="store_true", help="保留无损 wav")
|
||||
a = ap.parse_args()
|
||||
|
||||
src = os.path.expanduser(a.input)
|
||||
out = os.path.expanduser(a.output)
|
||||
x = decode(src, ANALYZE_SR)
|
||||
dur = len(x) / ANALYZE_SR
|
||||
print("素材 %s · %.2fs" % (os.path.basename(src), dur))
|
||||
|
||||
t0, L, score, why = pick_window(x, dur, a.length, a.tol, a.fade)
|
||||
print("循环长度 %.3fs(%s)· 起点 %.2fs · 接缝差异分 %.3f" % (L, why, t0, score))
|
||||
|
||||
wav = build(src, out, t0, L, a.fade, a.bitrate, a.peak)
|
||||
verify(wav)
|
||||
|
||||
if a.preview > 1:
|
||||
pv = os.path.splitext(out)[0] + "_x%d.mp3" % a.preview
|
||||
subprocess.run(["ffmpeg", "-hide_banner", "-v", "error", "-y",
|
||||
"-stream_loop", str(a.preview - 1), "-i", wav,
|
||||
"-c:a", "libmp3lame", "-b:a", a.bitrate, pv], check=True)
|
||||
print("接缝试听(连播 %d 遍): %s" % (a.preview, pv))
|
||||
if not a.keep_wav:
|
||||
os.remove(wav)
|
||||
else:
|
||||
print("无损: %s" % wav)
|
||||
print("成品: %s" % out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user