{ "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 }