Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,20 +5,20 @@ import pandas as pd
|
|
| 5 |
from faster_whisper import WhisperModel
|
| 6 |
from moviepy import VideoFileClip, TextClip, CompositeVideoClip
|
| 7 |
from arabic_reshaper import reshape
|
| 8 |
-
from bidi.algorithm import get_display
|
| 9 |
|
| 10 |
# --- الإعدادات ---
|
| 11 |
-
FONT_PATH = "arialbd.ttf" # تأكد من رفع هذا الملف في
|
| 12 |
model = WhisperModel("large-v3", device="cpu", compute_type="int8")
|
| 13 |
|
| 14 |
def process_arabic_text(text):
|
|
|
|
| 15 |
return reshape(text) + "\n "
|
| 16 |
|
| 17 |
def step_1_extract_words(video_path):
|
| 18 |
if not video_path:
|
| 19 |
return None, "الرجاء رفع فيديو أولاً."
|
| 20 |
|
| 21 |
-
|
| 22 |
segments, _ = model.transcribe(video_path, word_timestamps=True, language="ar")
|
| 23 |
words_data = []
|
| 24 |
for segment in segments:
|
|
@@ -40,16 +40,26 @@ def step_2_render_video(video_path, df_edited):
|
|
| 40 |
|
| 41 |
chunk_size = 3
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
for i in range(0, len(words_list), chunk_size):
|
| 44 |
current_chunk = words_list[i : i + chunk_size]
|
|
|
|
| 45 |
sentence = " ".join([str(r[0]) for r in current_chunk])
|
| 46 |
-
clean_sentence =
|
| 47 |
|
| 48 |
c_start = float(current_chunk[0][1])
|
| 49 |
c_end = float(current_chunk[-1][2])
|
| 50 |
-
duration = c_end - c_start
|
| 51 |
|
| 52 |
-
# إنشاء الن
|
| 53 |
txt_clip = TextClip(
|
| 54 |
text=clean_sentence,
|
| 55 |
font_size=90,
|
|
@@ -62,39 +72,33 @@ def step_2_render_video(video_path, df_edited):
|
|
| 62 |
text_align='center'
|
| 63 |
).with_start(c_start).with_duration(duration).with_position(('center', int(h * 0.75)))
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
def zoom_effect(t):
|
| 68 |
-
# يبدأ من 80% من الحجم ويصل لـ 100% في أول 0.15 ثانية
|
| 69 |
-
if t < 0.15:
|
| 70 |
-
return 0.8 + (1.33 * t)
|
| 71 |
-
return 1.0
|
| 72 |
-
|
| 73 |
-
# تطبيق التأثير بطريقة متوافقة مع الإصدار الجديد
|
| 74 |
-
animated_txt = txt_clip.with_effects([lambda clip: clip.resize(zoom_effect)])
|
| 75 |
|
| 76 |
clips.append(animated_txt)
|
| 77 |
|
|
|
|
| 78 |
final_video = CompositeVideoClip(clips, size=(w, h))
|
| 79 |
final_video.write_videofile(output_path, codec="libx264", audio_codec="aac", fps=video.fps, logger=None)
|
|
|
|
| 80 |
return output_path, "تم إنتاج الفيديو بأنيميشن Pop-up بنجاح!"
|
| 81 |
|
| 82 |
-
# --- بناء الواجهة
|
| 83 |
-
with gr.Blocks() as app:
|
| 84 |
-
gr.Markdown("# 🎬 Caption Pro - 3 Words Style")
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
-
video_in = gr.Video(label="
|
| 88 |
-
video_out = gr.Video(label="
|
| 89 |
|
| 90 |
-
status = gr.Textbox(label="
|
| 91 |
-
table = gr.Dataframe(headers=["الكلمة", "البداية", "النهاية"], interactive=True)
|
| 92 |
|
| 93 |
-
btn_ex = gr.Button("1. استخراج الكلمات", variant="primary")
|
| 94 |
-
btn_re = gr.Button("2. إنتاج الفيديو", variant="secondary")
|
| 95 |
|
| 96 |
-
# تصحيح الربط هنا: outputs تتبع ترتيب القيم الراجعة من الدالة
|
| 97 |
btn_ex.click(step_1_extract_words, inputs=[video_in], outputs=[table, status])
|
| 98 |
btn_re.click(step_2_render_video, inputs=[video_in, table], outputs=[video_out, status])
|
| 99 |
|
| 100 |
-
|
|
|
|
|
|
| 5 |
from faster_whisper import WhisperModel
|
| 6 |
from moviepy import VideoFileClip, TextClip, CompositeVideoClip
|
| 7 |
from arabic_reshaper import reshape
|
|
|
|
| 8 |
|
| 9 |
# --- الإعدادات ---
|
| 10 |
+
FONT_PATH = "arialbd.ttf" # تأكد من رفع هذا الملف في مجلد المشروع
|
| 11 |
model = WhisperModel("large-v3", device="cpu", compute_type="int8")
|
| 12 |
|
| 13 |
def process_arabic_text(text):
|
| 14 |
+
# نستخدم reshape فقط لأن السيرفر يدعم الاتجاه التلقائي
|
| 15 |
return reshape(text) + "\n "
|
| 16 |
|
| 17 |
def step_1_extract_words(video_path):
|
| 18 |
if not video_path:
|
| 19 |
return None, "الرجاء رفع فيديو أولاً."
|
| 20 |
|
| 21 |
+
print("جاري استخراج الكلمات...")
|
| 22 |
segments, _ = model.transcribe(video_path, word_timestamps=True, language="ar")
|
| 23 |
words_data = []
|
| 24 |
for segment in segments:
|
|
|
|
| 40 |
|
| 41 |
chunk_size = 3
|
| 42 |
|
| 43 |
+
# دالة الزووم (Pop-up)
|
| 44 |
+
def zoom_effect(t):
|
| 45 |
+
if t < 0.15:
|
| 46 |
+
# تأثير الانبثاق: من 80% إلى 100% من الحجم
|
| 47 |
+
return 0.8 + (1.33 * t)
|
| 48 |
+
return 1.0
|
| 49 |
+
|
| 50 |
+
print(f"جاري معالجة {len(words_list)} كلمة...")
|
| 51 |
+
|
| 52 |
for i in range(0, len(words_list), chunk_size):
|
| 53 |
current_chunk = words_list[i : i + chunk_size]
|
| 54 |
+
# تجميع الكلمات الثلاث في نص واحد
|
| 55 |
sentence = " ".join([str(r[0]) for r in current_chunk])
|
| 56 |
+
clean_sentence = process_arabic_text(sentence)
|
| 57 |
|
| 58 |
c_start = float(current_chunk[0][1])
|
| 59 |
c_end = float(current_chunk[-1][2])
|
| 60 |
+
duration = max(0.1, c_end - c_start)
|
| 61 |
|
| 62 |
+
# 1. إنشاء نص الكابشن
|
| 63 |
txt_clip = TextClip(
|
| 64 |
text=clean_sentence,
|
| 65 |
font_size=90,
|
|
|
|
| 72 |
text_align='center'
|
| 73 |
).with_start(c_start).with_duration(duration).with_position(('center', int(h * 0.75)))
|
| 74 |
|
| 75 |
+
# 2. تطبيق الأنيميشن مباشرة (Resize)
|
| 76 |
+
animated_txt = txt_clip.resize(zoom_effect)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
clips.append(animated_txt)
|
| 79 |
|
| 80 |
+
print("جاري دمج الفيديو النهائي...")
|
| 81 |
final_video = CompositeVideoClip(clips, size=(w, h))
|
| 82 |
final_video.write_videofile(output_path, codec="libx264", audio_codec="aac", fps=video.fps, logger=None)
|
| 83 |
+
|
| 84 |
return output_path, "تم إنتاج الفيديو بأنيميشن Pop-up بنجاح!"
|
| 85 |
|
| 86 |
+
# --- بناء الواجهة ---
|
| 87 |
+
with gr.Blocks(title="Caption Pro V3") as app:
|
| 88 |
+
gr.Markdown("# 🎬 Caption Pro - 3 Words Style & Animation")
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
+
video_in = gr.Video(label="فيديو المدخلات")
|
| 92 |
+
video_out = gr.Video(label="الفيديو الناتج")
|
| 93 |
|
| 94 |
+
status = gr.Textbox(label="الحالة", interactive=False)
|
| 95 |
+
table = gr.Dataframe(headers=["الكلمة", "البداية", "النهاية"], datatype=["str", "number", "number"], interactive=True)
|
| 96 |
|
| 97 |
+
btn_ex = gr.Button("1. استخراج الكلمات بدقة Large-v3", variant="primary")
|
| 98 |
+
btn_re = gr.Button("2. إنتاج الفيديو بالأنيميشن", variant="secondary")
|
| 99 |
|
|
|
|
| 100 |
btn_ex.click(step_1_extract_words, inputs=[video_in], outputs=[table, status])
|
| 101 |
btn_re.click(step_2_render_video, inputs=[video_in, table], outputs=[video_out, status])
|
| 102 |
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
app.launch()
|