kevinwang676 commited on
Commit
1b7bfce
·
verified ·
1 Parent(s): d5491ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -217,28 +217,24 @@ def trim_audio(intervals, input_file_path, output_file_path):
217
  # export the segment to a file
218
  segment.export(output_file_path_i, format='wav')
219
 
220
- import re
221
-
222
- def merge_audios(input_dir):
223
  output_file = "AI配音版.wav"
224
- # List all .wav files in the directory
225
- files = [f for f in os.listdir(input_dir) if f.endswith('.wav')]
226
-
227
- # Sort files based on the numerical order extracted from their names
228
- sorted_files = sorted(files, key=lambda x: int(re.search(r'(\d+)', x).group()))
229
-
230
  # Initialize an empty audio segment
231
- combined = AudioSegment.empty()
232
-
233
- # Loop through the sorted list and concatenate them
234
  for file in sorted_files:
235
- path = os.path.join(input_dir, file)
236
- audio = AudioSegment.from_wav(path)
237
- combined += audio
238
  print(f"Merged: {file}")
239
-
240
- # Export the combined audio
241
- combined.export(output_file, format="wav")
242
  return "AI配音版.wav"
243
 
244
  import shutil
 
217
  # export the segment to a file
218
  segment.export(output_file_path_i, format='wav')
219
 
220
+ def merge_audios(folder_path):
 
 
221
  output_file = "AI配音版.wav"
222
+ # Get all WAV files in the folder
223
+ files = [f for f in os.listdir(folder_path) if f.endswith('.wav')]
224
+ # Sort files based on the last digit in their names
225
+ sorted_files = sorted(files, key=lambda x: int(x.split()[-1].split('.')[0][-1]))
226
+
 
227
  # Initialize an empty audio segment
228
+ merged_audio = AudioSegment.empty()
229
+
230
+ # Loop through each file, in order, and concatenate them
231
  for file in sorted_files:
232
+ audio = AudioSegment.from_wav(os.path.join(folder_path, file))
233
+ merged_audio += audio
 
234
  print(f"Merged: {file}")
235
+
236
+ # Export the merged audio to a new file
237
+ merged_audio.export(output_file, format="wav")
238
  return "AI配音版.wav"
239
 
240
  import shutil