Skip to content

Instantly share code, notes, and snippets.

@a-sajjad72
Created December 7, 2023 05:30
Show Gist options
  • Save a-sajjad72/794ed214dccdd6c92f989789d0418358 to your computer and use it in GitHub Desktop.
Save a-sajjad72/794ed214dccdd6c92f989789d0418358 to your computer and use it in GitHub Desktop.
from pydub import AudioSegment
from pydub.silence import split_on_silence
import time
def remove_silence_add_delay(input_path, output_path, silence_thresh=-40, min_silence_len=2000, delay=200):
# Load the separated vocal track
vocal_track = AudioSegment.from_file(input_path)
# Split the vocal track on silence with the specified threshold
non_silent_vocals = split_on_silence(vocal_track, silence_thresh=silence_thresh, min_silence_len=min_silence_len)
# Add silence delay after each non-silent segment
result_segments = []
for segment in non_silent_vocals:
result_segments.append(segment)
silence = AudioSegment.silent(duration=delay)
result_segments.append(silence)
# Remove the last added silence (since there's one extra at the end)
result_segments.pop()
# Concatenate the non-silent segments with silence delay
result = sum(result_segments)
# Export the result without silence and with added delay
result.export(output_path, format="mp3")
timestamp = (
lambda seconds: f"{int(seconds // 60)}m {round(seconds % 60, 2):.2f}s"
if seconds >= 60
else f"{round(seconds, 2):.2f}s"
if seconds >= 1
else f"{round(seconds * 1000):.0f}ms"
)
# Example usage
start_time = time.time()
remove_silence_add_delay(
input_path="./Jee Le Zaraa (Talaash) [vocals].mp3",
output_path="Jee Le Zaraa (Talaash) [vocals] (silence_removed_no_delay).mp3",
silence_thresh=-40,
min_silence_len=1500,
delay=0
)
print(timestamp(time.time() - start_time))
from spleeter.separator import Separator
from spleeter.audio import Codec
# Initialize Spleeter with the desired configuration
separator = Separator('spleeter:2stems')
# Provide the path to your input audio file
input_audio_path = 'Mujhe Peene Do - Darshan Raval | Official Music Video | Romantic Song 2020 | Indie Music Label |.mp3'
# Specify the output path for the separated vocals
output_path = 'output'
# Separate vocals
separator.separate_to_file(input_audio_path, output_path, codec=Codec.MP3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment