Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,56 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from pydub import AudioSegment
|
| 3 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
uploaded_file = st.file_uploader("Choose an audio file", type=["mp3", "wav"])
|
| 8 |
|
| 9 |
if uploaded_file is not None:
|
| 10 |
-
|
| 11 |
st.audio(uploaded_file, format='audio/wav')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
start_time = st.number_input("Start time (in seconds)", value=0, min_value=0)
|
| 15 |
-
end_time = st.number_input("End time (in seconds)", value=10, min_value=0)
|
| 16 |
-
|
| 17 |
-
# Check if the end time is greater than start time
|
| 18 |
-
if st.button("Cut Audio"):
|
| 19 |
-
if start_time >= end_time:
|
| 20 |
-
st.error("End time must be greater than start time.")
|
| 21 |
-
else:
|
| 22 |
-
# Load the audio file
|
| 23 |
-
audio = AudioSegment.from_file(uploaded_file)
|
| 24 |
-
|
| 25 |
-
# Ensure the end time does not exceed the length of the audio
|
| 26 |
-
end_time = min(end_time, len(audio) / 1000.0)
|
| 27 |
-
|
| 28 |
-
# Clip the audio
|
| 29 |
clipped_audio = audio[start_time * 1000:end_time * 1000]
|
| 30 |
|
| 31 |
-
# Export the clipped audio to a BytesIO object
|
| 32 |
buf = BytesIO()
|
| 33 |
clipped_audio.export(buf, format='wav')
|
| 34 |
buf.seek(0)
|
| 35 |
|
| 36 |
-
# Display the clipped audio
|
| 37 |
st.audio(buf, format='audio/wav')
|
| 38 |
|
| 39 |
-
# Option to download the clipped audio
|
| 40 |
st.download_button(
|
| 41 |
label="Download Clipped Audio",
|
| 42 |
data=buf,
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from pydub import AudioSegment
|
| 3 |
from io import BytesIO
|
| 4 |
+
import numpy as np
|
| 5 |
+
import plotly.graph_objs as go
|
| 6 |
+
import plotly.express as px
|
| 7 |
|
| 8 |
+
# Function to plot the waveform
|
| 9 |
+
def plot_waveform(audio, start_time, end_time):
|
| 10 |
+
samples = np.array(audio.get_array_of_samples())
|
| 11 |
+
sample_rate = audio.frame_rate
|
| 12 |
+
duration = len(samples) / sample_rate
|
| 13 |
+
|
| 14 |
+
times = np.linspace(0, duration, num=len(samples))
|
| 15 |
+
|
| 16 |
+
fig = go.Figure()
|
| 17 |
+
fig.add_trace(go.Scatter(x=times, y=samples, mode='lines', name='Waveform'))
|
| 18 |
+
fig.update_layout(title='Audio Waveform', xaxis_title='Time (s)', yaxis_title='Amplitude')
|
| 19 |
+
|
| 20 |
+
fig.add_vrect(x0=start_time, x1=end_time, fillcolor="LightSalmon", opacity=0.5, layer="below", line_width=0)
|
| 21 |
+
return fig
|
| 22 |
+
|
| 23 |
+
# Streamlit UI
|
| 24 |
+
st.title("Interactive Audio Cutter")
|
| 25 |
|
| 26 |
uploaded_file = st.file_uploader("Choose an audio file", type=["mp3", "wav"])
|
| 27 |
|
| 28 |
if uploaded_file is not None:
|
| 29 |
+
audio = AudioSegment.from_file(uploaded_file)
|
| 30 |
st.audio(uploaded_file, format='audio/wav')
|
| 31 |
+
|
| 32 |
+
duration = len(audio) / 1000.0
|
| 33 |
+
st.write(f"Audio Duration: {duration:.2f} seconds")
|
| 34 |
+
|
| 35 |
+
# Initial slider values
|
| 36 |
+
start_time = st.slider("Start time (in seconds)", 0.0, duration, 0.0)
|
| 37 |
+
end_time = st.slider("End time (in seconds)", 0.0, duration, duration)
|
| 38 |
+
|
| 39 |
+
if start_time >= end_time:
|
| 40 |
+
st.error("End time must be greater than start time.")
|
| 41 |
+
else:
|
| 42 |
+
fig = plot_waveform(audio, start_time, end_time)
|
| 43 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 44 |
|
| 45 |
+
if st.button("Cut Audio"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
clipped_audio = audio[start_time * 1000:end_time * 1000]
|
| 47 |
|
|
|
|
| 48 |
buf = BytesIO()
|
| 49 |
clipped_audio.export(buf, format='wav')
|
| 50 |
buf.seek(0)
|
| 51 |
|
|
|
|
| 52 |
st.audio(buf, format='audio/wav')
|
| 53 |
|
|
|
|
| 54 |
st.download_button(
|
| 55 |
label="Download Clipped Audio",
|
| 56 |
data=buf,
|