import os import pathlib import tempfile import gradio as gr from utils import hex_to_rgb, visualize ABOUT = "# [seewav](https://github.com/adefossez/seewav)" MAX_DURATION = int(os.getenv("MAX_DURATION", "0")) def run( audio_file: str, wave_color: str = "#00237E", background_color: str = "#000000", num_bars: int = 50, video_width: int = 400, video_height: int = 300, ) -> str: """Generates a waveform video from an audio file using the seewav tool. This function processes the input audio file and creates a video visualizing its waveform. The waveform and background colors, number of waveform bars, and video resolution can be customized. Args: audio_file (str): Path to the input audio file (e.g., WAV or MP3). wave_color (str, optional): Hex color code for the waveform. Defaults to "#00237E". background_color (str, optional): Hex color code for the background. Defaults to "#000000". num_bars (int, optional): Number of bars to display in the waveform visualization. Defaults to 50. video_width (int, optional): Width of the output video in pixels. Defaults to 400. video_height (int, optional): Height of the output video in pixels. Defaults to 300. Returns: str: Path to the generated waveform video file. """ with tempfile.TemporaryDirectory() as tmp, tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as out: return visualize( audio_file, pathlib.Path(tmp), pathlib.Path(out.name), duration=MAX_DURATION if MAX_DURATION > 0 else None, bars=num_bars, fg_color=hex_to_rgb(wave_color), bg_color=hex_to_rgb(background_color), size=(video_width, video_height), ) with gr.Blocks(css_paths="style.css") as demo: gr.Markdown(ABOUT) with gr.Row(): with gr.Column(): audio_file = gr.Audio(type="filepath") with gr.Accordion("Advanced Configuration", open=False): wave_color = gr.ColorPicker(label="Waveform Color", value="#00237E") background_color = gr.ColorPicker(label="Background Color", value="#000000") num_bars = gr.Slider( label="Number of Bars", minimum=5, maximum=1500, step=5, value=50, ) video_width = gr.Slider( label="Video Width", minimum=100, maximum=3000, step=10, value=400, ) video_height = gr.Slider( label="Video Height", minimum=100, maximum=3000, step=10, value=300, ) run_button = gr.Button(variant="primary") with gr.Column(): video = gr.Video(interactive=False) gr.Examples(examples=["assets/sample.wav"], fn=run, inputs=audio_file, outputs=video) run_button.click( fn=run, inputs=[ audio_file, wave_color, background_color, num_bars, video_width, video_height, ], outputs=video, ) if __name__ == "__main__": demo.launch(mcp_server=True)