# -*- coding: utf-8 -*- """音频窄接口:info / convert / clip。由 songforge media 经 qc_python 调用。 用法(内部): python media_worker.py '{"op":"info","file":"..."}' stdout 最后一行前有 ###JSON### """ import json import os import sys def _load(path): import soundfile as sf import numpy as np y, sr = sf.read(path, always_2d=True) y = np.asarray(y, dtype=np.float32) return y, int(sr) def op_info(job): import librosa path = job["file"] y, sr = _load(path) ch = y.shape[1] mono = y.mean(axis=1) if ch > 1 else y[:, 0] dur = float(mono.shape[0]) / float(sr) bpm = None try: tempo, _ = librosa.beat.beat_track(y=mono, sr=sr) import numpy as np t = np.asarray(tempo).reshape(-1) bpm = float(t[0]) if t.size else None except Exception: bpm = None return dict( file=os.path.abspath(path), duration_sec=round(dur, 3), sample_rate=sr, channels=ch, frames=int(mono.shape[0]), tempo_est=round(bpm, 1) if bpm else None, size_bytes=os.path.getsize(path), ) def op_convert(job): import soundfile as sf path = job["file"] out = job["out"] fmt = (job.get("to") or "wav").lower().lstrip(".") y, sr = _load(path) os.makedirs(os.path.dirname(os.path.abspath(out)) or ".", exist_ok=True) # soundfile: wav/flac 原生;mp3 需外部,失败则明确报错 if fmt == "mp3": try: import numpy as np from pydub import AudioSegment # write temp wav then export import tempfile fd, tmp = tempfile.mkstemp(suffix=".wav") os.close(fd) try: sf.write(tmp, y, sr) AudioSegment.from_wav(tmp).export(out, format="mp3") finally: try: os.remove(tmp) except OSError: pass except Exception as e: raise RuntimeError( "mp3 需要 pydub+ffmpeg;或改用 --to wav/flac。详情: %s" % e) else: subtype = "PCM_16" if fmt == "wav" else None kw = dict(format=fmt.upper()) if subtype: kw["subtype"] = subtype sf.write(out, y, sr, **kw) return dict(file=os.path.abspath(path), out=os.path.abspath(out), format=fmt, ok=True) def op_clip(job): import soundfile as sf path = job["file"] out = job["out"] start = float(job.get("start") or 0.0) dur = job.get("dur") end = job.get("end") y, sr = _load(path) i0 = max(0, int(start * sr)) if dur is not None: i1 = min(y.shape[0], i0 + int(float(dur) * sr)) elif end is not None: i1 = min(y.shape[0], int(float(end) * sr)) else: i1 = y.shape[0] if i1 <= i0: raise ValueError("截取区间无效 start=%s dur=%s end=%s" % (start, dur, end)) chunk = y[i0:i1] os.makedirs(os.path.dirname(os.path.abspath(out)) or ".", exist_ok=True) ext = os.path.splitext(out)[1].lstrip(".").lower() or "wav" if ext == "mp3": raise RuntimeError("clip 输出请用 .wav 或 .flac(mp3 请再 convert)") subtype = "PCM_16" if ext == "wav" else None kw = {} if subtype: kw["subtype"] = subtype sf.write(out, chunk, sr, **kw) return dict( file=os.path.abspath(path), out=os.path.abspath(out), start_sec=start, end_sec=i1 / float(sr), duration_sec=round((i1 - i0) / float(sr), 3), ok=True) OPS = {"info": op_info, "convert": op_convert, "clip": op_clip} def main(): raw = sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read() job = json.loads(raw) op = job.get("op") if op not in OPS: raise SystemExit("unknown op: %s" % op) if not job.get("file") or not os.path.isfile(job["file"]): raise SystemExit("file missing: %s" % job.get("file")) out = OPS[op](job) print("###JSON###") print(json.dumps(out, ensure_ascii=False)) if __name__ == "__main__": try: main() except Exception as e: print("###JSON###") print(json.dumps(dict(error=str(e), type=type(e).__name__), ensure_ascii=False)) sys.exit(1)