Upload remove_silence_from_samples.ipynb
Browse files
remove_silence_from_samples.ipynb
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"from pydub import AudioSegment\n",
|
| 10 |
+
"import os, librosa, soundfile as sf\n",
|
| 11 |
+
"from pathlib import Path\n",
|
| 12 |
+
"from tqdm import tqdm\n",
|
| 13 |
+
"\n",
|
| 14 |
+
"top_db=20 #in decibels\n",
|
| 15 |
+
"threshold=-20.0 #in decibels\n",
|
| 16 |
+
"\n",
|
| 17 |
+
"def milliseconds_until_sound(sound, threshold, chunk_size=10):\n",
|
| 18 |
+
" trim_ms = 0 # ms\n",
|
| 19 |
+
" assert chunk_size > 0 # to avoid infinite loop\n",
|
| 20 |
+
" while sound[trim_ms:trim_ms+chunk_size].dBFS < threshold and trim_ms < len(sound):\n",
|
| 21 |
+
" trim_ms += chunk_size\n",
|
| 22 |
+
" return trim_ms\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"def remove_silence(input_file, output_file, top_db=top_db):#, target_sr=None):\n",
|
| 25 |
+
" sr = 16000\n",
|
| 26 |
+
" y, sr = librosa.load(input_file)\n",
|
| 27 |
+
" # if target_sr and target_sr != sr:\n",
|
| 28 |
+
" # y = librosa.resample(y, orig_sr=sr, target_sr=target_sr)\n",
|
| 29 |
+
" # sr = target_sr\n",
|
| 30 |
+
" intervals = librosa.effects.split(y, top_db=top_db)\n",
|
| 31 |
+
" y_trimmed = []\n",
|
| 32 |
+
" for start, end in intervals:\n",
|
| 33 |
+
" y_trimmed.extend(y[start:end])\n",
|
| 34 |
+
" sf.write(output_file, y_trimmed, sr)\n",
|
| 35 |
+
"\n",
|
| 36 |
+
"def process_directory(input_dir, output_dir, top_db=top_db):#, target_sr=None):\n",
|
| 37 |
+
" if not os.path.exists(output_dir):\n",
|
| 38 |
+
" os.makedirs(output_dir)\n",
|
| 39 |
+
"\n",
|
| 40 |
+
" for filename in os.listdir(input_dir):\n",
|
| 41 |
+
" if filename.endswith(\".mp3\"):\n",
|
| 42 |
+
" input_file = os.path.join(input_dir, filename)\n",
|
| 43 |
+
" output_file = os.path.join(output_dir, filename)\n",
|
| 44 |
+
" audio = AudioSegment.from_mp3(input_file)\n",
|
| 45 |
+
" # if audio.dBFS < -30:\n",
|
| 46 |
+
" start_trim = milliseconds_until_sound(audio)\n",
|
| 47 |
+
" if start_trim < len(audio):\n",
|
| 48 |
+
" remove_silence(input_file, output_file, top_db)#, target_sr)\n",
|
| 49 |
+
" # else:\n",
|
| 50 |
+
" # (print(\"file removed\"))\n",
|
| 51 |
+
"\n",
|
| 52 |
+
"if __name__ == \"__main__\":\n",
|
| 53 |
+
" input_dir = folder_path\n",
|
| 54 |
+
" output_dir = folder_path + \"_trim/\"\n",
|
| 55 |
+
" process_directory(input_dir, output_dir)#, target_sr=16000)"
|
| 56 |
+
]
|
| 57 |
+
}
|
| 58 |
+
],
|
| 59 |
+
"metadata": {
|
| 60 |
+
"language_info": {
|
| 61 |
+
"name": "python"
|
| 62 |
+
}
|
| 63 |
+
},
|
| 64 |
+
"nbformat": 4,
|
| 65 |
+
"nbformat_minor": 2
|
| 66 |
+
}
|