File size: 3,225 Bytes
4eefe6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import ffmpeg
import random
import tempfile
import os

def boost_volume(input_file, volume_boost):
    volume_multiplier = 1 + volume_boost / 100
    output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
    (
        ffmpeg
        .input(input_file)
        .filter('volume', f'{volume_multiplier}')
        .output(output_file.name)
        .run(overwrite_output=True)
    )
    return output_file.name

def preview_volume_boost(input_file, volume_boost):
    volume_multiplier = 1 + volume_boost / 100
    duration = get_audio_duration(input_file)
    start_time = random.uniform(0, max(0, duration - 30))
    output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
    (
        ffmpeg
        .input(input_file, ss=start_time, t=30)
        .filter('volume', f'{volume_multiplier}')
        .output(output_file.name)
        .run(overwrite_output=True)
    )
    return output_file.name

def get_audio_duration(input_file):
    probe = ffmpeg.probe(input_file)
    duration = float(probe['format']['duration'])
    return duration

def process_audio(input_file, volume_boost):
    preview_file = preview_volume_boost(input_file.name, volume_boost)
    boosted_file = boost_volume(input_file.name, volume_boost)
    return preview_file, boosted_file

with gr.Blocks(analytics_enabled=False,title="MP3 Volume Booster") as demo:
    gr.Markdown("# MP3 Volume Booster")
    gr.Markdown("## Increase or decrease the volume of your MP3 files up to 500%")

    with gr.Row():
        input_audio = gr.Audio(type="filepath", label="Upload MP3 File")
        volume_boost = gr.Slider(minimum=0, maximum=500, value=300, step=1, label="Volume Boost (to +500%)")

    with gr.Row():
        preview_button = gr.Button("Preview")
        gr.Markdown("**Note:** Preview will randomly select a 30-second segment from the uploaded audio.")
        generate_button = gr.Button("Generate")
        gr.Markdown("**Note:** Generate will increase or decrease the volume of the entire audio file.")

    with gr.Row():
        preview_output = gr.Audio(label="Preview Output")
        generate_output = gr.Audio(label="Generated Output")

    def on_preview_click(input_file, volume_boost):
        preview_file = preview_volume_boost(input_file, volume_boost)
        return preview_file

    def on_generate_click(input_file, volume_boost):
        boosted_file = boost_volume(input_file, volume_boost)
        return boosted_file

    preview_button.click(on_preview_click, [input_audio, volume_boost], preview_output)
    generate_button.click(on_generate_click, [input_audio, volume_boost], generate_output)

    gr.Markdown("## How to Use")
    gr.Markdown("1. **Upload MP3 File:** Click on the 'Upload MP3 File' button to select an MP3 file from your device.")
    gr.Markdown("2. **Adjust Volume Boost:** Use the slider to adjust the volume boost level (-500% to +500%).")
    gr.Markdown("3. **Preview:** Click the 'Preview' button to hear a 30-second segment of the audio with increased or decreased volume.")
    gr.Markdown("4. **Generate:** Click the 'Generate' button to increase or decrease the volume of the entire audio file.")

demo.launch(show_api=True,show_error=True)