Shinekoko commited on
Commit
9ff806b
Β·
verified Β·
1 Parent(s): 312f1fd

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +29 -25
utils.py CHANGED
@@ -141,10 +141,9 @@ def analyze_script_with_ai(video_path):
141
  if os.path.exists(audio_path): os.remove(audio_path)
142
  return f"Error: {str(e)}"
143
 
144
- # --- 4. VIDEO PROCESSING (UPDATED WITH AUTO SUBTITLES) ---
145
  async def generate_voice(text, output_file, voice, srt_file=None):
146
  communicate = edge_tts.Communicate(text, voice)
147
- # စာတန်းထိုးဖို့ α€œα€­α€―α€‘α€•α€Ία€›α€„α€Ί α€‘α€žα€Άα€”α€²α€·α€‘α€α€° SRT ပါ α€α€…α€Ία€α€«α€α€Šα€Ία€Έ α€‘α€―α€α€Ία€•α€±α€Έα€™α€Šα€Ί
148
  if srt_file:
149
  try:
150
  submaker = edge_tts.SubMaker()
@@ -154,11 +153,18 @@ async def generate_voice(text, output_file, voice, srt_file=None):
154
  file.write(chunk["data"])
155
  elif chunk["type"] == "WordBoundary":
156
  submaker.create_sub((chunk["offset"], chunk["duration"]), chunk["text"])
 
 
 
 
 
 
 
157
  with open(srt_file, "w", encoding="utf-8") as file:
158
- file.write(submaker.generate_subs())
159
  except Exception as e:
160
  print(f"⚠️ Auto Subtitle Generation Error: {e}")
161
- await communicate.save(output_file) # Error တက်ရင် α€›α€­α€―α€Έα€›α€­α€―α€Έα€•α€²α€žα€­α€™α€Ία€Έα€™α€Šα€Ί
162
  else:
163
  await communicate.save(output_file)
164
 
@@ -203,8 +209,6 @@ def process_video_edit(input_path, output_path, options):
203
  total_plays = math.ceil(target_duration / duration)
204
  loop_count = total_plays - 1
205
 
206
- print(f"πŸ’° Loop Calculation: Src={duration}s, Need={target_duration}s -> Plays={total_plays}, FFmpeg_Loop={loop_count}")
207
-
208
  if loop_count > 0:
209
  v = v.filter('loop', loop=loop_count, size=32767)
210
  a = a.filter('aloop', loop=loop_count, size=2147483647)
@@ -253,34 +257,34 @@ def process_video_edit(input_path, output_path, options):
253
  ty = int(options.get('text_y', 10))
254
 
255
  v = v.drawtext(
256
- text=txt_content,
257
- x=tx,
258
- y=ty,
259
- fontsize=35,
260
- fontcolor='white',
261
- borderw=2,
262
- bordercolor='black',
263
- shadowx=2,
264
- shadowy=2,
265
- shadowcolor='black@0.5'
266
  )
267
- except Exception as e:
268
- print(f"⚠️ Text Watermark Error: {e}")
269
 
270
- # --- AUTO SUBTITLES ---
271
  if options.get('auto_subtitles') and options.get('srt_path') and os.path.exists(options['srt_path']):
272
  try:
273
- # Format file path for FFmpeg
274
- srt_p = options['srt_path'].replace('\\', '/').replace(':', '\\:')
275
- # Subtitle Style (α€‘α€α€«α€›α€±α€¬α€„α€ΊαŠ α€‘α€”α€¬α€Έα€€α€½α€•α€Ία€‘α€™α€Šα€Ία€ΈαŠ α€‘α€œα€šα€Ία€α€Šα€·α€Ία€α€Šα€·α€Ί)
276
- style = "FontSize=20,PrimaryColour=&H0000FFFF,OutlineColour=&H00000000,BorderStyle=1,Outline=2,Alignment=2,MarginV=25"
277
- v = v.filter('subtitles', srt_p, force_style=style)
 
 
 
 
278
  except Exception as e:
279
- print(f"⚠️ Subtitle Filter Error: {e}")
280
 
281
  output = ffmpeg.output(v, a, output_path, vcodec='libx264', acodec='aac', preset='veryfast', shortest=None)
282
  output.run(overwrite_output=True, quiet=True)
283
  return True, "Success"
284
 
 
 
 
 
 
285
  except Exception as e:
286
  return False, str(e)
 
141
  if os.path.exists(audio_path): os.remove(audio_path)
142
  return f"Error: {str(e)}"
143
 
144
+ # --- 4. VIDEO PROCESSING (ULTRA SAFE AUTO SUBTITLES) ---
145
  async def generate_voice(text, output_file, voice, srt_file=None):
146
  communicate = edge_tts.Communicate(text, voice)
 
147
  if srt_file:
148
  try:
149
  submaker = edge_tts.SubMaker()
 
153
  file.write(chunk["data"])
154
  elif chunk["type"] == "WordBoundary":
155
  submaker.create_sub((chunk["offset"], chunk["duration"]), chunk["text"])
156
+
157
+ # Write SRT file
158
+ subs = submaker.generate_subs()
159
+ # α€‘α€€α€šα€Ία AI α€€ α€‘α€žα€Άα€‘α€½α€€α€Ία€•α€±α€™α€šα€·α€Ί α€…α€¬α€α€”α€Ία€Έα€™α€‘α€½α€€α€Ία€œα€¬α€α€²α€·α€›α€„α€Ί Error မတက်ထောင် Dummy α€…α€¬α€α€”α€Ία€Έα€‘α€Šα€·α€Ία€™α€Šα€Ί
160
+ if not subs.strip():
161
+ subs = "1\n00:00:00,000 --> 00:00:02,000\n[Audio]\n"
162
+
163
  with open(srt_file, "w", encoding="utf-8") as file:
164
+ file.write(subs)
165
  except Exception as e:
166
  print(f"⚠️ Auto Subtitle Generation Error: {e}")
167
+ await communicate.save(output_file) # Error တက်ခဲ့ရင် α€›α€­α€―α€Έα€›α€­α€―α€Έα€‘α€žα€Άα€•α€² α€žα€­α€™α€Ία€Έα€™α€Šα€Ί
168
  else:
169
  await communicate.save(output_file)
170
 
 
209
  total_plays = math.ceil(target_duration / duration)
210
  loop_count = total_plays - 1
211
 
 
 
212
  if loop_count > 0:
213
  v = v.filter('loop', loop=loop_count, size=32767)
214
  a = a.filter('aloop', loop=loop_count, size=2147483647)
 
257
  ty = int(options.get('text_y', 10))
258
 
259
  v = v.drawtext(
260
+ text=txt_content, x=tx, y=ty, fontsize=35, fontcolor='white',
261
+ borderw=2, bordercolor='black', shadowx=2, shadowy=2, shadowcolor='black@0.5'
 
 
 
 
 
 
 
 
262
  )
263
+ except: pass
 
264
 
265
+ # --- AUTO SUBTITLES (FIXED PATH & SAFETY) ---
266
  if options.get('auto_subtitles') and options.get('srt_path') and os.path.exists(options['srt_path']):
267
  try:
268
+ # FFmpeg တွင် Error မတက်စေရန် Relative Path α€•α€Όα€±α€¬α€„α€Ία€Έα€žα€―α€Άα€Έα€α€Όα€„α€Ία€Έ
269
+ srt_p = os.path.relpath(options['srt_path']).replace('\\', '/')
270
+
271
+ if os.path.getsize(options['srt_path']) > 0:
272
+ # α€‘α€α€«α€›α€±α€¬α€„α€ΊαŠ α€‘α€”α€¬α€Έα€€α€½α€•α€Ία€‘α€™α€Šα€Ία€ΈαŠ α€‘α€œα€šα€Ία€α€Šα€·α€Ία€α€Šα€·α€Ί
273
+ style = "FontSize=24,PrimaryColour=&H0000FFFF,OutlineColour=&H00000000,BorderStyle=1,Outline=2,Alignment=2,MarginV=25"
274
+ v = v.filter('subtitles', srt_p, force_style=style)
275
+ else:
276
+ print("⚠️ SRT File is empty, skipping subtitle burning.")
277
  except Exception as e:
278
+ print(f"⚠️ Subtitle Filter Application Error: {e}")
279
 
280
  output = ffmpeg.output(v, a, output_path, vcodec='libx264', acodec='aac', preset='veryfast', shortest=None)
281
  output.run(overwrite_output=True, quiet=True)
282
  return True, "Success"
283
 
284
+ except ffmpeg.Error as e:
285
+ # Error α€‘α€α€­α€‘α€€α€»α€žα€­α€›α€‘α€±α€¬α€„α€Ί Log ဖမ်းခြင်း
286
+ err_msg = e.stderr.decode('utf8') if e.stderr else str(e)
287
+ print(f"❌ FFmpeg Critical Error:\n{err_msg}")
288
+ return False, f"FFmpeg Render Error. (Check Terminal for details)"
289
  except Exception as e:
290
  return False, str(e)