File size: 1,518 Bytes
de1426a 769d963 de1426a 769d963 de1426a 769d963 de1426a 769d963 de1426a 769d963 de1426a 769d963 de1426a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os, librosa\n",
"\n",
"top_db=20\n",
"def remove_silence(input_file, output_file, top_db=top_db):\n",
"\n",
" y, sr = sf.read(input_file)\n",
" intervals = librosa.effects.split(y, top_db=top_db)\n",
" if len(intervals) == 0:\n",
" with open('output.txt', 'a') as f:\n",
" f.write(input_file, file=f)\n",
" else:\n",
" y_trimmed = []\n",
" for start, end in intervals:\n",
" y_trimmed.extend(y[start:end])\n",
" if not os.path.exists(output_file): \n",
" sf.write(output_file, y_trimmed, sr)\n",
"\n",
"def process_directory(input_dir, output_dir, top_db=top_db):\n",
" if not os.path.exists(output_dir):\n",
" os.makedirs(output_dir)\n",
"\n",
" for filename in os.listdir(input_dir):\n",
" if filename.endswith(\".mp3\"):\n",
" input_file = os.path.join(input_dir, filename)\n",
" output_file = os.path.join(output_dir, filename)\n",
" remove_silence(input_file, output_file, top_db)\n",
" \n",
"input_dir = folder_path\n",
"output_dir = folder_path\n",
"process_directory(folder_path, (folder_path + \"_trimmed/\"))"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|