kader1997 commited on
Commit
db158f3
·
verified ·
1 Parent(s): bcab1e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -20,7 +20,7 @@ def step_1_extract_words(video_path, progress=gr.Progress()):
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,19 +29,25 @@ def step_1_extract_words(video_path, progress=gr.Progress()):
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]
@@ -55,11 +61,11 @@ def step_2_render_video(video_path, df_edited, font_selection, text_color, font_
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,7 +74,6 @@ def step_2_render_video(video_path, df_edited, font_selection, text_color, font_
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",
@@ -80,21 +85,22 @@ def step_2_render_video(video_path, df_edited, font_selection, text_color, font_
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)
@@ -103,11 +109,10 @@ with gr.Blocks(title="Caption Pro Custom") as app:
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
 
 
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
  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 text_color.startswith('rgba'):
38
+ # تحويل بسيط لضمان قبول اللون
39
+ final_color = "yellow"
40
+ else:
41
+ final_color = text_color
42
+
43
+ output_path = "final_clean_video.mp4"
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]
 
61
  txt_clip = TextClip(
62
  text=clean_sentence,
63
  font_size=font_size,
64
+ color=final_color,
65
  stroke_color='black',
66
  stroke_width=2,
67
  method='caption',
68
+ font=font_selection,
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)))
 
74
 
75
  final_video = CompositeVideoClip(clips, size=(w, h))
76
 
 
77
  final_video.write_videofile(
78
  output_path,
79
  codec="libx264",
 
85
  return output_path, "تم إنتاج الفيديو بنجاح!"
86
 
87
  # --- بناء الواجهة ---
88
+ with gr.Blocks(title="Caption Pro Final") as app:
89
+ gr.Markdown("# 🎬 Caption Pro - Stable Version")
90
 
91
  with gr.Row():
92
  video_in = gr.Video(label="فيديو المدخلات")
93
  video_out = gr.Video(label="الفيديو الناتج")
94
 
95
  with gr.Row():
96
+ # استخدام القائمة المنسدلة للخطوط
97
  font_dropdown = gr.Dropdown(
98
  choices=["arialbd.ttf", "Cairo-Bold.ttf", "Almarai-Bold.ttf", "Tajawal-Bold.ttf"],
99
  value="arialbd.ttf",
100
+ label="اختر الخط (تأكد من وجود الملف)"
101
  )
102
+ # تحديد لون النص عبر الكلمات أو Hex لضمان الاستقرار
103
+ color_input = gr.ColorPicker(value="#FFFF00", label="اختر لون النص")
104
  size_slider = gr.Slider(minimum=30, maximum=120, value=70, step=5, label="حجم الخط")
105
 
106
  status = gr.Textbox(label="الحالة", interactive=False)
 
109
  btn_ex = gr.Button("1. استخراج الكلمات", variant="primary")
110
  btn_re = gr.Button("2. إنتاج الفيديو", variant="secondary")
111
 
 
112
  btn_ex.click(step_1_extract_words, inputs=[video_in], outputs=[table, status])
113
  btn_re.click(
114
  step_2_render_video,
115
+ inputs=[video_in, table, font_dropdown, color_input, size_slider],
116
  outputs=[video_out, status]
117
  )
118