from faster_whisper import WhisperModel
from pydub import AudioSegment
from pydub.generators import Sine
TARGETS = {"damn", "hell"}
PAD_MS = 60 # timestamps are slightly loose; pad both sides
model = WhisperModel("small", compute_type="int8")
segments, _ = model.transcribe("input.mp3", word_timestamps=True)
audio = AudioSegment.from_file("input.mp3")
spans = [
(int(w.start * 1000) - PAD_MS, int(w.end * 1000) + PAD_MS)
for seg in segments for w in seg.words
if w.word.strip().lower().strip(".,!?'\"") in TARGETS
]
for start, end in spans:
start, end = max(0, start), min(len(audio), end)
patch = Sine(1000).to_audio_segment(duration=end - start).apply_gain(-12)
# or: patch = AudioSegment.silent(duration=end - start)
audio = audio[:start] + patch + audio[end:]
audio.export("output.mp3", format="mp3")