Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,38 +16,32 @@ def step_1_extract_words(video_path, progress=gr.Progress()):
|
|
| 16 |
if not video_path:
|
| 17 |
return None, "الرجاء رفع فيديو أولاً."
|
| 18 |
|
| 19 |
-
progress(0, desc="جاري ت
|
| 20 |
segments, _ = model.transcribe(video_path, word_timestamps=True, language="ar")
|
| 21 |
|
| 22 |
words_data = []
|
| 23 |
-
progress(0.5, desc="جاري تحليل الصوت واستخراج الكلمات...")
|
| 24 |
-
|
| 25 |
for segment in segments:
|
| 26 |
for word in segment.words:
|
| 27 |
words_data.append([word.word.strip(), round(word.start, 2), round(word.end, 2)])
|
| 28 |
|
| 29 |
df = pd.DataFrame(words_data, columns=["الكلمة", "البداية", "النهاية"])
|
| 30 |
-
return df, "تم الاستخراج
|
| 31 |
|
| 32 |
def step_2_render_video(video_path, df_edited, font_selection, text_color, font_size, progress=gr.Progress()):
|
| 33 |
if video_path is None or df_edited is None or df_edited.empty:
|
| 34 |
return None, "بيانات ناقصة."
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
if
|
| 38 |
-
# تحويل بسيط لضمان قبول اللون
|
| 39 |
-
final_color = "yellow"
|
| 40 |
-
else:
|
| 41 |
-
final_color = text_color
|
| 42 |
|
| 43 |
-
output_path = "
|
| 44 |
video = VideoFileClip(video_path)
|
| 45 |
w, h = int(video.w), int(video.h)
|
| 46 |
clips = [video]
|
| 47 |
words_list = df_edited.values.tolist()
|
| 48 |
chunk_size = 3
|
| 49 |
|
| 50 |
-
progress(0.1, desc="جاري
|
| 51 |
|
| 52 |
for i in range(0, len(words_list), chunk_size):
|
| 53 |
current_chunk = words_list[i : i + chunk_size]
|
|
@@ -58,63 +52,59 @@ def step_2_render_video(video_path, df_edited, font_selection, text_color, font_
|
|
| 58 |
c_end = float(current_chunk[-1][2])
|
| 59 |
duration = max(0.1, c_end - c_start)
|
| 60 |
|
| 61 |
-
|
|
|
|
| 62 |
text=clean_sentence,
|
| 63 |
-
font_size=font_size,
|
| 64 |
-
color=
|
| 65 |
stroke_color='black',
|
| 66 |
-
stroke_width=
|
|
|
|
| 67 |
method='caption',
|
| 68 |
-
|
| 69 |
-
size=(int(w * 0.9), None),
|
| 70 |
text_align='center'
|
| 71 |
).with_start(c_start).with_duration(duration).with_position(('center', int(h * 0.65)))
|
| 72 |
|
| 73 |
-
clips.append(
|
| 74 |
|
| 75 |
final_video = CompositeVideoClip(clips, size=(w, h))
|
| 76 |
|
|
|
|
| 77 |
final_video.write_videofile(
|
| 78 |
output_path,
|
| 79 |
codec="libx264",
|
| 80 |
audio_codec="aac",
|
| 81 |
fps=video.fps,
|
| 82 |
-
logger='bar'
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
-
return output_path, "تم
|
| 86 |
|
| 87 |
-
# ---
|
| 88 |
-
with gr.Blocks(
|
| 89 |
-
gr.Markdown("# 🎬 Caption Pro
|
| 90 |
|
| 91 |
with gr.Row():
|
| 92 |
-
|
| 93 |
-
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
-
choices=["arialbd.ttf", "Cairo-Bold.ttf", "Almarai-Bold.ttf"
|
| 99 |
value="arialbd.ttf",
|
| 100 |
-
label="
|
| 101 |
)
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
size_slider = gr.Slider(minimum=30, maximum=120, value=70, step=5, label="حجم الخط")
|
| 105 |
|
| 106 |
-
|
| 107 |
-
table = gr.Dataframe(headers=["الكلمة", "البداية", "النهاية"],
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
btn_re = gr.Button("2. إنتاج الفيديو", variant="secondary")
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
step_2_render_video,
|
| 115 |
-
inputs=[video_in, table, font_dropdown, color_input, size_slider],
|
| 116 |
-
outputs=[video_out, status]
|
| 117 |
-
)
|
| 118 |
|
| 119 |
-
|
| 120 |
-
app.launch()
|
|
|
|
| 16 |
if not video_path:
|
| 17 |
return None, "الرجاء رفع فيديو أولاً."
|
| 18 |
|
| 19 |
+
progress(0, desc="جاري استخراج الكلمات...")
|
| 20 |
segments, _ = model.transcribe(video_path, word_timestamps=True, language="ar")
|
| 21 |
|
| 22 |
words_data = []
|
|
|
|
|
|
|
| 23 |
for segment in segments:
|
| 24 |
for word in segment.words:
|
| 25 |
words_data.append([word.word.strip(), round(word.start, 2), round(word.end, 2)])
|
| 26 |
|
| 27 |
df = pd.DataFrame(words_data, columns=["الكلمة", "البداية", "النهاية"])
|
| 28 |
+
return df, "تم الاستخراج!"
|
| 29 |
|
| 30 |
def step_2_render_video(video_path, df_edited, font_selection, text_color, font_size, progress=gr.Progress()):
|
| 31 |
if video_path is None or df_edited is None or df_edited.empty:
|
| 32 |
return None, "بيانات ناقصة."
|
| 33 |
|
| 34 |
+
# التأكد من مسار الخط: إذا لم يجد الملف المرفوع سيستخدم Arial الافتراضي
|
| 35 |
+
actual_font = font_selection if os.path.exists(font_selection) else "Arial-Bold"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
output_path = "output_final.mp4"
|
| 38 |
video = VideoFileClip(video_path)
|
| 39 |
w, h = int(video.w), int(video.h)
|
| 40 |
clips = [video]
|
| 41 |
words_list = df_edited.values.tolist()
|
| 42 |
chunk_size = 3
|
| 43 |
|
| 44 |
+
progress(0.1, desc="جاري معالجة النصوص بالإعدادات الجديدة...")
|
| 45 |
|
| 46 |
for i in range(0, len(words_list), chunk_size):
|
| 47 |
current_chunk = words_list[i : i + chunk_size]
|
|
|
|
| 52 |
c_end = float(current_chunk[-1][2])
|
| 53 |
duration = max(0.1, c_end - c_start)
|
| 54 |
|
| 55 |
+
# إنشاء الكليب مع التأكد من تمرير اللون والخط بشكل صريح
|
| 56 |
+
txt = TextClip(
|
| 57 |
text=clean_sentence,
|
| 58 |
+
font_size=int(font_size),
|
| 59 |
+
color=text_color, # سيأخذ اللون من ColorPicker مباشرة
|
| 60 |
stroke_color='black',
|
| 61 |
+
stroke_width=1.5,
|
| 62 |
+
font=actual_font,
|
| 63 |
method='caption',
|
| 64 |
+
size=(int(w * 0.85), None),
|
|
|
|
| 65 |
text_align='center'
|
| 66 |
).with_start(c_start).with_duration(duration).with_position(('center', int(h * 0.65)))
|
| 67 |
|
| 68 |
+
clips.append(txt)
|
| 69 |
|
| 70 |
final_video = CompositeVideoClip(clips, size=(w, h))
|
| 71 |
|
| 72 |
+
# تحسين سرعة المعالجة وتقليل المشاكل
|
| 73 |
final_video.write_videofile(
|
| 74 |
output_path,
|
| 75 |
codec="libx264",
|
| 76 |
audio_codec="aac",
|
| 77 |
fps=video.fps,
|
| 78 |
+
logger='bar',
|
| 79 |
+
threads=4 # استخدام تعدد الخيوط لتسريع العملية
|
| 80 |
)
|
| 81 |
|
| 82 |
+
return output_path, f"تم الحفظ! اللون: {text_color}، الخط: {actual_font}"
|
| 83 |
|
| 84 |
+
# --- الواجهة ---
|
| 85 |
+
with gr.Blocks() as app:
|
| 86 |
+
gr.Markdown("### 🎬 Caption Pro: تخصيص كامل")
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
+
v_in = gr.Video()
|
| 90 |
+
v_out = gr.Video()
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
+
# تأكد من رفع هذه الملفات في Hugging Face (مثلاً Cairo-Bold.ttf)
|
| 94 |
+
font_opt = gr.Dropdown(
|
| 95 |
+
choices=["arialbd.ttf", "Cairo-Bold.ttf", "Almarai-Bold.ttf"],
|
| 96 |
value="arialbd.ttf",
|
| 97 |
+
label="نوع الخط"
|
| 98 |
)
|
| 99 |
+
color_opt = gr.ColorPicker(value="#FFFF00", label="لون النص")
|
| 100 |
+
size_opt = gr.Slider(30, 150, value=70, label="حجم الخط")
|
|
|
|
| 101 |
|
| 102 |
+
btn_1 = gr.Button("1. استخراج")
|
| 103 |
+
table = gr.Dataframe(headers=["الكلمة", "البداية", "النهاية"], interactive=True)
|
| 104 |
+
btn_2 = gr.Button("2. إنتاج الفيديو")
|
| 105 |
+
status = gr.Textbox(label="الحالة")
|
|
|
|
| 106 |
|
| 107 |
+
btn_1.click(step_1_extract_words, inputs=[v_in], outputs=[table, status])
|
| 108 |
+
btn_2.click(step_2_render_video, inputs=[v_in, table, font_opt, color_opt, size_opt], outputs=[v_out, status])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
app.launch()
|
|
|