MySafeCode commited on
Commit
a543f09
·
verified ·
1 Parent(s): 63ddf37

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pydub
3
+ 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(mp3_file, progress=gr.Progress()):
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(mp3_file.name)
18
+
19
+ # Ensure it's 16-bit (PCM_16)
20
+ progress(0.5, desc="Converting to 16-bit PCM...")
21
+ audio = audio.set_sample_width(2) # 2 bytes = 16 bits
22
+
23
+ # Save as FLAC
24
+ progress(0.8, desc="Saving as FLAC...")
25
+
26
+ # Create temporary file for output
27
+ with tempfile.NamedTemporaryFile(suffix='.flac', delete=False) as tmp:
28
+ flac_path = tmp.name
29
+
30
+ # Export as FLAC
31
+ audio.export(flac_path, format="flac", parameters=["-sample_fmt", "s16"])
32
+
33
+ progress(1.0, desc="Conversion complete!")
34
+
35
+ return flac_path
36
+
37
+ except Exception as e:
38
+ raise gr.Error(f"Conversion failed: {str(e)}")
39
+
40
+ def cleanup_temp_files():
41
+ """Clean up temporary files if needed"""
42
+ temp_dir = tempfile.gettempdir()
43
+ for file in Path(temp_dir).glob("*.flac"):
44
+ try:
45
+ if file.stat().st_mtime < (time.time() - 3600): # Older than 1 hour
46
+ file.unlink()
47
+ except:
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
+ **Features:**
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 details:**
80
+ - Uses pydub for audio processing
81
+ - FLAC files use 16-bit PCM encoding
82
+ - Original channels (mono/stereo) are preserved
83
+
84
+ **Note:** This is a format conversion, not quality enhancement.
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(share=False)