Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import librosa
|
3 |
+
import librosa.display
|
4 |
+
import numpy as np
|
5 |
+
import soundfile as sf
|
6 |
+
import io
|
7 |
+
|
8 |
+
def trim_audio(audio_url, start_time, end_time):
|
9 |
+
"""Trims an audio file based on the provided start and end times."""
|
10 |
+
try:
|
11 |
+
# Load audio from URL
|
12 |
+
y, sr = librosa.load(audio_url)
|
13 |
+
|
14 |
+
# Check for valid start and end times
|
15 |
+
if start_time < 0 or end_time > len(y) / sr or start_time >= end_time:
|
16 |
+
return "Invalid start or end time. Please check your input."
|
17 |
+
|
18 |
+
|
19 |
+
start_sample = int(start_time * sr)
|
20 |
+
end_sample = int(end_time * sr)
|
21 |
+
trimmed_audio = y[start_sample:end_sample]
|
22 |
+
|
23 |
+
# Save the trimmed audio to a BytesIO object
|
24 |
+
output_audio = io.BytesIO()
|
25 |
+
sf.write(output_audio, trimmed_audio, sr, format='wav')
|
26 |
+
output_audio.seek(0) # Reset the file pointer to the beginning
|
27 |
+
|
28 |
+
return gr.Audio(output_audio, label="Trimmed Audio")
|
29 |
+
|
30 |
+
except Exception as e:
|
31 |
+
return f"An error occurred: {e}"
|
32 |
+
|
33 |
+
|
34 |
+
iface = gr.Interface(
|
35 |
+
fn=trim_audio,
|
36 |
+
inputs=[
|
37 |
+
gr.Textbox(label="Audio URL", lines=1),
|
38 |
+
gr.Number(label="Start Time (seconds)", value=0),
|
39 |
+
gr.Number(label="End Time (seconds)", value=5),
|
40 |
+
],
|
41 |
+
outputs=gr.Audio(label="Output Audio"),
|
42 |
+
title="Audio Trimmer",
|
43 |
+
description="Enter the URL of an audio file and specify the start and end times to trim it.",
|
44 |
+
)
|
45 |
+
|
46 |
+
iface.launch()
|