Wie man in Python Stille aus einer Aufnahme entfernt, ohne Wörter abzuschneiden
Pausen aus einer Sprachaufnahme zu entfernen, läuft auf vier Zahlen hinaus: einen Pegel, unter dem Audio als still gilt, wie lange ein leiser Abschnitt mindestens dauern muss, bevor er entfernt wird, und wie viel auf beiden Seiten jedes verbleibenden Stücks stehen bleibt. Der Sound Editor in der Desktop-App von osFoundry zeigt genau diese vier im Dialog Strip Silence, mit den Standardwerten −48…
Removing silence from a recording in Python without cutting words involves four key numbers: a level below which audio is considered silent, how long a quiet section must be to be removed, and how much remains on each side of remaining pieces. The Sound Editor in the osFoundry desktop app displays these four values in the Strip Silence dialog, with default values of −48 dB, 50 ms, 5 ms, and 20 ms. The same operation is reproduced in about 40 lines of numpy code, sampled against a file exported by the app and used to measure the effect of each number.
The final task is accomplished with silenceremove from ffmpeg, which behaves differently at a hard-to-notice spot. The test recordings consisted of five short text-to-speech sentences in English and Japanese, separated by 2.4 to 3.4 seconds of silence, along with generated room noise. Both audio files were 48 kHz, mono, 16-bit, with a length of 27.57 seconds for English and 33.55 seconds for Japanese.
The gap between the last two columns becomes important later. Noise with an average of −64.6 dBFS still has individual samples that reach −52.4 dBFS. A rule that a sample is quiet if its amount is at or below the threshold applies. A sequence of quiet samples can be removed if it lasts at least the minimum length. All between removable sequences becomes a clip, expanded by padding at the beginning and end.
The code imports numpy as np and defines two functions: silent_runs and strip_silence. The silent_runs function calculates quiet sections based on the threshold and minimum length, while strip_silence applies padding and returns clips. The space_clips function closes the gaps between clips by concatenating zeros and returning the result.
Both takes were run through the Sound Editor with the given parameters, then Space Clips, and finally Bounce Mix, normalized to −0.1 dBFS. The app's file is 24-bit stereo with identical channels. Compared to the numpy result, normalized equally, the largest discrepancy is 660,500 samples (13.76 seconds) for English and 815,030 samples (16.98 seconds) for Japanese, which is within the range of 24-bit rounding errors.
A simple per-sample comparison, without RMS windows or smoothing, is enough to reproduce what the app exports. The first version of the recreation was off by exactly one place, which is the only spot where a naive implementation fails. Each take begins with 0.47 seconds of silence before the first word, shorter than the minimum length of 500 ms. Literally applied, the rule keeps this section, and the output begins with 0.376 seconds of digital zero, followed by the first word: silence at the beginning.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.