Spaces:
Running
Running
File size: 1,416 Bytes
d7bc426 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
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() |