Spaces:
Sleeping
Sleeping
Ahmadkhan12
commited on
Commit
•
7d28942
1
Parent(s):
07b94ca
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import ffmpeg
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
def split_audio_ffmpeg(input_audio, chunk_duration=30):
|
6 |
+
"""
|
7 |
+
Splits an audio file into smaller chunks using FFmpeg.
|
8 |
+
:param input_audio: The uploaded audio file
|
9 |
+
:param chunk_duration: Duration of each chunk in seconds
|
10 |
+
"""
|
11 |
+
# Define input and output paths
|
12 |
+
input_audio_path = input_audio.name
|
13 |
+
output_directory = "./output_chunks"
|
14 |
+
os.makedirs(output_directory, exist_ok=True)
|
15 |
+
|
16 |
+
try:
|
17 |
+
# Get audio duration
|
18 |
+
probe = ffmpeg.probe(input_audio_path, v='error', select_streams='a', show_entries='stream=duration')
|
19 |
+
duration = float(probe['streams'][0]['duration'])
|
20 |
+
print(f"Audio duration: {duration} seconds")
|
21 |
+
|
22 |
+
# Split audio into chunks
|
23 |
+
for i in range(0, int(duration), chunk_duration):
|
24 |
+
chunk_file = os.path.join(output_directory, f"chunk_{i // chunk_duration + 1}.mp3")
|
25 |
+
ffmpeg.input(input_audio_path, ss=i, t=chunk_duration).output(chunk_file).run(quiet=True)
|
26 |
+
print(f"Generated: {chunk_file}")
|
27 |
+
|
28 |
+
# Collect all chunk paths for download
|
29 |
+
return [os.path.join(output_directory, f) for f in os.listdir(output_directory)]
|
30 |
+
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Error during splitting: {e}")
|
33 |
+
return []
|
34 |
+
|
35 |
+
# Gradio interface
|
36 |
+
def process_audio(input_audio):
|
37 |
+
chunks = split_audio_ffmpeg(input_audio)
|
38 |
+
if not chunks:
|
39 |
+
return "An error occurred during processing."
|
40 |
+
return chunks
|
41 |
+
|
42 |
+
# Launch Gradio interface
|
43 |
+
interface = gr.Interface(
|
44 |
+
fn=process_audio,
|
45 |
+
inputs=gr.File(label="Upload Audio File", file_types=["audio"]),
|
46 |
+
outputs=gr.File(label="Download Chunks", file_count="multiple"),
|
47 |
+
title="Audio Splitter"
|
48 |
+
)
|
49 |
+
|
50 |
+
interface.launch(share=True)
|