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()