staraks commited on
Commit
e166883
·
verified ·
1 Parent(s): cbbc496

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -5
app.py CHANGED
@@ -26,26 +26,77 @@ def save_as_word(text, filename="merged_transcripts.docx"):
26
  def convert_to_wav_if_needed(input_path):
27
  """
28
  If the input file is not WAV, try to convert it to WAV using pydub/ffmpeg.
 
 
 
29
  Returns path to WAV file (may be same as input if already WAV).
 
30
  """
 
 
31
  lower = input_path.lower()
32
  if lower.endswith('.wav'):
33
  return input_path
34
 
35
- # create a temp wav file
36
  tmp_wav = tempfile.NamedTemporaryFile(suffix='.wav', delete=False)
37
  tmp_wav.close()
38
  try:
39
- # pydub will use ffmpeg under the hood
40
  AudioSegment.from_file(input_path).export(tmp_wav.name, format='wav')
41
  return tmp_wav.name
42
- except Exception as e:
43
- # cleanup if conversion failed
44
  try:
45
  os.unlink(tmp_wav.name)
46
  except Exception:
47
  pass
48
- raise e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
 
51
  def transcribe_multiple(file_paths, model_name, advanced, merge_checkbox, zip_file=None, zip_password=None):
 
26
  def convert_to_wav_if_needed(input_path):
27
  """
28
  If the input file is not WAV, try to convert it to WAV using pydub/ffmpeg.
29
+ First attempts pydub (ffmpeg via AudioSegment). If that fails (common for obscure
30
+ .dct variants), it falls back to trying several ffmpeg raw-format heuristics
31
+ (different sample rates, signed/unsigned 8/16-bit) until one succeeds.
32
  Returns path to WAV file (may be same as input if already WAV).
33
+ Raises an exception with ffmpeg stderr if no conversion worked.
34
  """
35
+ import subprocess
36
+
37
  lower = input_path.lower()
38
  if lower.endswith('.wav'):
39
  return input_path
40
 
41
+ # 1) Try pydub automatic detection first (simplest)
42
  tmp_wav = tempfile.NamedTemporaryFile(suffix='.wav', delete=False)
43
  tmp_wav.close()
44
  try:
 
45
  AudioSegment.from_file(input_path).export(tmp_wav.name, format='wav')
46
  return tmp_wav.name
47
+ except Exception as e_auto:
48
+ # Clean up the partial file
49
  try:
50
  os.unlink(tmp_wav.name)
51
  except Exception:
52
  pass
53
+ # We'll try a set of ffmpeg heuristics below
54
+ ffmpeg_errors = []
55
+
56
+ # 2) Fallback: try various raw-format guesses with ffmpeg
57
+ guesses = [
58
+ # fmt, sample_rate, channels
59
+ ('s16le', 16000, 1),
60
+ ('s16le', 8000, 1),
61
+ ('u8', 8000, 1),
62
+ ('s16le', 44100, 1),
63
+ ('s16le', 16000, 2),
64
+ ('adpcm_ima_wav', 8000, 1),
65
+ ]
66
+
67
+ for fmt, sr, ch in guesses:
68
+ tmp_wav = tempfile.NamedTemporaryFile(suffix='.wav', delete=False)
69
+ tmp_wav.close()
70
+ cmd = [
71
+ 'ffmpeg', '-y', '-f', fmt, '-ar', str(sr), '-ac', str(ch), '-i', input_path,
72
+ tmp_wav.name
73
+ ]
74
+ try:
75
+ proc = subprocess.run(cmd, capture_output=True, text=True, timeout=60)
76
+ except Exception as e_run:
77
+ ffmpeg_errors.append(f"ffmpeg run failed for fmt={fmt},sr={sr},ch={ch}: {e_run}")
78
+ try:
79
+ os.unlink(tmp_wav.name)
80
+ except Exception:
81
+ pass
82
+ continue
83
+
84
+ if proc.returncode == 0 and os.path.exists(tmp_wav.name) and os.path.getsize(tmp_wav.name) > 100:
85
+ # success
86
+ return tmp_wav.name
87
+ else:
88
+ err = proc.stderr or proc.stdout or 'no ffmpeg output'
89
+ ffmpeg_errors.append(f"fmt={fmt},sr={sr},ch={ch} -> rc={proc.returncode} -> {err}")
90
+ try:
91
+ os.unlink(tmp_wav.name)
92
+ except Exception:
93
+ pass
94
+
95
+ # If we get here, nothing worked. Provide the accumulated errors.
96
+ msg = "Could not convert file to WAV. Tried pydub and several ffmpeg heuristics. Errors:
97
+ " + "
98
+ ".join(ffmpeg_errors)
99
+ raise Exception(msg)
100
 
101
 
102
  def transcribe_multiple(file_paths, model_name, advanced, merge_checkbox, zip_file=None, zip_password=None):