Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,78 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import ffmpeg
|
| 3 |
-
import random
|
| 4 |
-
import tempfile
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
-
def boost_volume(input_file, volume_boost):
|
| 8 |
-
volume_multiplier = 1 + volume_boost / 100
|
| 9 |
-
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 10 |
-
(
|
| 11 |
-
ffmpeg
|
| 12 |
-
.input(input_file)
|
| 13 |
-
.filter('volume', f'{volume_multiplier}')
|
| 14 |
-
.output(output_file.name)
|
| 15 |
-
.run(overwrite_output=True)
|
| 16 |
-
)
|
| 17 |
-
return output_file.name
|
| 18 |
-
|
| 19 |
-
def preview_volume_boost(input_file, volume_boost):
|
| 20 |
-
volume_multiplier = 1 + volume_boost / 100
|
| 21 |
-
duration = get_audio_duration(input_file)
|
| 22 |
-
start_time = random.uniform(0, max(0, duration - 30))
|
| 23 |
-
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 24 |
-
(
|
| 25 |
-
ffmpeg
|
| 26 |
-
.input(input_file, ss=start_time, t=30)
|
| 27 |
-
.filter('volume', f'{volume_multiplier}')
|
| 28 |
-
.output(output_file.name)
|
| 29 |
-
.run(overwrite_output=True)
|
| 30 |
-
)
|
| 31 |
-
return output_file.name
|
| 32 |
-
|
| 33 |
-
def get_audio_duration(input_file):
|
| 34 |
-
probe = ffmpeg.probe(input_file)
|
| 35 |
-
duration = float(probe['format']['duration'])
|
| 36 |
-
return duration
|
| 37 |
-
|
| 38 |
-
def process_audio(input_file, volume_boost):
|
| 39 |
-
preview_file = preview_volume_boost(input_file.name, volume_boost)
|
| 40 |
-
boosted_file = boost_volume(input_file.name, volume_boost)
|
| 41 |
-
return preview_file, boosted_file
|
| 42 |
-
|
| 43 |
-
with gr.Blocks(analytics_enabled=False,title="MP3 Volume Booster") as demo:
|
| 44 |
-
gr.Markdown("# MP3 Volume Booster")
|
| 45 |
-
gr.Markdown("## Increase or decrease the volume of your MP3 files up to 500%")
|
| 46 |
-
|
| 47 |
-
with gr.Row():
|
| 48 |
-
input_audio = gr.Audio(type="filepath", label="Upload MP3 File")
|
| 49 |
-
volume_boost = gr.Slider(minimum=0, maximum=500, value=300, step=1, label="Volume Boost (to +500%)")
|
| 50 |
-
|
| 51 |
-
with gr.Row():
|
| 52 |
-
preview_button = gr.Button("Preview")
|
| 53 |
-
gr.Markdown("**Note:** Preview will randomly select a 30-second segment from the uploaded audio.")
|
| 54 |
-
generate_button = gr.Button("Generate")
|
| 55 |
-
gr.Markdown("**Note:** Generate will increase or decrease the volume of the entire audio file.")
|
| 56 |
-
|
| 57 |
-
with gr.Row():
|
| 58 |
-
preview_output = gr.Audio(label="Preview Output")
|
| 59 |
-
generate_output = gr.Audio(label="Generated Output")
|
| 60 |
-
|
| 61 |
-
def on_preview_click(input_file, volume_boost):
|
| 62 |
-
preview_file = preview_volume_boost(input_file, volume_boost)
|
| 63 |
-
return preview_file
|
| 64 |
-
|
| 65 |
-
def on_generate_click(input_file, volume_boost):
|
| 66 |
-
boosted_file = boost_volume(input_file, volume_boost)
|
| 67 |
-
return boosted_file
|
| 68 |
-
|
| 69 |
-
preview_button.click(on_preview_click, [input_audio, volume_boost], preview_output)
|
| 70 |
-
generate_button.click(on_generate_click, [input_audio, volume_boost], generate_output)
|
| 71 |
-
|
| 72 |
-
gr.Markdown("## How to Use")
|
| 73 |
-
gr.Markdown("1. **Upload MP3 File:** Click on the 'Upload MP3 File' button to select an MP3 file from your device.")
|
| 74 |
-
gr.Markdown("2. **Adjust Volume Boost:** Use the slider to adjust the volume boost level (-500% to +500%).")
|
| 75 |
-
gr.Markdown("3. **Preview:** Click the 'Preview' button to hear a 30-second segment of the audio with increased or decreased volume.")
|
| 76 |
-
gr.Markdown("4. **Generate:** Click the 'Generate' button to increase or decrease the volume of the entire audio file.")
|
| 77 |
-
|
| 78 |
-
demo.launch(show_api=
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import ffmpeg
|
| 3 |
+
import random
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def boost_volume(input_file, volume_boost):
|
| 8 |
+
volume_multiplier = 1 + volume_boost / 100
|
| 9 |
+
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 10 |
+
(
|
| 11 |
+
ffmpeg
|
| 12 |
+
.input(input_file)
|
| 13 |
+
.filter('volume', f'{volume_multiplier}')
|
| 14 |
+
.output(output_file.name)
|
| 15 |
+
.run(overwrite_output=True)
|
| 16 |
+
)
|
| 17 |
+
return output_file.name
|
| 18 |
+
|
| 19 |
+
def preview_volume_boost(input_file, volume_boost):
|
| 20 |
+
volume_multiplier = 1 + volume_boost / 100
|
| 21 |
+
duration = get_audio_duration(input_file)
|
| 22 |
+
start_time = random.uniform(0, max(0, duration - 30))
|
| 23 |
+
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 24 |
+
(
|
| 25 |
+
ffmpeg
|
| 26 |
+
.input(input_file, ss=start_time, t=30)
|
| 27 |
+
.filter('volume', f'{volume_multiplier}')
|
| 28 |
+
.output(output_file.name)
|
| 29 |
+
.run(overwrite_output=True)
|
| 30 |
+
)
|
| 31 |
+
return output_file.name
|
| 32 |
+
|
| 33 |
+
def get_audio_duration(input_file):
|
| 34 |
+
probe = ffmpeg.probe(input_file)
|
| 35 |
+
duration = float(probe['format']['duration'])
|
| 36 |
+
return duration
|
| 37 |
+
|
| 38 |
+
def process_audio(input_file, volume_boost):
|
| 39 |
+
preview_file = preview_volume_boost(input_file.name, volume_boost)
|
| 40 |
+
boosted_file = boost_volume(input_file.name, volume_boost)
|
| 41 |
+
return preview_file, boosted_file
|
| 42 |
+
|
| 43 |
+
with gr.Blocks(analytics_enabled=False,title="MP3 Volume Booster") as demo:
|
| 44 |
+
gr.Markdown("# MP3 Volume Booster")
|
| 45 |
+
gr.Markdown("## Increase or decrease the volume of your MP3 files up to 500%")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
input_audio = gr.Audio(type="filepath", label="Upload MP3 File")
|
| 49 |
+
volume_boost = gr.Slider(minimum=0, maximum=500, value=300, step=1, label="Volume Boost (to +500%)")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
preview_button = gr.Button("Preview")
|
| 53 |
+
gr.Markdown("**Note:** Preview will randomly select a 30-second segment from the uploaded audio.")
|
| 54 |
+
generate_button = gr.Button("Generate")
|
| 55 |
+
gr.Markdown("**Note:** Generate will increase or decrease the volume of the entire audio file.")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
preview_output = gr.Audio(label="Preview Output")
|
| 59 |
+
generate_output = gr.Audio(label="Generated Output")
|
| 60 |
+
|
| 61 |
+
def on_preview_click(input_file, volume_boost):
|
| 62 |
+
preview_file = preview_volume_boost(input_file, volume_boost)
|
| 63 |
+
return preview_file
|
| 64 |
+
|
| 65 |
+
def on_generate_click(input_file, volume_boost):
|
| 66 |
+
boosted_file = boost_volume(input_file, volume_boost)
|
| 67 |
+
return boosted_file
|
| 68 |
+
|
| 69 |
+
preview_button.click(on_preview_click, [input_audio, volume_boost], preview_output)
|
| 70 |
+
generate_button.click(on_generate_click, [input_audio, volume_boost], generate_output)
|
| 71 |
+
|
| 72 |
+
gr.Markdown("## How to Use")
|
| 73 |
+
gr.Markdown("1. **Upload MP3 File:** Click on the 'Upload MP3 File' button to select an MP3 file from your device.")
|
| 74 |
+
gr.Markdown("2. **Adjust Volume Boost:** Use the slider to adjust the volume boost level (-500% to +500%).")
|
| 75 |
+
gr.Markdown("3. **Preview:** Click the 'Preview' button to hear a 30-second segment of the audio with increased or decreased volume.")
|
| 76 |
+
gr.Markdown("4. **Generate:** Click the 'Generate' button to increase or decrease the volume of the entire audio file.")
|
| 77 |
+
|
| 78 |
+
demo.launch(show_api=True,show_error=True)
|