File size: 3,427 Bytes
bd8bc39
9ee5547
df8f6a6
 
9ee5547
df8f6a6
9ee5547
 
 
 
bd8bc39
 
9ee5547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df8f6a6
9ee5547
df8f6a6
9ee5547
 
 
bd8bc39
9ee5547
 
 
 
df8f6a6
 
 
9ee5547
df8f6a6
 
 
9ee5547
df8f6a6
9ee5547
 
 
 
781750e
dc293d0
9ee5547
 
df8f6a6
9ee5547
df8f6a6
 
 
9ee5547
 
df8f6a6
9ee5547
df8f6a6
 
 
9ee5547
 
df8f6a6
9ee5547
df8f6a6
9ee5547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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)