import os import re import json import time import shutil import zipfile import tempfile import subprocess VERSION = 8 def sh(cmd, **kw): return subprocess.run(cmd, capture_output=True, text=True, **kw) _enc = {"txt": None} def _encoders(): if _enc["txt"] is None: try: _enc["txt"] = sh(["ffmpeg", "-hide_banner", "-encoders"], timeout=30).stdout except Exception: _enc["txt"] = "" return _enc["txt"] def has_encoder(name): return bool(re.search(r"\s" + re.escape(name) + r"\s", _encoders())) AUDIO_FORMATS = { # label: (extension, ffmpeg encoder, lossy) "MP3": ("mp3", "libmp3lame", True), "WAV (16-bit)": ("wav", "pcm_s16le", False), "WAV (24-bit)": ("wav", "pcm_s24le", False), "FLAC": ("flac", "flac", False), "AAC / M4A": ("m4a", "aac", True), "OGG Vorbis": ("ogg", "libvorbis", True), "Opus": ("opus", "libopus", True), "WebM audio": ("webm", "libopus", True), "AIFF": ("aiff", "pcm_s16be", False), "WMA": ("wma", "wmav2", True), "AMR-NB (phone)": ("amr", "libopencore_amrnb", True), "MP2": ("mp2", "mp2", True), "AC3": ("ac3", "ac3", True), "AU": ("au", "pcm_s16be", False), "MP4 (audio only)": ("mp4", "aac", True), } VIDEO_FORMATS = {"MP4 (H.264)": "mp4", "WebM (VP9)": "webm", "MOV (H.264)": "mov", "MKV": "mkv", "GIF (first 12 s)": "gif", "MP3 (audio only)": "mp3", "WAV (audio only)": "wav"} TEXT_FORMATS = ["TXT", "SRT", "VTT", "JSON"] def available_formats(): av = [k for k, (_, enc, _) in AUDIO_FORMATS.items() if has_encoder(enc)] return av or ["WAV (16-bit)"] def slug(s): return re.sub(r"[^a-z0-9]+", "-", s.lower()).strip("-") def convert_audio(src, label, out_dir, base, sr=0, ch=0, kbps=128, norm=False): ext, enc, lossy = AUDIO_FORMATS[label] out = os.path.join(out_dir, f"{base}-{slug(label)}.{ext}") cmd = ["ffmpeg", "-y", "-i", src, "-vn"] if norm: cmd += ["-af", "loudnorm=I=-16:TP=-1.5:LRA=11"] if enc == "libopus" and sr not in (8000, 12000, 16000, 24000, 48000): sr = 48000 if sr else 48000 if enc == "libopencore_amrnb": sr, ch = 8000, 1 if enc == "ac3" and sr and sr not in (32000, 44100, 48000): sr = 48000 if enc == "libmp3lame" and sr and sr not in (8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000): sr = 44100 if sr: cmd += ["-ar", str(int(sr))] if ch: cmd += ["-ac", str(int(ch))] cmd += ["-c:a", enc] if lossy and enc != "libopencore_amrnb" and kbps: cmd += ["-b:a", f"{int(kbps)}k"] if ext in ("m4a", "mp4"): cmd += ["-movflags", "+faststart"] cmd.append(out) r = sh(cmd, timeout=300) if r.returncode != 0 or not os.path.exists(out) or os.path.getsize(out) < 100: raise RuntimeError((r.stderr.strip().splitlines() or ["encoder failed"])[-1][:120]) return out def _cues(segs, timeline, total): if timeline: return [(d["start"], d["end"], d["text"]) for d in timeline] w = [len(s[0]) + 8 for s in segs] or [1] out, cur = [], 0.0 for s, x in zip(segs, w): d = total * x / sum(w) out.append((cur, cur + d, s[0])) cur += d return out def _sub(cues, per=8): out = [] for s, e, text in cues: ws = text.split() chunks = [ws[i:i + per] for i in range(0, len(ws), per)] or [[text]] tot, cur = sum(len(" ".join(c)) for c in chunks) or 1, s for c in chunks: d = (e - s) * len(" ".join(c)) / tot out.append((cur, cur + d, " ".join(c))) cur += d return out def _t(t, sep): ms = int(round(max(0, t) * 1000)) return f"{ms // 3600000:02d}:{ms // 60000 % 60:02d}:{ms // 1000 % 60:02d}{sep}{ms % 1000:03d}" def write_text_files(fmts, segs, timeline, total, out_dir, base): files = [] cues = _sub(_cues(segs, timeline, total)) content = { "TXT": "\n".join(s[0] for s in segs), "SRT": "\n".join(f"{i}\n{_t(a, ',')} --> {_t(b, ',')}\n{t}\n" for i, (a, b, t) in enumerate(cues, 1)), "VTT": "WEBVTT\n\n" + "\n".join(f"{_t(a, '.')} --> {_t(b, '.')}\n{t}\n" for a, b, t in cues), "JSON": json.dumps({"segments": [{"text": s[0], "emotion": s[1] if len(s) > 1 else "neutral", "pace": s[2] if len(s) > 2 else "normal", "pause": s[3] if len(s) > 3 else "short"} for s in segs], "timeline": timeline}, ensure_ascii=False, indent=1), } for f in fmts: if f in content: p = os.path.join(out_dir, f"{base}.{f.lower()}") with open(p, "w", encoding="utf-8") as fh: fh.write(content[f]) files.append(p) return files def export_all(src, labels, sr, ch, kbps, norm, text_fmts, segs, timeline): """Returns (files, zip_path_or_None, errors).""" import librosa base = "afc_voice_" + time.strftime("%Y%m%d_%H%M%S") work = tempfile.mkdtemp(prefix="export_") files, errs = [], [] for lab in labels or []: try: files.append(convert_audio(src, lab, work, base, sr, ch, kbps, norm)) except Exception as e: errs.append(f"{lab}: {str(e)[:60]}") if text_fmts: try: files += write_text_files(text_fmts, segs or [], timeline, float(librosa.get_duration(path=src)), work, base) except Exception as e: errs.append(f"text: {str(e)[:60]}") zp = None if len(files) > 1: zp = os.path.join(work, base + "_all-formats.zip") with zipfile.ZipFile(zp, "w", zipfile.ZIP_DEFLATED) as z: for p in files: z.write(p, os.path.basename(p)) return files, zp, errs def convert_video(src, labels): base = "afc_video_" + time.strftime("%Y%m%d_%H%M%S") work = tempfile.mkdtemp(prefix="vexport_") files, errs = [], [] for lab in labels or []: ext = VIDEO_FORMATS[lab] out = os.path.join(work, f"{base}.{ext}") try: if lab.startswith("MP4"): shutil.copy(src, out) else: cmd = ["ffmpeg", "-y", "-i", src] if lab.startswith("WebM"): cmd += ["-c:v", "libvpx-vp9", "-crf", "34", "-b:v", "0", "-row-mt", "1", "-deadline", "good", "-cpu-used", "4", "-c:a", "libopus"] elif lab.startswith("MOV"): cmd += ["-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac"] elif lab.startswith("MKV"): cmd += ["-c", "copy"] elif lab.startswith("GIF"): cmd += ["-t", "12", "-an", "-vf", "fps=12,scale=480:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse"] elif lab.startswith("MP3"): cmd += ["-vn", "-c:a", "libmp3lame", "-b:a", "192k"] else: cmd += ["-vn", "-c:a", "pcm_s16le"] r = sh(cmd + [out], timeout=600) if r.returncode != 0 or not os.path.exists(out): raise RuntimeError((r.stderr.strip().splitlines() or ["failed"])[-1][:80]) files.append(out) except Exception as e: errs.append(f"{lab}: {str(e)[:70]}") zp = None if len(files) > 1: zp = os.path.join(work, base + "_all-formats.zip") with zipfile.ZipFile(zp, "w", zipfile.ZIP_DEFLATED) as z: for p in files: z.write(p, os.path.basename(p)) return files, zp, errs def selftest(): import numpy as np import soundfile as sf work = tempfile.mkdtemp(prefix="exptest_") sr = 16000 t = np.linspace(0, 1, sr, endpoint=False) src = os.path.join(work, "t.wav") sf.write(src, (0.2 * np.sin(2 * np.pi * 220 * t)).astype(np.float32), sr) ok, bad = [], [] for lab in available_formats(): try: convert_audio(src, lab, work, "t", 0, 0, 128, False) ok.append(lab) except Exception as e: bad.append(f"{lab} ({str(e)[:40]})") return f"{len(ok)} audio formats OK" + (f"; failed: {', '.join(bad)}" if bad else "")