Spaces:
Running
Running
File size: 3,459 Bytes
15ed22f 3c3a980 15ed22f |
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import gradio as gr
from pydub import AudioSegment, silence
import zipfile
import os
def remove_silence(audio):
# Remove silence
non_silent_audio = silence.split_on_silence(audio, min_silence_len=1000, silence_thresh=-40)
return non_silent_audio
def join_audio_segments(segments, segment_duration):
joined_segments = []
current_segment = None
for segment in segments:
if current_segment is None:
current_segment = segment
elif current_segment.duration_seconds < segment_duration:
current_segment += segment
else:
joined_segments.append(current_segment)
current_segment = segment
if current_segment is not None:
if current_segment.duration_seconds < segment_duration:
if joined_segments:
joined_segments[-1] += current_segment
else:
joined_segments.append(current_segment)
else:
joined_segments.append(current_segment)
return joined_segments
def process_audio(files, segment_duration):
zip_file_name = "audio_segments.zip"
with zipfile.ZipFile(zip_file_name, 'w') as zip_file:
for i, audio_file in enumerate(files):
audio = AudioSegment.from_file(audio_file.name)
# Check if the file is in mp3 format
is_mp3 = False
if audio_file.name.lower().endswith('.mp3'):
is_mp3 = True
# Remove silence
non_silent_audio = remove_silence(audio)
segments = []
for j, segment in enumerate(non_silent_audio):
if segment.duration_seconds >= segment_duration:
segments.extend(segment[0:segment_duration * 1000] for segment in segment[::segment_duration * 1000])
else:
segments.append(segment)
# Join segments less than the specified duration with the nearest audio segment
joined_segments = join_audio_segments(segments, segment_duration)
for k, segment in enumerate(joined_segments):
segment_file_name = f"segment_{i + 1}_{k + 1}.wav" if not is_mp3 else f"segment_{i + 1}_{k + 1}.mp3"
segment.export(segment_file_name, format="wav" if not is_mp3 else "mp3")
zip_file.write(segment_file_name)
os.remove(segment_file_name)
return zip_file_name
def split_audio(files, segment_duration):
try:
segment_duration = int(segment_duration)
except ValueError:
return "Invalid segment duration. Please enter an integer."
if len(files) == 0:
return "Please upload audio files."
zip_path = process_audio(files, segment_duration)
return zip_path
# Gradio Interface
with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary_hue=gr.themes.colors.blue, spacing_size=gr.themes.sizes.spacing_sm, radius_size=gr.themes.sizes.radius_none)) as app:
gr.Markdown("## Audio Splitter")
with gr.Row():
audio_files = gr.File(label="Upload Audio Files", file_count="multiple", file_types=["audio"])
segment_duration = gr.Number(label="Segment Duration (seconds)", value=5)
split_button = gr.Button("Split Audio Files")
output = gr.File(label="Download Split Audio Segments")
split_button.click(split_audio, inputs=[audio_files, segment_duration], outputs=output)
app.launch()
|