Upload remove_silence_from_samples.ipynb
Browse files
remove_silence_from_samples.ipynb
CHANGED
|
@@ -6,34 +6,24 @@
|
|
| 6 |
"metadata": {},
|
| 7 |
"outputs": [],
|
| 8 |
"source": [
|
| 9 |
-
"
|
| 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
|
| 15 |
-
"
|
| 16 |
"\n",
|
| 17 |
-
"
|
| 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 |
-
"
|
| 32 |
-
"
|
| 33 |
-
"
|
| 34 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
"\n",
|
| 36 |
-
"def process_directory(input_dir, output_dir, top_db=top_db)
|
| 37 |
" if not os.path.exists(output_dir):\n",
|
| 38 |
" os.makedirs(output_dir)\n",
|
| 39 |
"\n",
|
|
@@ -41,18 +31,11 @@
|
|
| 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 |
-
"
|
| 45 |
-
"
|
| 46 |
-
"
|
| 47 |
-
"
|
| 48 |
-
"
|
| 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 |
],
|
|
|
|
| 6 |
"metadata": {},
|
| 7 |
"outputs": [],
|
| 8 |
"source": [
|
| 9 |
+
"import os, librosa\n",
|
|
|
|
|
|
|
|
|
|
| 10 |
"\n",
|
| 11 |
+
"top_db=20\n",
|
| 12 |
+
"def remove_silence(input_file, output_file, top_db=top_db):\n",
|
| 13 |
"\n",
|
| 14 |
+
" y, sr = sf.read(input_file)\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
" intervals = librosa.effects.split(y, top_db=top_db)\n",
|
| 16 |
+
" if len(intervals) == 0:\n",
|
| 17 |
+
" with open('output.txt', 'a') as f:\n",
|
| 18 |
+
" f.write(input_file, file=f)\n",
|
| 19 |
+
" else:\n",
|
| 20 |
+
" y_trimmed = []\n",
|
| 21 |
+
" for start, end in intervals:\n",
|
| 22 |
+
" y_trimmed.extend(y[start:end])\n",
|
| 23 |
+
" if not os.path.exists(output_file): \n",
|
| 24 |
+
" sf.write(output_file, y_trimmed, sr)\n",
|
| 25 |
"\n",
|
| 26 |
+
"def process_directory(input_dir, output_dir, top_db=top_db):\n",
|
| 27 |
" if not os.path.exists(output_dir):\n",
|
| 28 |
" os.makedirs(output_dir)\n",
|
| 29 |
"\n",
|
|
|
|
| 31 |
" if filename.endswith(\".mp3\"):\n",
|
| 32 |
" input_file = os.path.join(input_dir, filename)\n",
|
| 33 |
" output_file = os.path.join(output_dir, filename)\n",
|
| 34 |
+
" remove_silence(input_file, output_file, top_db)\n",
|
| 35 |
+
" \n",
|
| 36 |
+
"input_dir = folder_path\n",
|
| 37 |
+
"output_dir = folder_path\n",
|
| 38 |
+
"process_directory(folder_path, (folder_path + \"_trimmed/\"))"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
]
|
| 40 |
}
|
| 41 |
],
|