Gorgefound commited on
Commit
15ed22f
1 Parent(s): 184ba23

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from pydub import AudioSegment, silence
3
+ import zipfile
4
+ import os
5
+
6
+ def remove_silence(audio):
7
+ # Remove silence
8
+ non_silent_audio = silence.split_on_silence(audio, min_silence_len=1000, silence_thresh=-40)
9
+ return non_silent_audio
10
+
11
+ def join_audio_segments(segments, segment_duration):
12
+ joined_segments = []
13
+ current_segment = None
14
+ for segment in segments:
15
+ if current_segment is None:
16
+ current_segment = segment
17
+ elif current_segment.duration_seconds < segment_duration:
18
+ current_segment += segment
19
+ else:
20
+ joined_segments.append(current_segment)
21
+ current_segment = segment
22
+
23
+ if current_segment is not None:
24
+ if current_segment.duration_seconds < segment_duration:
25
+ if joined_segments:
26
+ joined_segments[-1] += current_segment
27
+ else:
28
+ joined_segments.append(current_segment)
29
+ else:
30
+ joined_segments.append(current_segment)
31
+
32
+ return joined_segments
33
+
34
+ def process_audio(files, segment_duration):
35
+ zip_file_name = "audio_segments.zip"
36
+ with zipfile.ZipFile(zip_file_name, 'w') as zip_file:
37
+ for i, audio_file in enumerate(files):
38
+ audio = AudioSegment.from_file(audio_file.name)
39
+
40
+ # Check if the file is in mp3 format
41
+ is_mp3 = False
42
+ if audio_file.name.lower().endswith('.mp3'):
43
+ is_mp3 = True
44
+
45
+ # Remove silence
46
+ non_silent_audio = remove_silence(audio)
47
+
48
+ segments = []
49
+ for j, segment in enumerate(non_silent_audio):
50
+ if segment.duration_seconds >= segment_duration:
51
+ segments.extend(segment[0:segment_duration * 1000] for segment in segment[::segment_duration * 1000])
52
+ else:
53
+ segments.append(segment)
54
+
55
+ # Join segments less than the specified duration with the nearest audio segment
56
+ joined_segments = join_audio_segments(segments, segment_duration)
57
+
58
+ for k, segment in enumerate(joined_segments):
59
+ segment_file_name = f"segment_{i + 1}_{k + 1}.wav" if not is_mp3 else f"segment_{i + 1}_{k + 1}.mp3"
60
+ segment.export(segment_file_name, format="wav" if not is_mp3 else "mp3")
61
+ zip_file.write(segment_file_name)
62
+ os.remove(segment_file_name)
63
+
64
+ return zip_file_name
65
+
66
+ def split_audio(files, segment_duration):
67
+ try:
68
+ segment_duration = int(segment_duration)
69
+ except ValueError:
70
+ return "Invalid segment duration. Please enter an integer."
71
+
72
+ if len(files) == 0:
73
+ return "Please upload audio files."
74
+
75
+ zip_path = process_audio(files, segment_duration)
76
+ return zip_path
77
+
78
+ # Gradio Interface
79
+ with gr.Blocks() as app:
80
+ gr.Markdown("## Audio Splitter")
81
+
82
+ with gr.Row():
83
+ audio_files = gr.File(label="Upload Audio Files", file_count="multiple", file_types=["audio"])
84
+ segment_duration = gr.Number(label="Segment Duration (seconds)", value=5)
85
+
86
+ split_button = gr.Button("Split Audio Files")
87
+ output = gr.File(label="Download Split Audio Segments")
88
+
89
+ split_button.click(split_audio, inputs=[audio_files, segment_duration], outputs=output)
90
+
91
+ app.launch()