File size: 9,460 Bytes
7f420a2
7d2babc
830b065
 
238b8e7
c6951d0
18259f1
7f420a2
afa92ba
 
78fd8b4
7f420a2
 
830b065
7cd2f78
7f420a2
e1cf819
78fd8b4
830b065
 
 
 
5a5661d
7f420a2
238b8e7
 
 
 
 
 
 
 
 
50e1e76
238b8e7
50e1e76
238b8e7
 
 
 
 
 
 
 
 
50e1e76
238b8e7
 
 
 
 
78fd8b4
 
 
 
7f420a2
830b065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50e1e76
830b065
 
 
 
 
 
 
 
 
f97cb86
c6951d0
788454e
18259f1
f97cb86
 
 
 
 
 
c6951d0
18259f1
c6951d0
18259f1
 
 
788454e
 
 
18259f1
788454e
18259f1
788454e
18259f1
788454e
 
 
 
 
 
 
 
 
18259f1
788454e
 
 
 
18259f1
 
 
 
788454e
c6951d0
18259f1
 
 
 
 
c6951d0
18259f1
 
 
c6951d0
 
afa92ba
 
 
 
 
 
 
 
 
 
 
 
 
 
ceea7f2
18259f1
 
 
 
 
 
 
6ec0fa6
18259f1
 
 
 
 
 
c6951d0
 
38a1c21
 
 
 
 
 
 
 
e61e890
38a1c21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18259f1
 
 
 
 
 
22796c2
18259f1
 
 
afa92ba
18259f1
 
afa92ba
18259f1
 
afa92ba
 
 
 
18259f1
 
afa92ba
 
 
 
18259f1
 
22796c2
c6951d0
e61e890
 
46c886c
78fd8b4
 
c31df8c
78fd8b4
 
49e8ca9
62ef7b9
 
4224815
 
 
78fd8b4
7f420a2
50e1e76
 
 
 
 
1a82d38
 
 
 
 
 
 
 
 
 
830b065
 
 
 
7f420a2
 
 
78fd8b4
7f420a2
 
 
 
1a82d38
78fd8b4
1a82d38
78fd8b4
1a82d38
 
 
78fd8b4
1a82d38
 
 
78fd8b4
1a82d38
 
 
 
 
 
 
 
7f420a2
830b065
 
 
7f420a2
78fd8b4
 
 
 
7f420a2
830b065
 
 
78fd8b4
 
 
 
 
 
830b065
 
 
7f420a2
 
 
 
 
 
7d2babc
 
 
 
 
78fd8b4
7d2babc
78fd8b4
51bccf8
 
7d2babc
 
 
 
78fd8b4
7d2babc
78fd8b4
7d2babc
 
 
 
78fd8b4
830b065
 
 
7f420a2
7cd2f78
 
 
b329749
7cd2f78
 
 
 
 
 
7f420a2
 
7cd2f78
7f420a2
830b065
 
 
7f420a2
 
8293472
ceea7f2
62ef7b9
 
4224815
 
 
7f420a2
 
e1cf819
 
830b065
7f420a2
 
 
 
 
e1cf819
7f420a2
e1cf819
 
 
 
 
 
 
 
 
 
 
 
7f420a2
e1cf819
7f420a2
 
830b065
 
 
 
7f420a2
 
78fd8b4
 
 
 
 
 
 
 
 
7f420a2
78fd8b4
 
7f420a2
 
e1cf819
49e8ca9
 
 
830b065
 
 
 
78fd8b4
c31df8c
 
 
 
7f420a2
78fd8b4
830b065
7f420a2
 
 
78fd8b4
7f420a2
 
78fd8b4
830b065
 
 
78fd8b4
7f420a2
62ef7b9
 
08a26bc
62ef7b9
 
 
 
 
 
 
 
 
 
 
 
00faf4f
 
 
 
 
 
 
 
62ef7b9
00faf4f
 
 
 
 
 
 
 
f97cb86
 
 
 
 
 
 
 
830b065
78fd8b4
 
 
 
830b065
78fd8b4
 
7f420a2
98291ac
 
 
 
 
 
 
 
 
 
 
 
 
 
c6951d0
830b065
78fd8b4
 
 
c31df8c
78fd8b4
 
49e8ca9
62ef7b9
 
4224815
 
 
51d0201
830b065
7f420a2
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import os
import json
import shutil
import gradio as gr
import subprocess
import cv2
import tempfile



from fonts import setup_fonts
from converter import convert_qurancaption_to_qvm
from subtitle import generate_ass
from background import generate_background
from background_video import prepare_background_video
from renderer import render_final_video
from intro import prepend_intro

# --------------------------------------------------
# Startup
# --------------------------------------------------

setup_fonts()

for f in os.listdir("fonts"):

    if f.endswith(".ttf") or f.endswith(".otf"):

        path = os.path.join(
            "fonts",
            f
        )

        print("\n" + "=" * 60)
        print("FONT INFO:", f)
        print("=" * 60)

        try:

            result = subprocess.run(
                ["fc-scan", path],
                capture_output=True,
                text=True
            )

            print(result.stdout[:5000])

        except Exception as e:

            print(e)

os.makedirs("uploads", exist_ok=True)
os.makedirs("temp", exist_ok=True)
os.makedirs("outputs", exist_ok=True)


# --------------------------------------------------
# Fonts
# --------------------------------------------------

def get_available_fonts():

    fonts = []

    if os.path.exists("fonts"):

        for f in os.listdir("fonts"):

            if (
                f.endswith(".ttf")
                or f.endswith(".otf")
            ):

                fonts.append(
                    f.replace(".ttf", "")
                     .replace(".otf", "")
                )

    if not fonts:

        fonts = [
            "Amiri-Regular"
        ]

    return sorted(fonts)


# --------------------------------------------------
# Main Process
# --------------------------------------------------

def preview_layout(
    images,
    background_video,
    timing_json,
    arabic_font,
    arabic_y,
    translation_gap,
    arabic_font_size,
    translation_font_size
):

    os.makedirs("temp", exist_ok=True)

    # =====================================
    # BACKGROUND
    # =====================================

    if images:

        background_image = images[0].name

    elif background_video:

        background_image = "temp/preview_frame.jpg"

        subprocess.run(
            [
                "ffmpeg",
                "-y",
                "-i",
                background_video,
                "-vframes",
                "1",
                background_image
            ],
            check=True
        )

    else:

        raise Exception(
            "Upload Images atau Background Video"
        )

    # =====================================
    # JSON -> QVM
    # =====================================

    qvm_json = "temp/preview_qvm.json"

    convert_qurancaption_to_qvm(
        timing_json.name,
        qvm_json
    )




    with open(
        qvm_json,
        "r",
        encoding="utf-8"
    ) as f:
    
        data = json.load(f)
    
    data[0]["start_time"] = 0
    data[0]["end_time"] = 999
    
 
    # =====================================
    # ASS
    # =====================================

    preview_ass = "temp/preview.ass"

    generate_ass(
        qvm_json,
        preview_ass,
        arabic_font,
        arabic_y,
        translation_gap,
        arabic_font_size,
        translation_font_size
    )

    print("=" * 60)
    print("PREVIEW ASS")
    print("=" * 60)
    
    print(
        "ASS EXISTS:",
        os.path.exists(preview_ass)
    )
    
    if os.path.exists(preview_ass):
    
        print(
            "ASS SIZE:",
            os.path.getsize(preview_ass)
        )
    
        with open(
            preview_ass,
            "r",
            encoding="utf-8"
        ) as f:
    
            print(f.read())

    # =====================================
    # RENDER 1 FRAME
    # =====================================

    preview_output = "temp/preview.jpg"

    subprocess.run(
        [
            "ffmpeg",
            "-y",
    
            "-loop", "1",
            "-i", background_image,
    
            "-vf",
            f"ass={preview_ass}:fontsdir=fonts",
    
            "-ss",
            "1",
    
            "-frames:v",
            "1",
    
            "-update",
            "1",
    
            preview_output
        ],
        check=True
    )

    return preview_output
    
def process(
    images,
    background_video,
    audio,
    timing_json,
    arabic_font,
    intro_video,
    arabic_y,
    translation_gap,
    arabic_font_size,
    translation_font_size
):

    print("=" * 60)
    print("SELECTED UI FONT")
    print("=" * 60)
    print(arabic_font)


    # ==========================================
    # VALIDATION
    # ==========================================

    if not images and not background_video:
        raise Exception(
            "Upload Images atau Background Video"
        )
        
    for folder in [
        "uploads",
        "temp"
    ]:

        if os.path.exists(folder):
            shutil.rmtree(folder)

        os.makedirs(folder)

    image_paths = []

    if images:

        for i, img in enumerate(images):

            ext = os.path.splitext(
                img.name
            )[1]

            dst = (
                f"uploads/{i:03d}{ext}"
            )

            shutil.copy(
                img.name,
                dst
            )

            image_paths.append(
                dst
            )

    audio_path = (
        "uploads/audio.mp3"
    )

    shutil.copy(
        audio,
        audio_path
    )

    json_path = (
        "uploads/timing.json"
    )

    shutil.copy(
        timing_json.name,
        json_path
    )

    qvm_json = (
        "temp/qvm_timing.json"
    )

    convert_qurancaption_to_qvm(
        json_path,
        qvm_json
    )

    with open(
        qvm_json,
        "r",
        encoding="utf-8"
    ) as f:

        preview = json.load(f)



    print(
        "TOTAL SUBTITLES:",
        len(preview)
    )

    if preview:

        print(
            "FIRST:",
            preview[0]
        )

    background_path = (
        "temp/background.mp4"
    )

    if background_video:

        prepare_background_video(
        background_video,
        background_path
        )

    else:

        generate_background(
        image_paths,
        background_path
        )

    subtitle_path = (
        "temp/subtitles.ass"
    )

    generate_ass(
        qvm_json,
        subtitle_path,
        arabic_font,
        arabic_y,
        translation_gap,
        arabic_font_size,
        translation_font_size
    )

    main_video = (
        "outputs/main.mp4"
    )

    render_final_video(
        background_path,
        audio_path,
        subtitle_path,
        main_video
    )
    
    if intro_video:

        final_video = "outputs/final.mp4"

        prepend_intro(
            intro_video.name,
            main_video,
            final_video
        )

        return final_video

    return main_video


# --------------------------------------------------
# UI
# --------------------------------------------------

with gr.Blocks() as demo:

    gr.Markdown(
        """
# Quran Cinematic Video Generator

Upload:

- Images
- Audio MP3
- QuranCaption JSON

Generate cinematic Quran video automatically.
"""
    )

    intro_video = gr.File(
    label="Intro Video (Optional)"
    )
    
    images = gr.Files(
        label="Upload Images",
        file_count="multiple"
    )

    background_video = gr.Video(
    label="Background Video (Optional)"
    )

    audio = gr.Audio(
        type="filepath",
        label="Upload Audio MP3"
    )

    timing_json = gr.File(
        label="Upload QuranCaption JSON"
    )

    arabic_font = gr.Dropdown(
        choices=get_available_fonts(),
        value=get_available_fonts()[0],
        label="Arabic Font"
    )

    arabic_y = gr.Slider(
    minimum=200,
    maximum=1200,
    value=380,
    step=10,
    label="Arabic Position Y"
    )
    
    translation_gap = gr.Slider(
        minimum=0,
        maximum=120,
        value=25,
        step=5,
        label="Translation Gap"
    )

    arabic_font_size = gr.Slider(
    80,
    180,
    value=120,
    step=2,
    label="Arabic Font Size"
    )
    
    translation_font_size = gr.Slider(
        20,
        60,
        value=34,
        step=1,
        label="Translation Font Size"
    )

    preview_btn = gr.Button(
    "Preview Layout"
    )
    
    preview_image = gr.Image(
        label="Preview"
    )

    generate_btn = gr.Button(
        "Generate Video",
        variant="primary"
    )

    output_video = gr.Video(
        label="Result"
    )

    preview_btn.click(
        fn=preview_layout,
        inputs=[
            images,
            background_video,
            timing_json,
            arabic_font,
            arabic_y,
            translation_gap,
            arabic_font_size,
            translation_font_size
        ],
        outputs=preview_image
    )

    generate_btn.click(
        fn=process,
        inputs=[
            images,
            background_video,
            audio,
            timing_json,
            arabic_font,
            intro_video,
            arabic_y,
            translation_gap,
            arabic_font_size,
            translation_font_size
        ],
        outputs=output_video
    )

demo.launch()