"""将采样版音频作者锚点投影到48k样本;不读取文件或估算真实长度。""" from app.schemas.editor_audio import AudioSegmentV2, AudioLayerV2 from app.domain.continuity import compile_story_timing, supports_sample_timing SAMPLES_PER_FRAME = 2000 def _timeline(manifest): shots, scenes, cursor = {}, {}, 0 projection = None sample_timing = supports_sample_timing(manifest) if sample_timing and supports_sample_timing(manifest["spec"]): projection = { row["shot_id"]: row for row in compile_story_timing(manifest["spec"])["shots"] } for sid, shot in zip(manifest["shot_ids"], manifest["spec"]["shots"], strict=True): edit = shot.get("edit", {}) count = edit.get("duration_frames", shot.get("duration_frames", 120)) q = edit.get("dedup_frames", 0) if sample_timing else 0 if type(count) is not int or type(q) is not int or q not in (0, 1) or count - q < 1: raise ValueError("镜头贡献帧无效") if projection is not None: row = projection[sid] start, end = ( row["start_frame"] * SAMPLES_PER_FRAME, row["end_frame"] * SAMPLES_PER_FRAME, ) else: start, end = cursor, cursor + (count - q) * SAMPLES_PER_FRAME shots[sid] = (start, end) scene = shot.get("scene_id") if scene: if scene in scenes and (scenes[scene] is None or scenes[scene][1] != start): # 同一场景跨越其它场景时须显式使用全片锚点,避免隐式跨过不相关内容。 scenes[scene] = None elif scenes.get(scene, (start, end)) is not None: scenes[scene] = (scenes.get(scene, (start, end))[0], end) cursor = end return shots, scenes, cursor def _position(segment, shots, scenes, total): anchor = segment.get("anchor", "shot") if anchor == "shot": bounds = shots.get(segment.get("shot_id")) elif anchor == "scene": bounds = scenes.get(segment.get("scene_id")) elif anchor == "absolute": bounds = (0, total) else: bounds = None if bounds is None: raise ValueError("音频对应镜头或连续场景不存在,请重新绑定锚点") return bounds def _measured_count(binding): count = binding.get("sample_count") rate = binding.get("sample_rate", 48000) if type(count) is not int or count <= 0 or rate != 48000: raise ValueError("音频需先归一化并测量实际48k样本数") return count def compile_audio_timeline(manifest, bindings, *, source_samples=None, include_layers=True): shots, scenes, total = _timeline(manifest) audio = manifest.get("audio", {}) source_samples = source_samples or {} segments = [] cursors = {} def add(raw, aid, count, role, sid): start, end = _position(raw, shots, scenes, total) a = raw.get("source_start_sample", 0) b = raw.get("source_end_sample") b = count if b is None else b offset = start + raw.get("offset_sample", 0) key = (raw.get("anchor", "shot"), raw.get("shot_id"), raw.get("scene_id")) if role == "narration" and not raw.get("offset_sample", 0): offset = max(offset, cursors.get(key, offset)) if a < 0 or b <= a or b > count or offset + b - a > end: raise ValueError("音频选用区间或播放位置超出源音频、锚点范围") if raw.get("fade_in_samples", 0) + raw.get("fade_out_samples", 0) > b - a: raise ValueError("音频淡入淡出超出选用区间") segments.append( { "asset_id": aid, "source_start_sample": a, "source_end_sample": b, "offset_sample": offset, "gain_db": raw.get("gain_db", 0.0), "fade_in_samples": raw.get("fade_in_samples", 0), "fade_out_samples": raw.get("fade_out_samples", 0), "role": role, "segment_id": sid, } ) if role == "narration": cursors[key] = offset + b - a if audio.get("mode") == "generated_narration": for raw in audio.get("segments", []): raw = AudioSegmentV2.model_validate(raw).model_dump() binding = bindings.get("audio:" + raw["id"], {}) if not binding.get("asset_id"): raise ValueError("旁白音频尚未生成或导入") add(raw, binding["asset_id"], _measured_count(binding), "narration", raw["id"]) elif audio.get("mode") == "manual_track": track = audio.get("track") if not track: raise ValueError("请选择人工音轨") if "source_end_sample" not in track: raise ValueError("旧毫秒音轨需先归一化为样本选择") count = source_samples.get(track["asset_id"]) if type(count) is not int or count < 1: raise ValueError("人工音轨需先归一化并测量实际样本数") add({**track, "anchor": "absolute"}, track["asset_id"], count, "narration", "track") for raw in audio.get("layers", []) if include_layers else []: raw = AudioLayerV2.model_validate(raw).model_dump() count = source_samples.get(raw["asset_id"]) if type(count) is not int or count < 1: raise ValueError("音频图层需先归一化并测量实际样本数") add(raw, raw["asset_id"], count, raw["role"], raw["id"]) return { "segments": segments, "total_frames": total // SAMPLES_PER_FRAME, "total_samples": total, } def audio_sample_ranges(manifest, bindings): return { segment["segment_id"]: ( segment["offset_sample"], segment["offset_sample"] + segment["source_end_sample"] - segment["source_start_sample"], ) for segment in compile_audio_timeline(manifest, bindings, include_layers=False)["segments"] if segment["role"] == "narration" }