Ahmadkhan12 commited on
Commit
0d0e94b
1 Parent(s): 0f625b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -29
app.py CHANGED
@@ -1,50 +1,63 @@
 
1
  import os
2
  import ffmpeg
3
- import gradio as gr
4
 
5
- def split_audio_ffmpeg(input_audio, chunk_duration=30):
 
6
  """
7
- Splits an audio file into smaller chunks using FFmpeg.
8
- :param input_audio: The uploaded audio file
 
9
  :param chunk_duration: Duration of each chunk in seconds
10
  """
11
- # Define input and output paths
12
- input_audio_path = input_audio.name
13
- output_directory = "./output_chunks"
14
  os.makedirs(output_directory, exist_ok=True)
15
 
16
  try:
17
- # Get audio duration
18
- probe = ffmpeg.probe(input_audio_path, v='error', select_streams='a', show_entries='stream=duration')
19
- duration = float(probe['streams'][0]['duration'])
20
- print(f"Audio duration: {duration} seconds")
 
 
 
21
 
22
- # Split audio into chunks
 
23
  for i in range(0, int(duration), chunk_duration):
24
  chunk_file = os.path.join(output_directory, f"chunk_{i // chunk_duration + 1}.mp3")
25
- ffmpeg.input(input_audio_path, ss=i, t=chunk_duration).output(chunk_file).run(quiet=True)
26
- print(f"Generated: {chunk_file}")
 
 
 
 
27
 
28
- # Collect all chunk paths for download
29
- return [os.path.join(output_directory, f) for f in os.listdir(output_directory)]
30
-
31
  except Exception as e:
32
- print(f"Error during splitting: {e}")
33
- return []
 
34
 
35
- # Gradio interface
36
  def process_audio(input_audio):
37
- chunks = split_audio_ffmpeg(input_audio)
38
- if not chunks:
39
- return "An error occurred during processing."
40
- return chunks
 
 
 
 
 
41
 
42
- # Launch Gradio interface
43
  interface = gr.Interface(
44
  fn=process_audio,
45
- inputs=gr.File(label="Upload Audio File", file_types=["audio"]),
46
- outputs=gr.File(label="Download Chunks", file_count="multiple"),
47
- title="Audio Splitter"
 
48
  )
49
 
50
- interface.launch(share=True)
 
 
 
1
+ import gradio as gr
2
  import os
3
  import ffmpeg
 
4
 
5
+ # Function to validate and split audio
6
+ def split_audio_ffmpeg(input_audio, output_directory, chunk_duration=30):
7
  """
8
+ Split an audio file into smaller chunks using FFmpeg.
9
+ :param input_audio: Path to the input audio file
10
+ :param output_directory: Directory where output chunks will be saved
11
  :param chunk_duration: Duration of each chunk in seconds
12
  """
 
 
 
13
  os.makedirs(output_directory, exist_ok=True)
14
 
15
  try:
16
+ # Validate the file duration
17
+ probe = ffmpeg.probe(input_audio.name, select_streams="a", show_entries="stream=duration")
18
+ duration = float(probe["streams"][0]["duration"])
19
+ max_duration = 3600 # 1 hour limit for uploaded audio files
20
+
21
+ if duration > max_duration:
22
+ return f"Error: File duration exceeds {max_duration / 60} minutes. Please upload a shorter file."
23
 
24
+ # Split the file into chunks
25
+ chunks = []
26
  for i in range(0, int(duration), chunk_duration):
27
  chunk_file = os.path.join(output_directory, f"chunk_{i // chunk_duration + 1}.mp3")
28
+ ffmpeg.input(input_audio.name, ss=i, t=chunk_duration).output(chunk_file).run(
29
+ quiet=True, overwrite_output=True
30
+ )
31
+ chunks.append(chunk_file)
32
+
33
+ return chunks
34
 
 
 
 
35
  except Exception as e:
36
+ error_message = f"Error processing file: {str(e)}"
37
+ print(error_message)
38
+ return error_message
39
 
40
+ # Gradio interface function
41
  def process_audio(input_audio):
42
+ output_directory = "/tmp/audio_chunks"
43
+ result = split_audio_ffmpeg(input_audio, output_directory, chunk_duration=30)
44
+
45
+ if isinstance(result, str) and result.startswith("Error"):
46
+ return result
47
+ elif isinstance(result, list):
48
+ return [gr.File.update(value=chunk) for chunk in result]
49
+ else:
50
+ return "An unexpected error occurred. Please check the logs."
51
 
52
+ # Gradio interface setup
53
  interface = gr.Interface(
54
  fn=process_audio,
55
+ inputs=gr.File(label="Upload Audio", file_types=["audio"]),
56
+ outputs=gr.Files(label="Download Audio Chunks"),
57
+ title="Audio Splitter",
58
+ description="Upload an audio file to split it into smaller chunks of 30 seconds each.",
59
  )
60
 
61
+ # Launch the app
62
+ if __name__ == "__main__":
63
+ interface.launch()