File size: 1,345 Bytes
b492874
 
 
 
 
 
 
 
 
 
5fc3073
b492874
 
 
 
 
5fc3073
b492874
 
 
 
 
 
 
 
 
 
 
5fc3073
b492874
 
 
5fc3073
b492874
 
 
 
 
 
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
import gradio as gr
from pydub import AudioSegment
import os

def cut_audio(audio, start_time, end_time):
    # Convert start and end times to milliseconds
    start_time_ms = start_time * 1000
    end_time_ms = end_time * 1000
    
    # Load the audio file using pydub
    audio_segment = AudioSegment.from_file(audio)
    
    # Cut the audio segment
    cut_segment = audio_segment[start_time_ms:end_time_ms]
    
    # Determine the output format based on the original file extension
    file_ext = os.path.splitext(audio)[-1].lower()
    
    # Create a temporary file to save the cut audio
    output_file = f"cut_audio{file_ext}"
    cut_segment.export(output_file, format=file_ext[1:])
    
    return output_file

# Create Gradio interface
interface = gr.Interface(
    fn=cut_audio,
    inputs=[
        gr.Audio(sources="upload", type="filepath"),  # Audio input
        gr.Number(label="Start Time (seconds)"),  # Numeric input for start time
        gr.Number(label="End Time (seconds)")     # Numeric input for end time
    ],
    outputs=gr.Audio(label="Cut Audio"),  # Output the cut audio file
    title="Audio Cutter",
    description="Upload an audio file and specify the start and end times to cut the audio. The output will be the cut audio in the same format as the original."
)

# Launch the interface
interface.launch()