Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,17 +4,18 @@ import numpy as np
|
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
from pathlib import Path
|
|
|
|
| 7 |
import warnings
|
| 8 |
warnings.filterwarnings("ignore")
|
| 9 |
|
| 10 |
-
def convert_mp3_to_flac(
|
| 11 |
"""Convert MP3 file to 16-bit FLAC"""
|
| 12 |
try:
|
| 13 |
progress(0, desc="Starting conversion...")
|
| 14 |
|
| 15 |
# Read MP3 file
|
| 16 |
progress(0.2, desc="Loading MP3 file...")
|
| 17 |
-
audio = pydub.AudioSegment.from_mp3(
|
| 18 |
|
| 19 |
# Ensure it's 16-bit (PCM_16)
|
| 20 |
progress(0.5, desc="Converting to 16-bit PCM...")
|
|
@@ -48,50 +49,108 @@ def cleanup_temp_files():
|
|
| 48 |
pass
|
| 49 |
|
| 50 |
# Create Gradio interface
|
| 51 |
-
with gr.Blocks(title="MP3 to 16-bit FLAC Converter") as demo:
|
| 52 |
gr.Markdown("# 🎵 MP3 to 16-bit FLAC Converter")
|
| 53 |
gr.Markdown("Upload an MP3 file and convert it to 16-bit FLAC format")
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
-
with gr.Column():
|
| 57 |
mp3_input = gr.File(
|
| 58 |
label="Upload MP3 File",
|
| 59 |
file_types=[".mp3"],
|
| 60 |
-
type="file
|
| 61 |
)
|
| 62 |
-
convert_btn = gr.Button("Convert to FLAC", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
with gr.Column():
|
| 65 |
flac_output = gr.File(
|
| 66 |
label="Download FLAC File",
|
| 67 |
file_types=[".flac"]
|
| 68 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Additional info
|
| 71 |
with gr.Accordion("ℹ️ About this converter", open=False):
|
| 72 |
gr.Markdown("""
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
- Converts MP3 to FLAC format
|
| 75 |
- Ensures 16-bit depth (CD quality)
|
| 76 |
-
- Maintains original sample rate
|
| 77 |
- Lossless compression for FLAC output
|
| 78 |
|
| 79 |
-
**Technical
|
| 80 |
-
- Uses pydub for audio processing
|
| 81 |
-
- FLAC files use 16-bit PCM encoding
|
| 82 |
- Original channels (mono/stereo) are preserved
|
|
|
|
| 83 |
|
| 84 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
""")
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
# Connect the conversion function
|
| 88 |
convert_btn.click(
|
| 89 |
fn=convert_mp3_to_flac,
|
| 90 |
inputs=[mp3_input],
|
| 91 |
outputs=[flac_output]
|
| 92 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
# Clean up old temp files on startup
|
| 96 |
cleanup_temp_files()
|
| 97 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
from pathlib import Path
|
| 7 |
+
import time
|
| 8 |
import warnings
|
| 9 |
warnings.filterwarnings("ignore")
|
| 10 |
|
| 11 |
+
def convert_mp3_to_flac(mp3_file_path, progress=gr.Progress()):
|
| 12 |
"""Convert MP3 file to 16-bit FLAC"""
|
| 13 |
try:
|
| 14 |
progress(0, desc="Starting conversion...")
|
| 15 |
|
| 16 |
# Read MP3 file
|
| 17 |
progress(0.2, desc="Loading MP3 file...")
|
| 18 |
+
audio = pydub.AudioSegment.from_mp3(mp3_file_path)
|
| 19 |
|
| 20 |
# Ensure it's 16-bit (PCM_16)
|
| 21 |
progress(0.5, desc="Converting to 16-bit PCM...")
|
|
|
|
| 49 |
pass
|
| 50 |
|
| 51 |
# Create Gradio interface
|
| 52 |
+
with gr.Blocks(title="MP3 to 16-bit FLAC Converter", theme=gr.themes.Soft()) as demo:
|
| 53 |
gr.Markdown("# 🎵 MP3 to 16-bit FLAC Converter")
|
| 54 |
gr.Markdown("Upload an MP3 file and convert it to 16-bit FLAC format")
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
+
with gr.Column(scale=1):
|
| 58 |
mp3_input = gr.File(
|
| 59 |
label="Upload MP3 File",
|
| 60 |
file_types=[".mp3"],
|
| 61 |
+
type="filepath" # Fixed: Changed from 'file' to 'filepath'
|
| 62 |
)
|
| 63 |
+
convert_btn = gr.Button("Convert to FLAC", variant="primary", scale=0)
|
| 64 |
+
|
| 65 |
+
with gr.Accordion("Settings", open=False):
|
| 66 |
+
info_text = gr.Markdown("""
|
| 67 |
+
**Default Settings:**
|
| 68 |
+
- Output format: FLAC
|
| 69 |
+
- Bit depth: 16-bit
|
| 70 |
+
- Channels: Preserved from source
|
| 71 |
+
- Sample rate: Preserved from source
|
| 72 |
+
""")
|
| 73 |
|
| 74 |
+
with gr.Column(scale=1):
|
| 75 |
flac_output = gr.File(
|
| 76 |
label="Download FLAC File",
|
| 77 |
file_types=[".flac"]
|
| 78 |
)
|
| 79 |
+
info_box = gr.Markdown("""
|
| 80 |
+
**Conversion Status:** Waiting for file upload...
|
| 81 |
+
|
| 82 |
+
**Note:**
|
| 83 |
+
- Maximum file size: 200MB
|
| 84 |
+
- Processing time depends on file size
|
| 85 |
+
- Download link will appear after conversion
|
| 86 |
+
""")
|
| 87 |
+
|
| 88 |
+
# Stats display
|
| 89 |
+
with gr.Row():
|
| 90 |
+
with gr.Column():
|
| 91 |
+
stats_text = gr.Markdown("**File Statistics:** No file processed yet")
|
| 92 |
|
| 93 |
# Additional info
|
| 94 |
with gr.Accordion("ℹ️ About this converter", open=False):
|
| 95 |
gr.Markdown("""
|
| 96 |
+
## Features
|
| 97 |
+
|
| 98 |
+
**Audio Conversion:**
|
| 99 |
- Converts MP3 to FLAC format
|
| 100 |
- Ensures 16-bit depth (CD quality)
|
| 101 |
+
- Maintains original sample rate and channels
|
| 102 |
- Lossless compression for FLAC output
|
| 103 |
|
| 104 |
+
**Technical Specifications:**
|
| 105 |
+
- Uses pydub (FFmpeg wrapper) for audio processing
|
| 106 |
+
- FLAC files use 16-bit PCM encoding (signed 16-bit integer)
|
| 107 |
- Original channels (mono/stereo) are preserved
|
| 108 |
+
- No quality loss in format conversion
|
| 109 |
|
| 110 |
+
**Limitations:**
|
| 111 |
+
- This converts formats, but cannot enhance audio quality
|
| 112 |
+
- MP3 files are already lossy compressed
|
| 113 |
+
- Output FLAC will have same perceived quality as input MP3
|
| 114 |
+
|
| 115 |
+
**Supported Formats:** .mp3 → .flac
|
| 116 |
""")
|
| 117 |
|
| 118 |
+
def update_stats(mp3_path):
|
| 119 |
+
if mp3_path:
|
| 120 |
+
try:
|
| 121 |
+
audio = pydub.AudioSegment.from_mp3(mp3_path)
|
| 122 |
+
stats = f"""
|
| 123 |
+
**File Statistics:**
|
| 124 |
+
- Duration: {len(audio) / 1000:.2f} seconds
|
| 125 |
+
- Channels: {'Mono' if audio.channels == 1 else 'Stereo'}
|
| 126 |
+
- Sample Rate: {audio.frame_rate} Hz
|
| 127 |
+
- Bit Depth: 16-bit (output)
|
| 128 |
+
- File Size: {os.path.getsize(mp3_path) / 1024 / 1024:.2f} MB
|
| 129 |
+
"""
|
| 130 |
+
return stats
|
| 131 |
+
except:
|
| 132 |
+
return "**File Statistics:** Could not read file details"
|
| 133 |
+
return "**File Statistics:** No file processed yet"
|
| 134 |
+
|
| 135 |
# Connect the conversion function
|
| 136 |
convert_btn.click(
|
| 137 |
fn=convert_mp3_to_flac,
|
| 138 |
inputs=[mp3_input],
|
| 139 |
outputs=[flac_output]
|
| 140 |
)
|
| 141 |
+
|
| 142 |
+
# Update stats when file is uploaded
|
| 143 |
+
mp3_input.change(
|
| 144 |
+
fn=update_stats,
|
| 145 |
+
inputs=[mp3_input],
|
| 146 |
+
outputs=[stats_text]
|
| 147 |
+
)
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|
| 150 |
# Clean up old temp files on startup
|
| 151 |
cleanup_temp_files()
|
| 152 |
+
demo.launch(
|
| 153 |
+
server_name="0.0.0.0",
|
| 154 |
+
server_port=7860,
|
| 155 |
+
share=False
|
| 156 |
+
)
|