Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
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 (
|
| 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(
|
| 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 |
-
|
| 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
|
| 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 |
-
#
|
| 274 |
-
srt_p = options['srt_path'].replace('\\', '/')
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|