kader1997 commited on
Commit
62983bd
·
verified ·
1 Parent(s): 0f27677

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -7,13 +7,11 @@ 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
  return reshape(text) + "\n "
15
 
16
- # إضافة gr.Progress لتتبع التقدم في الواجهة
17
  def step_1_extract_words(video_path, progress=gr.Progress()):
18
  if not video_path:
19
  return None, "الرجاء رفع فيديو أولاً."
@@ -22,7 +20,7 @@ def step_1_extract_words(video_path, progress=gr.Progress()):
22
  segments, _ = model.transcribe(video_path, word_timestamps=True, language="ar")
23
 
24
  words_data = []
25
- progress(0.5, desc="جاري تحليل الصوت واستخراج الكلمات...")
26
 
27
  for segment in segments:
28
  for word in segment.words:
@@ -31,18 +29,19 @@ def step_1_extract_words(video_path, progress=gr.Progress()):
31
  df = pd.DataFrame(words_data, columns=["الكلمة", "البداية", "النهاية"])
32
  return df, "تم الاستخراج بنجاح!"
33
 
34
- def step_2_render_video(video_path, df_edited, progress=gr.Progress()):
 
35
  if video_path is None or df_edited is None or df_edited.empty:
36
  return None, "بيانات ناقصة."
37
 
38
- output_path = "final_clean_video.mp4"
39
  video = VideoFileClip(video_path)
40
  w, h = int(video.w), int(video.h)
41
  clips = [video]
42
  words_list = df_edited.values.tolist()
43
  chunk_size = 3
44
 
45
- progress(0.1, desc="جاري تحضير النصوص...")
46
 
47
  for i in range(0, len(words_list), chunk_size):
48
  current_chunk = words_list[i : i + chunk_size]
@@ -55,12 +54,12 @@ def step_2_render_video(video_path, df_edited, progress=gr.Progress()):
55
 
56
  txt_clip = TextClip(
57
  text=clean_sentence,
58
- font_size=65,
59
- color='yellow',
60
  stroke_color='black',
61
  stroke_width=2,
62
  method='caption',
63
- font=FONT_PATH,
64
  size=(int(w * 0.9), None),
65
  text_align='center'
66
  ).with_start(c_start).with_duration(duration).with_position(('center', int(h * 0.65)))
@@ -69,35 +68,48 @@ def step_2_render_video(video_path, df_edited, progress=gr.Progress()):
69
 
70
  final_video = CompositeVideoClip(clips, size=(w, h))
71
 
72
- # هنا أعدنا الـ logger لكي تظهر النسبة المئوية % في الـ Log
73
- print("بدء عملية دمج الفيديو والترميز...")
74
  final_video.write_videofile(
75
  output_path,
76
  codec="libx264",
77
  audio_codec="aac",
78
  fps=video.fps,
79
- logger='bar' # 'bar' هي التي تظهر النسبة المئوية % في الـ Logs
80
  )
81
 
82
  return output_path, "تم إنتاج الفيديو بنجاح!"
83
 
84
  # --- بناء الواجهة ---
85
- with gr.Blocks(title="Caption Pro Progress") as app:
86
- gr.Markdown("# 🎬 Caption Pro - With Progress Bar")
87
 
88
  with gr.Row():
89
  video_in = gr.Video(label="فيديو المدخلات")
90
  video_out = gr.Video(label="الفيديو الناتج")
91
 
 
 
 
 
 
 
 
 
 
 
92
  status = gr.Textbox(label="الحالة", interactive=False)
93
  table = gr.Dataframe(headers=["الكلمة", "البداية", "النهاية"], datatype=["str", "number", "number"], interactive=True)
94
 
95
  btn_ex = gr.Button("1. استخراج الكلمات", variant="primary")
96
  btn_re = gr.Button("2. إنتاج الفيديو", variant="secondary")
97
 
98
- # نلاحظ إضافة progress=True في الربط
99
  btn_ex.click(step_1_extract_words, inputs=[video_in], outputs=[table, status])
100
- btn_re.click(step_2_render_video, inputs=[video_in, table], outputs=[video_out, status])
 
 
 
 
101
 
102
  if __name__ == "__main__":
103
- app.launch()
 
7
  from arabic_reshaper import reshape
8
 
9
  # --- الإعدادات ---
 
10
  model = WhisperModel("large-v3", device="cpu", compute_type="int8")
11
 
12
  def process_arabic_text(text):
13
  return reshape(text) + "\n "
14
 
 
15
  def step_1_extract_words(video_path, progress=gr.Progress()):
16
  if not video_path:
17
  return None, "الرجاء رفع فيديو أولاً."
 
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:
 
29
  df = pd.DataFrame(words_data, columns=["الكلمة", "البداية", "النهاية"])
30
  return df, "تم الاستخراج بنجاح!"
31
 
32
+ # إضافة خيارات اللون والخط كمدخلات للدالة
33
+ def step_2_render_video(video_path, df_edited, font_selection, text_color, font_size, progress=gr.Progress()):
34
  if video_path is None or df_edited is None or df_edited.empty:
35
  return None, "بيانات ناقصة."
36
 
37
+ output_path = "final_captioned_video.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]
 
54
 
55
  txt_clip = TextClip(
56
  text=clean_sentence,
57
+ font_size=font_size,
58
+ color=text_color, # اللون المختار من الواجهة
59
  stroke_color='black',
60
  stroke_width=2,
61
  method='caption',
62
+ font=font_selection, # الخط المختار من الواجهة
63
  size=(int(w * 0.9), None),
64
  text_align='center'
65
  ).with_start(c_start).with_duration(duration).with_position(('center', int(h * 0.65)))
 
68
 
69
  final_video = CompositeVideoClip(clips, size=(w, h))
70
 
71
+ print(f"بدء الدمج بلون {text_color} وخط {font_selection}...")
 
72
  final_video.write_videofile(
73
  output_path,
74
  codec="libx264",
75
  audio_codec="aac",
76
  fps=video.fps,
77
+ logger='bar'
78
  )
79
 
80
  return output_path, "تم إنتاج الفيديو بنجاح!"
81
 
82
  # --- بناء الواجهة ---
83
+ with gr.Blocks(title="Caption Pro Custom") as app:
84
+ gr.Markdown("# 🎬 محرر الكابشن: تخصيص الألوان والخطوط")
85
 
86
  with gr.Row():
87
  video_in = gr.Video(label="فيديو المدخلات")
88
  video_out = gr.Video(label="الفيديو الناتج")
89
 
90
+ with gr.Row():
91
+ # خيارات التخصيص
92
+ font_dropdown = gr.Dropdown(
93
+ choices=["arialbd.ttf", "Cairo-Bold.ttf", "Almarai-Bold.ttf", "Tajawal-Bold.ttf"],
94
+ value="arialbd.ttf",
95
+ label="اختر الخط (تأكد من رفع الملف)"
96
+ )
97
+ color_picker = gr.ColorPicker(value="#FFFF00", label="اختر لون النص") # القيمة الافتراضية أصفر
98
+ size_slider = gr.Slider(minimum=30, maximum=120, value=70, step=5, label="حجم الخط")
99
+
100
  status = gr.Textbox(label="الحالة", interactive=False)
101
  table = gr.Dataframe(headers=["الكلمة", "البداية", "النهاية"], datatype=["str", "number", "number"], interactive=True)
102
 
103
  btn_ex = gr.Button("1. استخراج الكلمات", variant="primary")
104
  btn_re = gr.Button("2. إنتاج الفيديو", variant="secondary")
105
 
106
+ # ربط المدخلات الجديدة بالدالة
107
  btn_ex.click(step_1_extract_words, inputs=[video_in], outputs=[table, status])
108
+ btn_re.click(
109
+ step_2_render_video,
110
+ inputs=[video_in, table, font_dropdown, color_picker, size_slider],
111
+ outputs=[video_out, status]
112
+ )
113
 
114
  if __name__ == "__main__":
115
+ app.launch()