Turbiling commited on
Commit
dadd64d
·
verified ·
1 Parent(s): 57bf8f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -70,21 +70,38 @@ def download_youtube_audio(youtube_url):
70
 
71
  def convert_to_wav(input_path):
72
  """
73
- Convert any audio/video to 16kHz mono WAV using ffmpeg CLI for reliability.
 
74
  Returns path to wav file.
75
  """
76
- p = Path(input_path)
77
- out_wav = str(p.with_suffix(".wav"))
 
78
  try:
79
- subprocess.run(
 
 
 
 
 
80
  ["ffmpeg", "-y", "-i", str(input_path), "-ar", "16000", "-ac", "1", out_wav],
81
- check=True,
82
- stdout=subprocess.DEVNULL,
83
- stderr=subprocess.DEVNULL,
84
  )
 
 
 
 
 
 
 
 
 
 
85
  return out_wav
 
86
  except Exception as e:
87
- raise RuntimeError(f"ffmpeg conversion failed: {e}")
88
 
89
  def split_audio_to_chunks(wav_path, max_ms=5*60*1000):
90
  """
 
70
 
71
  def convert_to_wav(input_path):
72
  """
73
+ Convert any audio/video to 16kHz mono WAV using ffmpeg.
74
+ Always creates a new temp wav file (never overwrites input).
75
  Returns path to wav file.
76
  """
77
+ import tempfile
78
+ from pydub import AudioSegment
79
+
80
  try:
81
+ tmp_wav = tempfile.NamedTemporaryFile(suffix=".wav", delete=False, prefix="conv_")
82
+ tmp_wav.close()
83
+ out_wav = tmp_wav.name
84
+
85
+ # Use ffmpeg CLI
86
+ result = subprocess.run(
87
  ["ffmpeg", "-y", "-i", str(input_path), "-ar", "16000", "-ac", "1", out_wav],
88
+ stdout=subprocess.PIPE,
89
+ stderr=subprocess.PIPE
 
90
  )
91
+
92
+ if result.returncode != 0 or not os.path.exists(out_wav):
93
+ print("⚠️ ffmpeg failed, trying pydub fallback...")
94
+ try:
95
+ audio = AudioSegment.from_file(input_path)
96
+ audio = audio.set_frame_rate(16000).set_channels(1)
97
+ audio.export(out_wav, format="wav")
98
+ except Exception as e:
99
+ raise RuntimeError(f"Both ffmpeg and pydub conversion failed: {e}")
100
+
101
  return out_wav
102
+
103
  except Exception as e:
104
+ raise RuntimeError(f"Conversion error: {e}")
105
 
106
  def split_audio_to_chunks(wav_path, max_ms=5*60*1000):
107
  """