mobenta commited on
Commit
30d2e4e
·
verified ·
1 Parent(s): 2c84fff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -1,13 +1,14 @@
1
  import gradio as gr
2
  from pydub import AudioSegment
3
 
4
- def compress_audio(input_audio_path, compression_rate=128):
5
  """
6
  Compress an audio file.
7
 
8
  Args:
9
  input_audio_path: Path to the input audio file.
10
  compression_rate: Desired bitrate for compression (in kbps).
 
11
 
12
  Returns:
13
  Compressed audio file path.
@@ -16,13 +17,14 @@ def compress_audio(input_audio_path, compression_rate=128):
16
  # Load the audio file using pydub
17
  audio = AudioSegment.from_file(input_audio_path)
18
 
19
- # Temporary output path
20
- output_path = "compressed_audio.mp3"
 
21
 
22
  # Export the compressed audio
23
- audio.export(output_path, format="mp3", bitrate=f"{compression_rate}k")
24
 
25
- return output_path
26
  except Exception as e:
27
  return str(e)
28
 
@@ -32,10 +34,11 @@ interface = gr.Interface(
32
  inputs=[
33
  gr.Audio(label="Upload Audio File", type="filepath"),
34
  gr.Slider(32, 320, step=16, label="Compression Rate (kbps)", value=128),
 
35
  ],
36
  outputs=gr.File(label="Download Compressed Audio"),
37
  title="Audio Compressor",
38
- description="Upload an audio file and select the compression rate to reduce the file size.",
39
  allow_flagging="never",
40
  )
41
 
 
1
  import gradio as gr
2
  from pydub import AudioSegment
3
 
4
+ def compress_audio(input_audio_path, compression_rate=128, output_name="compressed_audio.mp3"):
5
  """
6
  Compress an audio file.
7
 
8
  Args:
9
  input_audio_path: Path to the input audio file.
10
  compression_rate: Desired bitrate for compression (in kbps).
11
+ output_name: Desired name for the compressed file.
12
 
13
  Returns:
14
  Compressed audio file path.
 
17
  # Load the audio file using pydub
18
  audio = AudioSegment.from_file(input_audio_path)
19
 
20
+ # Ensure output name has the .mp3 extension
21
+ if not output_name.endswith(".mp3"):
22
+ output_name += ".mp3"
23
 
24
  # Export the compressed audio
25
+ audio.export(output_name, format="mp3", bitrate=f"{compression_rate}k")
26
 
27
+ return output_name
28
  except Exception as e:
29
  return str(e)
30
 
 
34
  inputs=[
35
  gr.Audio(label="Upload Audio File", type="filepath"),
36
  gr.Slider(32, 320, step=16, label="Compression Rate (kbps)", value=128),
37
+ gr.Textbox(label="Output File Name", placeholder="compressed_audio.mp3"),
38
  ],
39
  outputs=gr.File(label="Download Compressed Audio"),
40
  title="Audio Compressor",
41
+ description="Upload an audio file, set the compression rate, and specify a name for the compressed file.",
42
  allow_flagging="never",
43
  )
44