#!/usr/bin/env python from pydub import AudioSegment def fade_point(audio_segment, fade_threshold=-12, chunk_size=10): """ Returns the millisecond/index of the point where the fade is down to fade_threshold and doesn't get louder again. audio_segment - the sdlg_search_database_uiegment to find silence in fade_threshold - the upper bound for how quiet is silent in dFBS chunk_size - chunk size for interating over the segment in ms """ assert chunk_size > 0 # to avoid infinite loop segment_length = audio_segment.duration_seconds * 1000 # ms print(f"segment_length={int(segment_length/1000)}") trim_ms = segment_length - chunk_size max_vol = audio_segment.dBFS print(f"{max_vol=}") fade_threshold = max_vol while ( audio_segment[trim_ms : trim_ms + chunk_size].dBFS < fade_threshold and trim_ms > 0 ): # noqa W503 trim_ms -= chunk_size # if there is no trailing silence, return lenght of track (it's less # the chunk_size, but for chunk_size = 10ms, this may be ignored) print(f"Fade last {int(segment_length - trim_ms)/1000} seconds") # print("Shout:") # segment = AudioSegment.from_mp3("../archive/shout.mp3") # fade_point(segment) # print("Champagne:") # segment = AudioSegment.from_mp3("../archive/champ.mp3") # fade_point(segment) # print("Be good:") # segment = AudioSegment.from_mp3("../archive/wibg.mp3") # fade_point(segment) print("Be good:") segment = AudioSegment.from_file("/tmp/bia.flac", "flac") fade_point(segment)