Spaces:
Paused
Paused
Update app.py
Browse files39 unmodified lines40414243444539 unmodified lineserror_msg = f"Conversion failed: {str(e)}"raise gr.Error(error_msg)def get_file_stats(file_path):"""Get statistics about the uploaded file"""if not file_path or not os.path.exists(file_path):39 unmodified lines40414243444546474849505152535455565758596061626364656667686970717273747576777879808182838439 unmodified lineserror_msg = f"Conversion failed: {str(e)}"raise gr.Error(error_msg)def convert_mp3_to_flac(mp3_file_path, progress=gr.Progress()):"""Convert MP3 file to 16-bit FLAC at 44.1kHz"""try:if not mp3_file_path:raise ValueError("No file uploaded")progress(0, desc="Starting conversion...")# Read MP3 fileprogress(0.2, desc="Loading MP3 file...")audio = pydub.AudioSegment.from_mp3(mp3_file_path)# 1. Ensure 16-bit depthprogress(0.4, desc="Converting to 16-bit PCM...")audio = audio.set_sample_width(2) # 2 bytes = 16 bits# 2. NEW STEP: Resample to 44.1 kHz if needed[citation:5][citation:7]# The sample rate is stored as `frame_rate` in pydub[citation:7]if audio.frame_rate != 44100:progress(0.6, desc=f"Resampling from {audio.frame_rate} Hz to 44.1 kHz...")audio = audio.set_frame_rate(44100) # Resample to the required rate# Save as FLACprogress(0.8, desc="Saving as 44.1kHz FLAC...")# Create temporary file for outputwith tempfile.NamedTemporaryFile(suffix='.flac', delete=False) as tmp:flac_path = tmp.name# Export as FLAC with explicit parametersaudio.export(flac_path, format="flac", parameters=["-sample_fmt", "s16", "-ar", "44100"])progress(1.0, desc="Conversion complete!")return flac_pathexcept Exception as e:raise gr.Error(f"Conversion failed: {str(e)}")def get_file_stats(file_path):"""Get statistics about the uploaded file"""if not file_path or not os.path.exists(file_path):
app.py+39