Spaces:
Running
Running
import streamlit as st | |
from pydub import AudioSegment | |
from io import BytesIO | |
st.title("Audio Cutter") | |
uploaded_file = st.file_uploader("Choose an audio file", type=["mp3", "wav"]) | |
if uploaded_file is not None: | |
# Display the uploaded audio file | |
st.audio(uploaded_file, format='audio/wav') | |
# Inputs for start and end time | |
start_time = st.number_input("Start time (in seconds)", value=0, min_value=0) | |
end_time = st.number_input("End time (in seconds)", value=10, min_value=0) | |
# Check if the end time is greater than start time | |
if st.button("Cut Audio"): | |
if start_time >= end_time: | |
st.error("End time must be greater than start time.") | |
else: | |
# Load the audio file | |
audio = AudioSegment.from_file(uploaded_file) | |
# Ensure the end time does not exceed the length of the audio | |
end_time = min(end_time, len(audio) / 1000.0) | |
# Clip the audio | |
clipped_audio = audio[start_time * 1000:end_time * 1000] | |
# Export the clipped audio to a BytesIO object | |
buf = BytesIO() | |
clipped_audio.export(buf, format='wav') | |
buf.seek(0) | |
# Display the clipped audio | |
st.audio(buf, format='audio/wav') | |
# Option to download the clipped audio | |
st.download_button( | |
label="Download Clipped Audio", | |
data=buf, | |
file_name="clipped_audio.wav", | |
mime="audio/wav" | |
) | |