import gradio as gr import subprocess import os import tempfile import imageio_ffmpeg def convert_audio(input_files, progress=gr.Progress()): if not input_files: return [] # This dynamically gets the path to the FFmpeg binary that pip installed! ffmpeg_exe = imageio_ffmpeg.get_ffmpeg_exe() output_files = [] # Create a temporary directory to store the converted files safely out_dir = tempfile.mkdtemp() # progress.tqdm creates a progress bar in the Gradio UI for file_path in progress.tqdm(input_files, desc="Converting Audio..."): filename = os.path.basename(file_path) name_only = os.path.splitext(filename)[0] out_path = os.path.join(out_dir, f"{name_only}_96k_24b.m4a") # FFmpeg command using the pip-installed binary command = [ ffmpeg_exe, "-y", "-v", "error", "-i", file_path, "-c:a", "alac", # Apple Lossless Audio Codec "-ar", "96000", # Downsample to 96kHz "-sample_fmt", "s32p", # Padding to ensure 24-bit depth is preserved out_path ] try: # Execute the conversion subprocess.run(command, check=True) output_files.append(out_path) except subprocess.CalledProcessError as e: print(f"Error processing {filename}: {e}") return output_files # Build the Gradio Interface with gr.Blocks(title="High-Res Audio Converter") as app: gr.Markdown("# 🎵 Pure-Python High-Res FLAC to ALAC Converter") gr.Markdown("Upload **384kHz / 24-bit FLAC** files. They will be converted to **96kHz / 24-bit ALAC (.m4a)** natively in your Python environment.") with gr.Row(): audio_in = gr.File(label="1. Upload FLAC files", file_count="multiple", file_types=[".flac"], type="filepath") audio_out = gr.File(label="2. Download Converted Files", file_count="multiple", interactive=False) convert_btn = gr.Button("Convert to ALAC (96kHz 24-bit)", variant="primary") convert_btn.click(fn=convert_audio, inputs=audio_in, outputs=audio_out) if __name__ == "__main__": app.launch()