phamhapa101 commited on
Commit
ea339b3
·
verified ·
1 Parent(s): 618b106

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -109,7 +109,7 @@ def process_single_line(index, text, start_sec, end_sec, provider, v_code, nativ
109
  srt_duration = end_sec - start_sec
110
  if srt_duration <= 0: srt_duration = 0.5
111
 
112
- # 1. PHÂN LUỒNG & GỌI API
113
  if provider == "Microsoft Edge (Neural Cao Cấp)":
114
  base_boost = 15
115
  user_boost = int((speed_m - 1.0) * 100)
@@ -122,16 +122,28 @@ def process_single_line(index, text, start_sec, end_sec, provider, v_code, nativ
122
  if audio_chunk is None:
123
  return index, None, None, None
124
 
125
- # 2. XÉN KHOẢNG LẶNG (Có vòng bảo vệ)
126
  audio_chunk_trimmed, _ = librosa.effects.trim(audio_chunk, top_db=35)
127
- # Chỉ trim nếu file sau khi trim còn đủ dài (> 0.2s) để tránh lỗi thư viện sập
 
128
  if len(audio_chunk_trimmed) > native_sr * 0.2:
129
  audio_chunk = audio_chunk_trimmed
130
 
 
 
 
 
 
 
 
 
 
131
  audio_chunk = safe_normalize(audio_chunk, target_peak=0.90)
 
 
132
  orig_dur = len(audio_chunk) / native_sr
133
 
134
- # 3. TÍNH TOÁN BÙ TRỪ THỜI GIAN
135
  if provider == "Microsoft Edge (Neural Cao Cấp)":
136
  # Tốc độ đã được Microsoft ép trên server, ta chỉ ép thêm nếu nó vẫn dài hơn SRT
137
  stretch_factor = orig_dur / srt_duration
@@ -140,16 +152,16 @@ def process_single_line(index, text, start_sec, end_sec, provider, v_code, nativ
140
  # TikTok không ép tốc độ trên server, ta phải ép tay
141
  stretch_factor = max(speed_m, orig_dur / srt_duration)
142
 
143
- # 4. ÉP THỜI GIAN/CAO ĐỘ AN TOÀN
144
  if abs(stretch_factor - 1.0) > 0.01 or pitch_steps != 0:
145
  try:
146
  processed_chunk = pyrb.time_stretch(audio_chunk, native_sr, stretch_factor)
147
  if pitch_steps != 0:
148
  processed_chunk = pyrb.pitch_shift(processed_chunk, native_sr, pitch_steps)
149
  except Exception as e:
150
- # Nếu pyrubberband sập (crash), hoàn tác dùng lại file gốc chứ không để bị khoảng trắng
151
  processed_chunk = audio_chunk
152
- print(f"Bỏ qua ép tốc độ dòng {index} do file quá ngắn hoặc lỗi: {e}")
153
  else:
154
  processed_chunk = audio_chunk
155
 
 
109
  srt_duration = end_sec - start_sec
110
  if srt_duration <= 0: srt_duration = 0.5
111
 
112
+ # 1. PHÂN LUỒNG & GỌI API (Edge tự buff thêm tốc độ)
113
  if provider == "Microsoft Edge (Neural Cao Cấp)":
114
  base_boost = 15
115
  user_boost = int((speed_m - 1.0) * 100)
 
122
  if audio_chunk is None:
123
  return index, None, None, None
124
 
125
+ # 2. XÉN KHOẢNG LẶNG BƠM "ĐỆM KHÍ" (BUFFER) 100ms
126
  audio_chunk_trimmed, _ = librosa.effects.trim(audio_chunk, top_db=35)
127
+
128
+ # Chỉ xử lý nếu file đủ dài (> 0.2s)
129
  if len(audio_chunk_trimmed) > native_sr * 0.2:
130
  audio_chunk = audio_chunk_trimmed
131
 
132
+ # Tạo 100 mili-giây (0.1 giây) âm thanh trống tuyệt đối
133
+ silence_samples = int(0.1 * native_sr)
134
+
135
+ # Chia đôi 100ms ra: 50ms gắn vào đầu, 50ms gắn vào cuối
136
+ half_silence = np.zeros(silence_samples // 2, dtype=np.float32)
137
+
138
+ # Ghép nối an toàn
139
+ audio_chunk = np.concatenate((half_silence, audio_chunk, half_silence))
140
+
141
  audio_chunk = safe_normalize(audio_chunk, target_peak=0.90)
142
+
143
+ # Tính lại tổng chiều dài sau khi đã bơm 100ms đệm khí
144
  orig_dur = len(audio_chunk) / native_sr
145
 
146
+ # 3. TÍNH TOÁN BÙ TRỪ THỜI GIAN THEO SRT
147
  if provider == "Microsoft Edge (Neural Cao Cấp)":
148
  # Tốc độ đã được Microsoft ép trên server, ta chỉ ép thêm nếu nó vẫn dài hơn SRT
149
  stretch_factor = orig_dur / srt_duration
 
152
  # TikTok không ép tốc độ trên server, ta phải ép tay
153
  stretch_factor = max(speed_m, orig_dur / srt_duration)
154
 
155
+ # 4. ÉP THỜI GIAN/CAO ĐỘ AN TOÀN (CÓ VÒNG BẢO VỆ CHỐNG CRASH)
156
  if abs(stretch_factor - 1.0) > 0.01 or pitch_steps != 0:
157
  try:
158
  processed_chunk = pyrb.time_stretch(audio_chunk, native_sr, stretch_factor)
159
  if pitch_steps != 0:
160
  processed_chunk = pyrb.pitch_shift(processed_chunk, native_sr, pitch_steps)
161
  except Exception as e:
162
+ # Nếu pyrubberband sập, hoàn tác dùng lại file gốc đệm 100ms để không bị mất câu
163
  processed_chunk = audio_chunk
164
+ print(f"Bỏ qua ép tốc độ dòng {index} do file quá ngắn hoặc lỗi pyrubberband: {e}")
165
  else:
166
  processed_chunk = audio_chunk
167