import gradio as gr import tempfile import subprocess import os import datetime def convert_webm_to_mp3(webm_file, output_filename=None): # Get current date current_date = datetime.datetime.now().strftime("%Y-%m-%d") if not output_filename: output_filename = f"Morning_and_{current_date}.mp3" else: # Ensure the filename ends with .mp3 if not output_filename.endswith(".mp3"): output_filename += ".mp3" # Create a temporary directory to store the output file temp_dir = tempfile.mkdtemp() output_path = os.path.join(temp_dir, output_filename) # Run ffmpeg command directly for conversion with error handling ffmpeg_command = [ "ffmpeg", "-y", # Overwrite output file if exists "-i", webm_file, # Input file "-vn", # No video "-acodec", "libmp3lame", # Use the mp3 codec "-q:a", "2", # Set quality to ensure good compression output_path # Output file path ] # Run the ffmpeg command and capture any errors process = subprocess.run(ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Check if there was an error during the conversion if process.returncode != 0: # Log or print the error for debugging print("Error in conversion:", process.stderr.decode()) return "Error in conversion. Please check the file format and try again." # Return the path to the .mp3 file if successful return output_path # Gradio interface iface = gr.Interface( fn=convert_webm_to_mp3, inputs=[ gr.File(type="filepath", label="Upload .webm file"), gr.Textbox(lines=1, placeholder="Enter output filename (optional)", label="Output Filename") ], outputs=gr.File(label="Converted .mp3 file"), title="WebM to MP3 Converter", description="Upload a .webm audio file, and this tool will convert it to .mp3 format. You can set a custom filename for the output." ) # Launch the interface if __name__ == "__main__": iface.launch()