mehdilaalali commited on
Commit
e5362b5
Β·
verified Β·
1 Parent(s): 195eb72

fix(core): migrate extraction to PCM-WAV to bypass libmp3lame missing codec and enforce 25s limit on cloning endpoints

Browse files
Files changed (1) hide show
  1. core.py +5 -3
core.py CHANGED
@@ -16,9 +16,9 @@ def get_client():
16
  # ─── Utility ──────────────────────────────────────────────────────────────────
17
  def trim_audio_if_needed(audio_path, max_seconds=25):
18
  """Trims audio to max_seconds using ffmpeg."""
19
- out_path = tempfile.mktemp(suffix=".mp3")
20
  try:
21
- subprocess.run(["ffmpeg", "-y", "-i", audio_path, "-t", str(max_seconds), "-acodec", "libmp3lame", out_path], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
22
  return out_path
23
  except Exception as e:
24
  print(f"Warning: Failed to trim audio, returning original: {e}")
@@ -150,12 +150,14 @@ def clone_voice(audio_path, url_input, voice_name, gender, languages_str):
150
  'preferredquality': '128',
151
  }],
152
  'postprocessor_args': [
153
- '-t', '60' # Limit to first 60 seconds
154
  ],
155
  }
156
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
157
  info = ydl.extract_info(url, download=True)
158
  final_audio_path = base_out + '.mp3'
 
 
159
 
160
  client = get_client()
161
  sample_b64 = base64.b64encode(Path(final_audio_path).read_bytes()).decode()
 
16
  # ─── Utility ──────────────────────────────────────────────────────────────────
17
  def trim_audio_if_needed(audio_path, max_seconds=25):
18
  """Trims audio to max_seconds using ffmpeg."""
19
+ out_path = tempfile.mktemp(suffix=".wav")
20
  try:
21
+ subprocess.run(["ffmpeg", "-y", "-i", audio_path, "-t", str(max_seconds), "-c:a", "pcm_s16le", out_path], check=True)
22
  return out_path
23
  except Exception as e:
24
  print(f"Warning: Failed to trim audio, returning original: {e}")
 
150
  'preferredquality': '128',
151
  }],
152
  'postprocessor_args': [
153
+ '-t', '25' # Hard Limit to 25 seconds to bypass API 30s limit
154
  ],
155
  }
156
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
157
  info = ydl.extract_info(url, download=True)
158
  final_audio_path = base_out + '.mp3'
159
+ # Ensure any direct MP3 or uploaded file is ALSO strictly trimmed
160
+ final_audio_path = trim_audio_if_needed(final_audio_path, max_seconds=25)
161
 
162
  client = get_client()
163
  sample_b64 = base64.b64encode(Path(final_audio_path).read_bytes()).decode()