digidau commited on
Commit
830b065
·
verified ·
1 Parent(s): 15933a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -39
app.py CHANGED
@@ -1,16 +1,19 @@
1
- import gradio as gr
2
  import os
3
- import shutil
4
  import json
 
 
5
 
6
  from fonts import setup_fonts
7
  from converter import convert_qurancaption_to_qvm
8
- from background import generate_background
9
  from subtitle import generate_ass
 
10
  from renderer import render_final_video
11
 
12
 
13
- # download font saat startup
 
 
 
14
  setup_fonts()
15
 
16
  os.makedirs("uploads", exist_ok=True)
@@ -18,6 +21,41 @@ os.makedirs("temp", exist_ok=True)
18
  os.makedirs("outputs", exist_ok=True)
19
 
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def process(
22
  images,
23
  audio,
@@ -25,50 +63,75 @@ def process(
25
  arabic_font
26
  ):
27
 
28
- # cleanup
29
- for folder in ["uploads", "temp"]:
 
 
 
30
 
31
  if os.path.exists(folder):
32
  shutil.rmtree(folder)
33
 
34
  os.makedirs(folder)
35
 
 
 
 
 
36
  image_paths = []
37
 
38
- # save uploaded images
39
  for i, img in enumerate(images):
40
 
41
  ext = os.path.splitext(
42
  img.name
43
  )[1]
44
 
45
- dst = f"uploads/{i:03d}{ext}"
 
 
46
 
47
  shutil.copy(
48
  img.name,
49
  dst
50
  )
51
 
52
- image_paths.append(dst)
 
 
53
 
54
- # save audio
55
- audio_path = "uploads/audio.mp3"
 
 
 
 
 
56
 
57
  shutil.copy(
58
  audio,
59
  audio_path
60
  )
61
 
62
- # save json
63
- json_path = "uploads/timing.json"
 
 
 
 
 
64
 
65
  shutil.copy(
66
  timing_json.name,
67
  json_path
68
  )
69
 
70
- # convert QuranCaption
71
- qvm_json = "temp/qvm_timing.json"
 
 
 
 
 
72
 
73
  convert_qurancaption_to_qvm(
74
  json_path,
@@ -95,16 +158,26 @@ def process(
95
  preview[0]
96
  )
97
 
98
- # generate background video
99
- background_path = "temp/background.mp4"
 
 
 
 
 
100
 
101
  generate_background(
102
  image_paths,
103
  background_path
104
  )
105
 
106
- # generate subtitle ass
107
- subtitle_path = "temp/subtitles.ass"
 
 
 
 
 
108
 
109
  generate_ass(
110
  qvm_json,
@@ -112,8 +185,13 @@ def process(
112
  arabic_font
113
  )
114
 
115
- # render final video
116
- final_video = "outputs/final.mp4"
 
 
 
 
 
117
 
118
  render_final_video(
119
  background_path,
@@ -125,6 +203,10 @@ def process(
125
  return final_video
126
 
127
 
 
 
 
 
128
  with gr.Blocks() as demo:
129
 
130
  gr.Markdown(
@@ -141,16 +223,14 @@ Generate cinematic Quran video automatically.
141
  """
142
  )
143
 
144
- with gr.Row():
145
-
146
- images = gr.Files(
147
- label="Upload Images",
148
- file_count="multiple"
149
- )
150
 
151
  audio = gr.Audio(
152
  type="filepath",
153
- label="Upload Audio"
154
  )
155
 
156
  timing_json = gr.File(
@@ -158,25 +238,21 @@ Generate cinematic Quran video automatically.
158
  )
159
 
160
  arabic_font = gr.Dropdown(
161
- [
162
- "Noto Naskh Arabic",
163
- "Noto Kufi Arabic",
164
- "Noto Sans Arabic"
165
- ],
166
- value="Noto Naskh Arabic",
167
- label="Arabic Font"
168
  )
169
 
170
- btn = gr.Button(
171
  "Generate Video",
172
  variant="primary"
173
  )
174
 
175
- output = gr.Video(
176
  label="Result"
177
  )
178
 
179
- btn.click(
180
  fn=process,
181
  inputs=[
182
  images,
@@ -184,7 +260,8 @@ Generate cinematic Quran video automatically.
184
  timing_json,
185
  arabic_font
186
  ],
187
- outputs=output
188
  )
189
 
 
190
  demo.launch()
 
 
1
  import os
 
2
  import json
3
+ import shutil
4
+ import gradio as gr
5
 
6
  from fonts import setup_fonts
7
  from converter import convert_qurancaption_to_qvm
 
8
  from subtitle import generate_ass
9
+ from background import generate_background
10
  from renderer import render_final_video
11
 
12
 
13
+ # --------------------------------------------------
14
+ # Startup
15
+ # --------------------------------------------------
16
+
17
  setup_fonts()
18
 
19
  os.makedirs("uploads", exist_ok=True)
 
21
  os.makedirs("outputs", exist_ok=True)
22
 
23
 
24
+ # --------------------------------------------------
25
+ # Fonts
26
+ # --------------------------------------------------
27
+
28
+ def get_available_fonts():
29
+
30
+ fonts = []
31
+
32
+ if os.path.exists("fonts"):
33
+
34
+ for f in os.listdir("fonts"):
35
+
36
+ if (
37
+ f.endswith(".ttf")
38
+ or f.endswith(".otf")
39
+ ):
40
+
41
+ fonts.append(
42
+ f.replace(".ttf", "")
43
+ .replace(".otf", "")
44
+ )
45
+
46
+ if not fonts:
47
+
48
+ fonts = [
49
+ "NotoNaskhArabic-Regular"
50
+ ]
51
+
52
+ return sorted(fonts)
53
+
54
+
55
+ # --------------------------------------------------
56
+ # Main Process
57
+ # --------------------------------------------------
58
+
59
  def process(
60
  images,
61
  audio,
 
63
  arabic_font
64
  ):
65
 
66
+ # clean temp folders
67
+ for folder in [
68
+ "uploads",
69
+ "temp"
70
+ ]:
71
 
72
  if os.path.exists(folder):
73
  shutil.rmtree(folder)
74
 
75
  os.makedirs(folder)
76
 
77
+ # --------------------
78
+ # Images
79
+ # --------------------
80
+
81
  image_paths = []
82
 
 
83
  for i, img in enumerate(images):
84
 
85
  ext = os.path.splitext(
86
  img.name
87
  )[1]
88
 
89
+ dst = (
90
+ f"uploads/{i:03d}{ext}"
91
+ )
92
 
93
  shutil.copy(
94
  img.name,
95
  dst
96
  )
97
 
98
+ image_paths.append(
99
+ dst
100
+ )
101
 
102
+ # --------------------
103
+ # Audio
104
+ # --------------------
105
+
106
+ audio_path = (
107
+ "uploads/audio.mp3"
108
+ )
109
 
110
  shutil.copy(
111
  audio,
112
  audio_path
113
  )
114
 
115
+ # --------------------
116
+ # JSON
117
+ # --------------------
118
+
119
+ json_path = (
120
+ "uploads/timing.json"
121
+ )
122
 
123
  shutil.copy(
124
  timing_json.name,
125
  json_path
126
  )
127
 
128
+ # --------------------
129
+ # Convert QuranCaption
130
+ # --------------------
131
+
132
+ qvm_json = (
133
+ "temp/qvm_timing.json"
134
+ )
135
 
136
  convert_qurancaption_to_qvm(
137
  json_path,
 
158
  preview[0]
159
  )
160
 
161
+ # --------------------
162
+ # Background Video
163
+ # --------------------
164
+
165
+ background_path = (
166
+ "temp/background.mp4"
167
+ )
168
 
169
  generate_background(
170
  image_paths,
171
  background_path
172
  )
173
 
174
+ # --------------------
175
+ # ASS Subtitle
176
+ # --------------------
177
+
178
+ subtitle_path = (
179
+ "temp/subtitles.ass"
180
+ )
181
 
182
  generate_ass(
183
  qvm_json,
 
185
  arabic_font
186
  )
187
 
188
+ # --------------------
189
+ # Final Render
190
+ # --------------------
191
+
192
+ final_video = (
193
+ "outputs/final.mp4"
194
+ )
195
 
196
  render_final_video(
197
  background_path,
 
203
  return final_video
204
 
205
 
206
+ # --------------------------------------------------
207
+ # UI
208
+ # --------------------------------------------------
209
+
210
  with gr.Blocks() as demo:
211
 
212
  gr.Markdown(
 
223
  """
224
  )
225
 
226
+ images = gr.Files(
227
+ label="Upload Images",
228
+ file_count="multiple"
229
+ )
 
 
230
 
231
  audio = gr.Audio(
232
  type="filepath",
233
+ label="Upload Audio MP3"
234
  )
235
 
236
  timing_json = gr.File(
 
238
  )
239
 
240
  arabic_font = gr.Dropdown(
241
+ choices=get_available_fonts(),
242
+ value=get_available_fonts()[0],
243
+ label="Arabic Font"
 
 
 
 
244
  )
245
 
246
+ generate_btn = gr.Button(
247
  "Generate Video",
248
  variant="primary"
249
  )
250
 
251
+ output_video = gr.Video(
252
  label="Result"
253
  )
254
 
255
+ generate_btn.click(
256
  fn=process,
257
  inputs=[
258
  images,
 
260
  timing_json,
261
  arabic_font
262
  ],
263
+ outputs=output_video
264
  )
265
 
266
+
267
  demo.launch()