Skip to content

Cut Silences

The cut-silences command is an essential utility for audio editors, podcasters, and content creators who wish to refine the auditory experience of their content. It meticulously scans the provided audio file and removes all the detected silent portions, ensuring the end result is a continuous and engaging audio piece without abrupt or prolonged gaps.

Features:

  • Simple Input: The command needs just the source audio_file and the desired output_file where the processed audio will be saved.

  • Efficient Silence Removal: Leveraging advanced algorithms, this tool efficiently detects and eliminates silences, making the audio flow more natural.

  • Customizable Detection: Users have the flexibility to define what constitutes a 'silence' in their audio:

  • silence-time (or -s): Determines the minimum duration of a silence (measured in milliseconds) before it's considered for removal.
  • threshold (or -t): Sets the decibel level below which an audio segment is considered silent.

Example Usage:

To remove silences from an audio file named podcast.mp3 and save the processed audio as podcast_no_silences.mp3, use:

vmh cut-silences podcast.mp3 podcast_no_silences.mp3

--help Flag Output:

$ vmh cut-silences --help

 Usage: vmh cut-silences [OPTIONS] AUDIO_FILE OUTPUT_FILE                              

 Removes all silences from an audio file.                                              

╭─ Arguments ─────────────────────────────────────────────────────────────────────────╮
│ *    audio_file       PATH  [default: None] [required]                              │
│ *    output_file      PATH  [default: None] [required]                              │
╰─────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ───────────────────────────────────────────────────────────────────────────╮
│ --silence-time  -s      INTEGER                       Minimal time in ms for        │
│                                                       configure a silence           │
│                                                       [default: 400]                │
│ --threshold     -t      INTEGER                       Value in db for detect        │
│                                                       silence                       │
│                                                       [default: -65]                │
│ --distance      -d      [negative|tiny|small|medium|  Distance betweet silences     │
│                         large|huge]                   [default: tiny]               │
│ --help                                                Show this message and exit.   │
╰─────────────────────────────────────────────────────────────────────────────────────╯

API for developers

Source code in videomaker_helper/audio.py
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def cut_silences(
    audio_file: str,
    output_file: str,
    silence_time: int = 400,
    threshold: int = -65,
    distance: Literal[
        'negative',
        'tiny',
        'small',
        'medium',
        'large',
        'huge',
    ] = 'tiny',
) -> Path:
    logger.info(f'Reading file: {audio_file}')
    audio = AudioSegment.from_file(audio_file)
    logger.info(f'File read: {audio_file}')

    logger.info(f'Detecting silences with distance: {distance}')

    silences = detect_silences(
        audio_file,
        silence_time=silence_time,
        threshold=threshold,
        distance=distance,
    )

    logger.info('Deleting silences')

    not_silent_segments = islice(pairwise(silences), 1, None, 2)
    combined = AudioSegment.empty()

    for start, stop in not_silent_segments:
        logger.debug(f'Cutting from {start} to {stop}')
        start_ms = int(start * 1000)
        stop_ms = int(stop * 1000)
        combined += audio[start_ms:stop_ms]

    logger.info(f'Writing file: {output_file}')

    combined.export(output_file, format='mp3')

    return Path(output_file)