|
import gradio as gr |
|
import tempfile |
|
import subprocess |
|
import os |
|
import datetime |
|
|
|
def convert_webm_to_mp3(webm_file, output_filename=None): |
|
|
|
current_date = datetime.datetime.now().strftime("%Y-%m-%d") |
|
if not output_filename: |
|
output_filename = f"Morning_and_{current_date}.mp3" |
|
else: |
|
|
|
if not output_filename.endswith(".mp3"): |
|
output_filename += ".mp3" |
|
|
|
|
|
temp_dir = tempfile.mkdtemp() |
|
output_path = os.path.join(temp_dir, output_filename) |
|
|
|
|
|
ffmpeg_command = [ |
|
"ffmpeg", "-y", |
|
"-i", webm_file, |
|
"-vn", |
|
"-acodec", "libmp3lame", |
|
"-q:a", "2", |
|
output_path |
|
] |
|
|
|
|
|
process = subprocess.run(ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
|
|
|
|
if process.returncode != 0: |
|
|
|
print("Error in conversion:", process.stderr.decode()) |
|
return "Error in conversion. Please check the file format and try again." |
|
|
|
|
|
return output_path |
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|