feat: destructive audio op command builders (delete/silence/reverse region)

This commit is contained in:
2026-07-02 17:54:11 +02:00
parent 68368b4ff6
commit 1553d7faf0
2 changed files with 79 additions and 1 deletions
+39
View File
@@ -301,6 +301,45 @@ def build_crossfade_merge_command(clips: list[str], crossfade,
return cmd + ["-filter_complex", fc, "-map", "[out]", *codec, out_path]
def build_audio_delete_command(input_path: str, start: float, end: float,
out_path: str) -> list[str]:
"""Remove [start, end] from the audio: keep [0,start] + [end,inf], concat."""
s, e = round(start, 3), round(end, 3)
ext = os.path.splitext(out_path)[1].lower()
codec = _AUDIO_CODEC_BY_EXT.get(ext, [])
fc = (f"[0]atrim=end={s},asetpts=PTS-STARTPTS[a];"
f"[0]atrim=start={e},asetpts=PTS-STARTPTS[b];"
f"[a][b]concat=n=2:v=0:a=1[out]")
return [_bin("ffmpeg"), "-y", "-i", input_path,
"-filter_complex", fc, "-map", "[out]", *codec, out_path]
def build_audio_silence_command(input_path: str, start: float, end: float,
out_path: str) -> list[str]:
"""Silence the [start, end] region (volume=0 gated by an enable expr)."""
s, e = round(start, 3), round(end, 3)
ext = os.path.splitext(out_path)[1].lower()
codec = _AUDIO_CODEC_BY_EXT.get(ext, [])
# commas inside between() must be escaped so the filtergraph parser doesn't
# treat them as filter separators.
af = f"volume=0:enable='between(t\\,{s}\\,{e})'"
return [_bin("ffmpeg"), "-y", "-i", input_path, "-af", af, *codec, out_path]
def build_audio_reverse_command(input_path: str, start: float, end: float,
out_path: str) -> list[str]:
"""Reverse only the [start, end] segment; head and tail unchanged."""
s, e = round(start, 3), round(end, 3)
ext = os.path.splitext(out_path)[1].lower()
codec = _AUDIO_CODEC_BY_EXT.get(ext, [])
fc = (f"[0]atrim=end={s},asetpts=PTS-STARTPTS[a];"
f"[0]atrim=start={s}:end={e},asetpts=PTS-STARTPTS,areverse[b];"
f"[0]atrim=start={e},asetpts=PTS-STARTPTS[c];"
f"[a][b][c]concat=n=3:v=0:a=1[out]")
return [_bin("ffmpeg"), "-y", "-i", input_path,
"-filter_complex", fc, "-map", "[out]", *codec, out_path]
def detect_hw_encoders() -> list[str]:
"""Probe ffmpeg for available H.264 hardware encoders.