Spaces:
Sleeping
Sleeping
import gradio as gr | |
import librosa | |
import librosa.display | |
import numpy as np | |
import soundfile as sf | |
import io | |
def trim_audio(audio_url, start_time, end_time): | |
"""Trims an audio file based on the provided start and end times.""" | |
try: | |
# Load audio from URL | |
y, sr = librosa.load(audio_url) | |
# Check for valid start and end times | |
if start_time < 0 or end_time > len(y) / sr or start_time >= end_time: | |
return "Invalid start or end time. Please check your input." | |
start_sample = int(start_time * sr) | |
end_sample = int(end_time * sr) | |
trimmed_audio = y[start_sample:end_sample] | |
# Save the trimmed audio to a BytesIO object | |
output_audio = io.BytesIO() | |
sf.write(output_audio, trimmed_audio, sr, format='wav') | |
output_audio.seek(0) # Reset the file pointer to the beginning | |
return gr.Audio(output_audio, label="Trimmed Audio") | |
except Exception as e: | |
return f"An error occurred: {e}" | |
iface = gr.Interface( | |
fn=trim_audio, | |
inputs=[ | |
gr.Textbox(label="Audio URL", lines=1), | |
gr.Number(label="Start Time (seconds)", value=0), | |
gr.Number(label="End Time (seconds)", value=5), | |
], | |
outputs=gr.Audio(label="Output Audio"), | |
title="Audio Trimmer", | |
description="Enter the URL of an audio file and specify the start and end times to trim it.", | |
) | |
iface.launch() |