mobenta commited on
Commit
2401333
·
verified ·
1 Parent(s): e2d0bc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -9
app.py CHANGED
@@ -1,12 +1,22 @@
1
-
2
  import gradio as gr
3
  import tempfile
4
  import subprocess
 
 
5
 
6
- def convert_webm_to_mp3(webm_file):
7
- # Create a temporary file for the output .mp3
8
- with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp_mp3:
9
- tmp_mp3_path = tmp_mp3.name
 
 
 
 
 
 
 
 
 
10
 
11
  # Run ffmpeg command directly for conversion with error handling
12
  ffmpeg_command = [
@@ -15,7 +25,7 @@ def convert_webm_to_mp3(webm_file):
15
  "-vn", # No video
16
  "-acodec", "libmp3lame", # Use the mp3 codec
17
  "-q:a", "2", # Set quality to ensure good compression
18
- tmp_mp3_path # Output file path
19
  ]
20
 
21
  # Run the ffmpeg command and capture any errors
@@ -28,15 +38,18 @@ def convert_webm_to_mp3(webm_file):
28
  return "Error in conversion. Please check the file format and try again."
29
 
30
  # Return the path to the .mp3 file if successful
31
- return tmp_mp3_path
32
 
33
  # Gradio interface
34
  iface = gr.Interface(
35
  fn=convert_webm_to_mp3,
36
- inputs=gr.File(type="filepath", label="Upload .webm file"),
 
 
 
37
  outputs=gr.File(label="Converted .mp3 file"),
38
  title="WebM to MP3 Converter",
39
- description="Upload a .webm audio file, and this tool will convert it to .mp3 format."
40
  )
41
 
42
  # Launch the interface
 
 
1
  import gradio as gr
2
  import tempfile
3
  import subprocess
4
+ import os
5
+ import datetime
6
 
7
+ def convert_webm_to_mp3(webm_file, output_filename=None):
8
+ # Get current date
9
+ current_date = datetime.datetime.now().strftime("%Y-%m-%d")
10
+ if not output_filename:
11
+ output_filename = f"Morning_and_{current_date}.mp3"
12
+ else:
13
+ # Ensure the filename ends with .mp3
14
+ if not output_filename.endswith(".mp3"):
15
+ output_filename += ".mp3"
16
+
17
+ # Create a temporary directory to store the output file
18
+ temp_dir = tempfile.mkdtemp()
19
+ output_path = os.path.join(temp_dir, output_filename)
20
 
21
  # Run ffmpeg command directly for conversion with error handling
22
  ffmpeg_command = [
 
25
  "-vn", # No video
26
  "-acodec", "libmp3lame", # Use the mp3 codec
27
  "-q:a", "2", # Set quality to ensure good compression
28
+ output_path # Output file path
29
  ]
30
 
31
  # Run the ffmpeg command and capture any errors
 
38
  return "Error in conversion. Please check the file format and try again."
39
 
40
  # Return the path to the .mp3 file if successful
41
+ return output_path
42
 
43
  # Gradio interface
44
  iface = gr.Interface(
45
  fn=convert_webm_to_mp3,
46
+ inputs=[
47
+ gr.File(type="filepath", label="Upload .webm file"),
48
+ gr.Textbox(lines=1, placeholder="Enter output filename (optional)", label="Output Filename")
49
+ ],
50
  outputs=gr.File(label="Converted .mp3 file"),
51
  title="WebM to MP3 Converter",
52
+ description="Upload a .webm audio file, and this tool will convert it to .mp3 format. You can set a custom filename for the output."
53
  )
54
 
55
  # Launch the interface