Sin2pi commited on
Commit
769d963
1 Parent(s): de1426a

Upload remove_silence_from_samples.ipynb

Browse files
Files changed (1) hide show
  1. remove_silence_from_samples.ipynb +19 -36
remove_silence_from_samples.ipynb CHANGED
@@ -6,34 +6,24 @@
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",
@@ -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
- " 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
  ],
 
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
  ],