digidau commited on
Commit
18259f1
·
verified ·
1 Parent(s): 788454e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -53
app.py CHANGED
@@ -4,6 +4,7 @@ import shutil
4
  import gradio as gr
5
  import subprocess
6
  import cv2
 
7
 
8
  from fonts import setup_fonts
9
  from converter import convert_qurancaption_to_qvm
@@ -89,6 +90,7 @@ def get_available_fonts():
89
  def preview_layout(
90
  images,
91
  background_video,
 
92
  arabic_font,
93
  arabic_y,
94
  translation_gap,
@@ -96,74 +98,92 @@ def preview_layout(
96
  translation_font_size
97
  ):
98
 
99
- if not images and not background_video:
100
- raise Exception(
101
- "Upload Images atau Background Video"
102
- )
103
-
104
- preview_path = "temp/preview.jpg"
105
 
106
- # =========================
107
- # IMAGE MODE
108
- # =========================
109
 
110
  if images:
111
 
112
- img = cv2.imread(
113
- images[0].name
114
- )
115
-
116
- # =========================
117
- # VIDEO MODE
118
- # =========================
119
 
120
- else:
121
 
122
- frame_path = (
123
- "temp/frame.jpg"
124
- )
125
 
126
  subprocess.run(
127
  [
128
  "ffmpeg",
129
  "-y",
130
-
131
  "-i",
132
  background_video,
133
-
134
  "-vframes",
135
  "1",
136
-
137
- frame_path
138
  ],
139
  check=True
140
  )
141
 
142
- img = cv2.imread(
143
- frame_path
 
 
144
  )
145
 
146
- h, w = img.shape[:2]
 
 
 
 
147
 
148
- scale = min(
149
- 1920 / w,
150
- 1080 / h
151
  )
152
 
153
- img = cv2.resize(
154
- img,
155
- (
156
- int(w * scale),
157
- int(h * scale)
158
- )
 
 
 
 
 
 
 
 
159
  )
160
 
161
- cv2.imwrite(
162
- preview_path,
163
- img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  )
165
 
166
- return preview_path
167
 
168
  def process(
169
  images,
@@ -428,19 +448,20 @@ Generate cinematic Quran video automatically.
428
  label="Result"
429
  )
430
 
431
- preview_btn.click(
432
- fn=preview_layout,
433
- inputs=[
434
- images,
435
- background_video,
436
- arabic_font,
437
- arabic_y,
438
- translation_gap,
439
- arabic_font_size,
440
- translation_font_size
441
- ],
442
- outputs=preview_image
443
- )
 
444
 
445
  generate_btn.click(
446
  fn=process,
 
4
  import gradio as gr
5
  import subprocess
6
  import cv2
7
+ import tempfile
8
 
9
  from fonts import setup_fonts
10
  from converter import convert_qurancaption_to_qvm
 
90
  def preview_layout(
91
  images,
92
  background_video,
93
+ timing_json,
94
  arabic_font,
95
  arabic_y,
96
  translation_gap,
 
98
  translation_font_size
99
  ):
100
 
101
+ os.makedirs("temp", exist_ok=True)
 
 
 
 
 
102
 
103
+ # =====================================
104
+ # BACKGROUND
105
+ # =====================================
106
 
107
  if images:
108
 
109
+ background_image = images[0].name
 
 
 
 
 
 
110
 
111
+ elif background_video:
112
 
113
+ background_image = "temp/preview_frame.jpg"
 
 
114
 
115
  subprocess.run(
116
  [
117
  "ffmpeg",
118
  "-y",
 
119
  "-i",
120
  background_video,
 
121
  "-vframes",
122
  "1",
123
+ background_image
 
124
  ],
125
  check=True
126
  )
127
 
128
+ else:
129
+
130
+ raise Exception(
131
+ "Upload Images atau Background Video"
132
  )
133
 
134
+ # =====================================
135
+ # JSON -> QVM
136
+ # =====================================
137
+
138
+ qvm_json = "temp/preview_qvm.json"
139
 
140
+ convert_qurancaption_to_qvm(
141
+ timing_json.name,
142
+ qvm_json
143
  )
144
 
145
+ # =====================================
146
+ # ASS
147
+ # =====================================
148
+
149
+ preview_ass = "temp/preview.ass"
150
+
151
+ generate_ass(
152
+ qvm_json,
153
+ preview_ass,
154
+ arabic_font,
155
+ arabic_y,
156
+ translation_gap,
157
+ arabic_font_size,
158
+ translation_font_size
159
  )
160
 
161
+ # =====================================
162
+ # RENDER 1 FRAME
163
+ # =====================================
164
+
165
+ preview_output = "temp/preview.jpg"
166
+
167
+ subprocess.run(
168
+ [
169
+ "ffmpeg",
170
+ "-y",
171
+
172
+ "-loop", "1",
173
+ "-i", background_image,
174
+
175
+ "-vf",
176
+ f"ass={preview_ass}:fontsdir=fonts",
177
+
178
+ "-frames:v",
179
+ "1",
180
+
181
+ preview_output
182
+ ],
183
+ check=True
184
  )
185
 
186
+ return preview_output
187
 
188
  def process(
189
  images,
 
448
  label="Result"
449
  )
450
 
451
+ preview_btn.click(
452
+ fn=preview_layout,
453
+ inputs=[
454
+ images,
455
+ background_video,
456
+ timing_json,
457
+ arabic_font,
458
+ arabic_y,
459
+ translation_gap,
460
+ arabic_font_size,
461
+ translation_font_size
462
+ ],
463
+ outputs=preview_image
464
+ )
465
 
466
  generate_btn.click(
467
  fn=process,