deveix commited on
Commit
998e0cb
·
1 Parent(s): f40f389

add repair

Browse files
Files changed (2) hide show
  1. app/main.py +20 -1
  2. requirements.txt +2 -1
app/main.py CHANGED
@@ -19,6 +19,7 @@ import librosa.display
19
  import soundfile as sf
20
  import opensmile
21
 
 
22
 
23
  load_dotenv()
24
 
@@ -237,6 +238,24 @@ def extract_features(file_path):
237
  all_data = pd.DataFrame([features_reshaped])
238
  return all_data
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
 
241
  @app.post("/mlp")
242
  async def handle_audio(file: UploadFile = File(...)):
@@ -259,7 +278,7 @@ async def handle_audio(file: UploadFile = File(...)):
259
  f.write(contents)
260
 
261
  preprocess_audio(temp_filename, 'app')
262
-
263
  # Here you would add the feature extraction logic
264
  features = extract_features(temp_filename)
265
  print("Extracted Features:", features)
 
19
  import soundfile as sf
20
  import opensmile
21
 
22
+ import ffmpeg
23
 
24
  load_dotenv()
25
 
 
238
  all_data = pd.DataFrame([features_reshaped])
239
  return all_data
240
 
241
+ def repair_mp3_with_ffmpeg_python(input_path, output_path):
242
+ """Attempt to repair an MP3 file using FFmpeg."""
243
+ try:
244
+ # Define the audio stream with the necessary conversion parameters
245
+ audio = (
246
+ ffmpeg
247
+ .input(input_path, nostdin=None, y=None)
248
+ .output(output_path, vn=None, acodec='libmp3lame', ar='44100', ac='1', b='192k', af='aresample=44100')
249
+ .global_args('-nostdin', '-y') # Applying global arguments
250
+ .overwrite_output()
251
+ )
252
+
253
+ # Execute the FFmpeg command
254
+ ffmpeg.run(audio)
255
+ print(f"File repaired and saved as {output_path}")
256
+ except ffmpeg.Error as e:
257
+ print(f"Failed to repair file {input_path}: {str(e.stderr)}")
258
+
259
 
260
  @app.post("/mlp")
261
  async def handle_audio(file: UploadFile = File(...)):
 
278
  f.write(contents)
279
 
280
  preprocess_audio(temp_filename, 'app')
281
+ repair_mp3_with_ffmpeg_python(temp_filename, temp_filename)
282
  # Here you would add the feature extraction logic
283
  features = extract_features(temp_filename)
284
  print("Extracted Features:", features)
requirements.txt CHANGED
@@ -16,4 +16,5 @@ soundfile
16
  opensmile
17
  eyeD3
18
  matplotlib
19
- python-multipart
 
 
16
  opensmile
17
  eyeD3
18
  matplotlib
19
+ python-multipart
20
+ ffmpeg-python