kader1997 commited on
Commit
253bd7c
ยท
verified ยท
1 Parent(s): 2c93198

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -37
app.py CHANGED
@@ -35,67 +35,62 @@ def step_2_render_video(video_path, df_edited):
35
  if video_path is None or df_edited is None or df_edited.empty:
36
  return None, "ุชุฃูƒุฏ ู…ู† ูˆุฌูˆุฏ ุงู„ููŠุฏูŠูˆ ูˆุงู„ุจูŠุงู†ุงุช."
37
 
38
- output_path = "pro_output.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
 
44
- # ุชู‚ุณูŠู… ุงู„ูƒู„ู…ุงุช ุฅู„ู‰ ู…ุฌู…ูˆุนุงุช (ูƒู„ ู…ุฌู…ูˆุนุฉ 3 ูƒู„ู…ุงุช)
45
  chunk_size = 3
 
46
  for i in range(len(words_list)):
47
- # ุชุญุฏูŠุฏ ุงู„ู…ุฌู…ูˆุนุฉ ุงู„ุชูŠ ุชู†ุชู…ูŠ ุฅู„ูŠู‡ุง ุงู„ูƒู„ู…ุฉ ุงู„ุญุงู„ูŠุฉ
48
  start_chunk = (i // chunk_size) * chunk_size
49
  end_chunk = min(start_chunk + chunk_size, len(words_list))
50
  current_chunk = words_list[start_chunk:end_chunk]
51
-
52
- # ุจู†ุงุก ุงู„ู†ุต ู„ู„ู…ุฌู…ูˆุนุฉ ู…ุน ุชู…ูŠูŠุฒ ุงู„ูƒู„ู…ุฉ ุงู„ุญุงู„ูŠุฉ
53
- full_text_parts = []
54
- for idx, row in enumerate(current_chunk):
55
- word_idx_in_main_list = start_chunk + idx
56
- # ุฅุฐุง ูƒุงู†ุช ู‡ุฐู‡ ู‡ูŠ ุงู„ูƒู„ู…ุฉ ุงู„ุชูŠ ูŠู†ุทู‚ู‡ุง ุงู„ู…ุนู„ู‚ ุงู„ุขู†
57
- if word_idx_in_main_list == i:
58
- # ุงู„ูƒู„ู…ุฉ ุงู„ู…ู†ุทูˆู‚ุฉ (ุฃุจูŠุถ) - ุณู†ู‚ูˆู… ุจูˆุณู…ู‡ุง ู„ุงุญู‚ุงู‹ ุฃูˆ ู…ุนุงู„ุฌุชู‡ุง
59
- # ููŠ MoviePy ุงู„ู…ุฌุงู†ูŠุŒ ุฃุณู‡ู„ ุทุฑูŠู‚ุฉ ู‡ูŠ ุชุบูŠูŠุฑ ุงู„ู„ูˆู† ู„ู„ู€ Clip ุจุงู„ูƒุงู…ู„ ุฃุซู†ุงุก ูˆู‚ุชู‡ุง
60
- color = 'white'
61
- else:
62
- # ุงู„ูƒู„ู…ุงุช ุงู„ุฃุฎุฑู‰ ููŠ ุงู„ู…ุฌู…ูˆุนุฉ (ุฃุตูุฑ)
63
- color = 'yellow'
64
-
65
- # ู„ู„ุญุตูˆู„ ุนู„ู‰ ุชุฃุซูŠุฑ 3 ูƒู„ู…ุงุช ุชุธู‡ุฑ ู…ุนุงู‹ ู…ุน ุชู„ูˆูŠู† ุงู„ูˆุงุญุฏุฉ ุงู„ู…ู†ุทูˆู‚ุฉ:
66
- # ู†ู‚ูˆู… ุจุฅู†ุดุงุก ุงู„ุฌู…ู„ุฉ (ุงู„ู€ 3 ูƒู„ู…ุงุช)
67
- sentence = " ".join([r[0] for r in current_chunk])
68
- clean_sentence = reshape(sentence) # ุฑุจุท ุงู„ุฌู…ู„ุฉ ูƒุงู…ู„ุฉ
69
 
70
- # ุฅู†ุดุงุก ุงู„ูƒู„ูŠุจ ู„ู„ูƒู„ู…ุฉ ุงู„ุญุงู„ูŠุฉ (ุณุชุธู‡ุฑ ุงู„ุฌู…ู„ุฉ ูƒุงู…ู„ุฉุŒ ู„ูƒู† ู„ูˆู†ู‡ุง ูŠุชุบูŠุฑ ุญุณุจ ุงู„ุชูˆู‚ูŠุช)
71
- txt_clip = TextClip(
 
 
 
 
 
 
 
 
72
  text=clean_sentence + "\n ",
73
  font_size=80,
74
- color='yellow', # ุงู„ู„ูˆู† ุงู„ุงูุชุฑุงุถูŠ ู„ู„ุฌู…ู„ุฉ
75
  stroke_color='black',
76
  stroke_width=2,
77
  method='caption',
78
  font=FONT_PATH,
79
  size=(int(w * 0.9), None),
80
  text_align='center'
81
- )
82
-
83
- # ุงู„ุฌุฒุก ุงู„ุณุญุฑูŠ: ุชู„ูˆูŠู† ุงู„ูƒู„ู…ุฉ ุงู„ู…ู†ุทูˆู‚ุฉ ูู‚ุท
84
- # ุจู…ุง ุฃู† TextClip ุจุณูŠุทุŒ ุณู†ู‚ูˆู… ุจุฅู†ุดุงุก "ู‚ู†ุงุน" ุฃูˆ ุทุจู‚ุฉ ุจูŠุถุงุก ู„ู„ูƒู„ู…ุฉ ุงู„ู…ู†ุทูˆู‚ุฉ ููˆู‚ ุงู„ุฌู…ู„ุฉ ุงู„ุตูุฑุงุก
85
- # ูˆู„ูƒู† ู„ุชุณู‡ูŠู„ ุงู„ุฃู…ุฑ ุนู„ูŠูƒ ุญุงู„ูŠุงู‹ุŒ ุณู†ุฌุนู„ ุงู„ุฌู…ู„ุฉ ุชุธู‡ุฑ ุจู„ูˆู† ุฃุจูŠุถ ุจุงู„ูƒุงู…ู„ ุนู†ุฏู…ุง ุชู†ุทู‚ ุฅุญุฏู‰ ูƒู„ู…ุงุชู‡ุง
86
- # ุฃูˆ ู†ุบูŠุฑ ู„ูˆู† ุงู„ุฌู…ู„ุฉ ูƒุงู…ู„ุฉ ู„ู„ุฃุจูŠุถ ุฃุซู†ุงุก ู†ุทู‚ ุงู„ูƒู„ู…ุฉ ุงู„ุญุงู„ูŠุฉ:
87
 
88
- txt_clip = (txt_clip
89
- .with_start(words_list[i][1])
90
- .with_duration(max(0.1, words_list[i][2] - words_list[i][1]))
91
- .with_color('white') # ุชู„ูˆูŠู† ุงู„ุฌู…ู„ุฉ ุจุงู„ุฃุจูŠุถ ุฃุซู†ุงุก ู†ุทู‚ ุงู„ูƒู„ู…ุฉ
92
- .with_position(('center', int(h * 0.75))))
 
 
 
 
 
 
 
 
93
 
94
- clips.append(txt_clip)
 
95
 
96
  final_video = CompositeVideoClip(clips, size=(w, h))
97
  final_video.write_videofile(output_path, codec="libx264", audio_codec="aac", fps=video.fps)
98
- return output_path, "ุชู… ุงู„ุฅู†ุชุงุฌ ุจู†ู…ุท 3 ูƒู„ู…ุงุช ุจู†ุฌุงุญ!"
99
 
100
  # --- ุงู„ูˆุงุฌู‡ุฉ (ู†ูุณ ูˆุงุฌู‡ุชูƒ ุงู„ุณุงุจู‚ุฉ) ---
101
  with gr.Blocks(title="Caption Pro V2") as app:
 
35
  if video_path is None or df_edited is None or df_edited.empty:
36
  return None, "ุชุฃูƒุฏ ู…ู† ูˆุฌูˆุฏ ุงู„ููŠุฏูŠูˆ ูˆุงู„ุจูŠุงู†ุงุช."
37
 
38
+ output_path = "final_pro_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
 
44
+ # ุญุฌู… ุงู„ู…ุฌู…ูˆุนุฉ (3 ูƒู„ู…ุงุช)
45
  chunk_size = 3
46
+
47
  for i in range(len(words_list)):
48
+ # ุชุญุฏูŠุฏ ุจุฏุงูŠุฉ ูˆู†ู‡ุงูŠุฉ ุงู„ู…ุฌู…ูˆุนุฉ ุงู„ุญุงู„ูŠุฉ (3 ูƒู„ู…ุงุช)
49
  start_chunk = (i // chunk_size) * chunk_size
50
  end_chunk = min(start_chunk + chunk_size, len(words_list))
51
  current_chunk = words_list[start_chunk:end_chunk]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
+ # 1. ุฅู†ุดุงุก ุงู„ุฌู…ู„ุฉ ุงู„ูƒุงู…ู„ุฉ (ุจุงู„ู„ูˆู† ุงู„ุฃุตูุฑ) ู„ุชุนู…ู„ ูƒุฎู„ููŠุฉ
54
+ full_sentence = " ".join([r[0] for r in current_chunk])
55
+ clean_sentence = reshape(full_sentence) # ุจุฏูˆู† ู‚ู„ุจ ู„ุฃู† ุงู„ุณูŠุฑูุฑ ูŠุฏุนู… RTL
56
+
57
+ # ุชูˆู‚ูŠุช ุธู‡ูˆุฑ ุงู„ู…ุฌู…ูˆุนุฉ (ู…ู† ุจุฏุงูŠุฉ ุฃูˆู„ ูƒู„ู…ุฉ ู„ู†ู‡ุงูŠุฉ ุขุฎุฑ ูƒู„ู…ุฉ ููŠ ุงู„ู…ุฌู…ูˆุนุฉ)
58
+ chunk_start_time = float(current_chunk[0][1])
59
+ chunk_end_time = float(current_chunk[-1][2])
60
+
61
+ # ุทุจู‚ุฉ ุงู„ุฌู…ู„ุฉ ูƒุงู…ู„ุฉ (ุฃุตูุฑ)
62
+ bg_txt = TextClip(
63
  text=clean_sentence + "\n ",
64
  font_size=80,
65
+ color='yellow',
66
  stroke_color='black',
67
  stroke_width=2,
68
  method='caption',
69
  font=FONT_PATH,
70
  size=(int(w * 0.9), None),
71
  text_align='center'
72
+ ).with_start(chunk_start_time).with_duration(chunk_end_time - chunk_start_time).with_position(('center', int(h * 0.75)))
 
 
 
 
 
73
 
74
+ # 2. ุทุจู‚ุฉ ุงู„ูƒู„ู…ุฉ ุงู„ู†ุดุทุฉ (ุฃุจูŠุถ) ุชุธู‡ุฑ ูู‚ุท ููŠ ูˆู‚ุชู‡ุง
75
+ # ู…ู„ุงุญุธุฉ: ู„ุฌุนู„ู‡ุง ุงุญุชุฑุงููŠุฉ 100% ุณู†ุบูŠุฑ ู„ูˆู† ุงู„ุฌู…ู„ุฉ ู„ู„ุฃุจูŠุถ ุจุงู„ูƒุงู…ู„ ููŠ ูˆู‚ุช ุงู„ูƒู„ู…ุฉ ุงู„ุญุงู„ูŠุฉ
76
+ active_txt = TextClip(
77
+ text=clean_sentence + "\n ",
78
+ font_size=80,
79
+ color='white', # ุงู„ูƒู„ู…ุฉ ุงู„ู†ุดุทุฉ ุชุถูŠุก ุงู„ุฌู…ู„ุฉ ุจุงู„ุฃุจูŠุถ
80
+ stroke_color='black',
81
+ stroke_width=2,
82
+ method='caption',
83
+ font=FONT_PATH,
84
+ size=(int(w * 0.9), None),
85
+ text_align='center'
86
+ ).with_start(float(words_list[i][1])).with_duration(max(0.1, float(words_list[i][2]) - float(words_list[i][1]))).with_position(('center', int(h * 0.75)))
87
 
88
+ clips.append(bg_txt)
89
+ clips.append(active_txt)
90
 
91
  final_video = CompositeVideoClip(clips, size=(w, h))
92
  final_video.write_videofile(output_path, codec="libx264", audio_codec="aac", fps=video.fps)
93
+ return output_path, "ู…ุจุฑูˆูƒ! ุชู… ุฅู†ุชุงุฌ ุงู„ููŠุฏูŠูˆ ุจู†ู…ุท ุงู„ู…ุญุชุฑููŠู†."
94
 
95
  # --- ุงู„ูˆุงุฌู‡ุฉ (ู†ูุณ ูˆุงุฌู‡ุชูƒ ุงู„ุณุงุจู‚ุฉ) ---
96
  with gr.Blocks(title="Caption Pro V2") as app: