File size: 2,875 Bytes
faf3bcd
836e38d
 
 
d5dc5fc
 
 
 
faf3bcd
 
123195e
d5dc5fc
836e38d
faf3bcd
 
1a87c1e
faf3bcd
 
 
 
1a87c1e
faf3bcd
 
 
 
1a87c1e
faf3bcd
 
d234946
 
 
 
 
 
 
 
 
1a87c1e
 
 
faf3bcd
1a87c1e
 
 
 
faf3bcd
 
274d72d
 
 
 
 
faf3bcd
 
 
836e38d
d5dc5fc
 
 
 
 
 
 
 
 
 
 
 
 
faf3bcd
d5dc5fc
 
 
 
 
 
 
 
 
 
 
faf3bcd
d5dc5fc
 
faf3bcd
 
 
 
 
 
 
d5dc5fc
 
 
 
 
 
 
 
 
836e38d
 
 
1a87c1e
faf3bcd
 
 
d5dc5fc
 
 
 
 
 
1a87c1e
 
 
d5dc5fc
1a87c1e
 
 
 
d5dc5fc
 
faf3bcd
 
 
1a87c1e
faf3bcd
1a87c1e
faf3bcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a87c1e
 
 
faf3bcd
1a87c1e
 
faf3bcd
 
 
 
 
 
 
1a87c1e
 
 
d234946
faf3bcd
 
 
 
1a87c1e
 
 
 
faf3bcd
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
import os
import subprocess


def render_final_video(
    background,
    audio,
    subtitles,
    output,
    crf=23,
    preset="ultrafast"
):

    if not os.path.exists(background):
        raise FileNotFoundError(
            f"Background file not found: {background}"
        )

    if not os.path.exists(audio):
        raise FileNotFoundError(
            f"Audio file not found: {audio}"
        )

    if not os.path.exists(subtitles):
        raise FileNotFoundError(
            f"Subtitle file not found: {subtitles}"
        )

    print("=" * 60)
    print("AVAILABLE FONTS")
    print("=" * 60)

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

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

    print("=" * 60)
    print("FINAL RENDER START")
    print("=" * 60)

    print("BACKGROUND :", background)
    print("AUDIO      :", audio)
    print("SUBTITLE   :", subtitles)
    print("OUTPUT     :", output)

    vf_filter = (
        #"drawbox="
        #"x=0:y=0:"
        #"w=iw:h=ih:"
        #"color=black@0.20:"
        #"t=fill,"
        f"ass={subtitles}:fontsdir=fonts"
    )

    cmd = [
        "ffmpeg",
        "-y",

        "-stream_loop",
        "-1",

        "-i",
        background,

        "-i",
        audio,

        "-vf",
        vf_filter,

        "-map",
        "0:v",

        "-map",
        "1:a",

        "-c:v",
        "libx264",

        "-preset",
        preset,

        "-crf",
        str(crf),

        "-pix_fmt",
        "yuv420p",

        "-movflags",
        "+faststart",

        "-c:a",
        "aac",

        "-b:a",
        "192k",

        "-shortest",

        output
    ]

    print("\nFFMPEG COMMAND:\n")
    print(" ".join(cmd))
    print()

    result = subprocess.run(
        cmd,
        capture_output=True,
        text=True
    )

    print("=" * 60)
    print("STDOUT")
    print("=" * 60)
    print(result.stdout)

    print("=" * 60)
    print("STDERR")
    print("=" * 60)
    print(result.stderr)

    result.check_returncode()

    if not os.path.exists(output):

        raise Exception(
            "Output video was not generated"
        )

    probe = subprocess.run(
        [
            "ffprobe",
            "-v",
            "error",

            "-show_entries",
            "format=duration",

            "-of",
            "default=noprint_wrappers=1:nokey=1",

            output
        ],
        capture_output=True,
        text=True
    )

    duration = probe.stdout.strip()

    print("=" * 60)
    print("FINAL VIDEO DURATION")
    print("=" * 60)
    print(duration)

    size_mb = (
        os.path.getsize(output)
        / 1024
        / 1024
    )

    print("=" * 60)
    print("FINAL VIDEO SIZE")
    print("=" * 60)

    print(
        f"{size_mb:.2f} MB"
    )

    print("=" * 60)
    print("FINAL RENDER SUCCESS")
    print("=" * 60)

    return output