Skip to content

Instantly share code, notes, and snippets.

@danlei
Last active February 19, 2024 20:57
Show Gist options
  • Save danlei/6b57aa8bc9f5b3d13ed34e8ad3689c9e to your computer and use it in GitHub Desktop.
Save danlei/6b57aa8bc9f5b3d13ed34e8ad3689c9e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3.10
# trim noise from forvo audio files
# experiment with treshold (in db) as needed
import sys
from pydub import AudioSegment
from pydub.silence import split_on_silence
if not 2 <= len(sys.argv) <= 3:
sys.exit("usage: forvo-trim FILE [TRESHOLD]")
input_file = sys.argv[1]
tresh = float(sys.argv[2]) if len(sys.argv) > 2 else -33
audio = AudioSegment.from_file(input_file)
chunks = split_on_silence(audio,
silence_thresh=tresh,
min_silence_len=150)
if not len(chunks):
sys.exit("no audio chunk found")
chunks[0].export('trimmed_' + input_file, format='mp3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment