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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -27
app.py CHANGED
@@ -1,8 +1,9 @@
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.
@@ -13,51 +14,48 @@ def split_audio_ffmpeg(input_audio, output_directory, chunk_duration=30):
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()
 
1
  import gradio as gr
2
  import os
3
  import ffmpeg
4
+ import shutil
5
 
6
+ # Function to split the audio file using FFmpeg
7
  def split_audio_ffmpeg(input_audio, output_directory, chunk_duration=30):
8
  """
9
  Split an audio file into smaller chunks using FFmpeg.
 
14
  os.makedirs(output_directory, exist_ok=True)
15
 
16
  try:
17
+ # Get the total duration of the audio file using ffmpeg.probe()
18
+ probe = ffmpeg.probe(input_audio.name, 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 the file into chunks
23
  chunks = []
24
  for i in range(0, int(duration), chunk_duration):
25
  chunk_file = os.path.join(output_directory, f"chunk_{i // chunk_duration + 1}.mp3")
26
+ ffmpeg.input(input_audio.name, ss=i, t=chunk_duration).output(chunk_file).run(quiet=True, overwrite_output=True)
 
 
27
  chunks.append(chunk_file)
28
+ print(f"Generated: {chunk_file}")
29
+
30
+ # Create a zip archive of the chunks
31
+ zip_filename = '/tmp/audio_chunks.zip'
32
+ shutil.make_archive(zip_filename.replace('.zip', ''), 'zip', output_directory)
33
+ return zip_filename
34
 
35
  except Exception as e:
36
+ print(f"Error during split: {e}")
37
+ return f"Error during file processing: {e}"
 
38
 
39
  # Gradio interface function
40
  def process_audio(input_audio):
41
+ output_directory = "/tmp/output_chunks"
42
+ zip_file = split_audio_ffmpeg(input_audio, output_directory, chunk_duration=30)
43
+
44
+ # Check if zip creation was successful
45
+ if "Error" in zip_file:
46
+ return zip_file
47
 
48
+ return gr.File(zip_file)
 
 
 
 
 
49
 
50
  # Gradio interface setup
51
  interface = gr.Interface(
52
  fn=process_audio,
53
  inputs=gr.File(label="Upload Audio", file_types=["audio"]),
54
+ outputs=gr.File(label="Download Chunks as Zip"),
55
  title="Audio Splitter",
56
+ description="Upload an audio file to split it into smaller chunks of 30 seconds each and download as a zip.",
57
  )
58
 
59
+ # Launch the Gradio interface
60
  if __name__ == "__main__":
61
  interface.launch()