File size: 5,753 Bytes
b1d4203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b45bbe
 
 
b1d4203
 
7b45bbe
 
 
 
 
b1d4203
7b45bbe
b1d4203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dec3ec5
b1d4203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b45bbe
 
 
 
 
 
 
 
 
 
 
b1d4203
768cd5c
b1d4203
 
 
 
 
7b45bbe
b1d4203
 
 
 
 
 
 
2266c59
b1d4203
 
768cd5c
b1d4203
dec3ec5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1d4203
768cd5c
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
from moviepy.editor import VideoFileClip, concatenate_videoclips

files = {
    1: "1.mp4", 2: "2.mp4", 3: "3.mp4", 4: "4.mp4", 5: "5.mp4",
    6: "6.mp4", 7: "7.mp4", 8: "8.mp4", 9: "9.mp4",
    10: "10.mp4", 11: "11.mp4", 12: "12.mp4", 13: "13.mp4", 14: "14.mp4",
    15: "15.mp4", 16: "16.mp4", 17: "17.mp4", 18: "18.mp4", 19: "19.mp4",
    20: "20.mp4", 30: "30.mp4", 40: "40.mp4", 50: "50.mp4",
    60: "60.mp4", 70: "70.mp4", 80: "80.mp4", 90: "90.mp4",
    100: "100.mp4", 1000: "1000.mp4",
    100000: "100000.mp4"
}

def number_to_clips(n):
    components = []
    if n >= 100000:
        lakh = n // 100000
        if lakh > 0:
            components.append(lakh)
            components.append(100000)
        n %= 100000
    if n >= 1000:
        thousand = n // 1000
        if thousand > 0:
            components.append(thousand)
            components.append(1000)
        n %= 1000
    if n >= 100:
        hundred = n // 100
        if hundred > 0:
            components.append(hundred)
            components.append(100)
        n %= 100
    if n > 20:
        tens = (n // 10) * 10
        if tens > 0:
            components.append(tens)
        n %= 10
    if n > 0:
        components.append(n)
    return components

def generate_clip_sequence(n):
    components = number_to_clips(n)
    clip_sequence = []
    for component in components:
        if component in files:
            clip_sequence.append(files[component])
        else:
            # Break down further if component is not in files
            sub_components = number_to_clips(component)
            clip_sequence.extend([files[sub] for sub in sub_components])
    return clip_sequence


def trimmed_clip_path(clip, avatar_path="custom_clips"):
    print(f"{avatar_path}/{clip.split('.mp4')[0]}_cut.mp4")
    return f"{avatar_path}/{clip.split('.mp4')[0]}_cut.mp4"


def get_clip_duration(clip, avatar_path="custom_clips"):
    if avatar_path == "custom_clips":
        avatar_path = "custom_clips"
    else:
        avatar_path = "template_clips"
    try:
        clip = VideoFileClip(trimmed_clip_path(clip, avatar_path=avatar_path))
        return clip.duration
    except Exception as e:
        print(f"Error loading clip {trimmed_clip_path(clip)}: {e}")
        return 0
    


def cut_clip(clip, st, en):
    """
    Cuts the given clip from start time `st` to end time `en`.
    """
    return clip.subclip(st, en)



def create_video_old(number,st, en, output_file="output.mp4"):
    """
    Creates a video for the given number using pre-defined video clips.
    """
    
    # Special files
    intro_file = "trimmed_clips/Intro_cut.mp4"
    conclusion_file = "trimmed_clips/Conclusion_cut.mp4"

    clips_to_combine = [intro_file]

    clips = generate_clip_sequence(number)
    for clip in clips:
        clips_to_combine.append(trimmed_clip_path(clip))

    clips_to_combine.append(conclusion_file)

    # Load and combine video clips
    video_clips = []
    for clip_path in clips_to_combine:
        try:
            clip = VideoFileClip(clip_path)
            clip = cut_clip(clip, st, clip.duration - en)
            video_clips.append(clip)
        except Exception as e:
            print(f"Error loading clip {clip_path}: {e}")

    # Concatenate the video clips
    if video_clips:
        final_video = concatenate_videoclips(video_clips)
        final_video.write_videofile(output_file, codec="libx264", audio_codec="aac")
    else:
        print("No clips to combine. Video creation failed.")


def create_advanced_video(number, trim_settings, avatar, output_file="output.mp4"):
    if avatar == "custom":
        path = "custom_clips"
        intro_file = f"{path}/Intro_cut.mp4"
        conclusion_file = f"{path}/Conclusion_cut.mp4"
    else:
        path = "template_clips"
        intro_file = f"{path}/Intro_cut.mp4"
        conclusion_file = f"{path}/Conclusion_cut.mp4"

    print(f"Using avatar: {avatar}")

    clips_to_combine = [VideoFileClip(intro_file)]
    clips = generate_clip_sequence(number)

    video_clips = []
    for i, clip_path in enumerate(clips):
        try:
            clip = VideoFileClip(trimmed_clip_path(clip_path, avatar_path=path))
            trim_start, trim_end = trim_settings[i]
            trimmed_clip = cut_clip(clip, trim_start, clip.duration - trim_end)
            video_clips.append(trimmed_clip)
        except Exception as e:
            print(f"Error loading clip {clip_path}: {e}")

    clips_to_combine.extend(video_clips)
    clips_to_combine.append(VideoFileClip(conclusion_file))

    if video_clips:
        final_video = concatenate_videoclips(clips_to_combine)
        final_video.write_videofile(output_file, codec="libx264", audio_codec="aac")
    else:
        print("No clips to combine. Video creation failed.")


def combine_video(number, video_id, output_file="output.mp4"):
    path = f"{video_id}_clips"
    intro_file = f"{path}/Intro_cut.mp4"
    conclusion_file = f"{path}/Conclusion_cut.mp4"

    clips_to_combine = [intro_file]

    clips = generate_clip_sequence(number)
    for clip in clips:
        clips_to_combine.append(trimmed_clip_path(clip, avatar_path=path))

    clips_to_combine.append(conclusion_file)

    # Load and combine video clips
    video_clips = []
    for clip_path in clips_to_combine:
        try:
            clip = VideoFileClip(clip_path)
            video_clips.append(clip)
        except Exception as e:
            print(f"Error loading clip {clip_path}: {e}")

    # Concatenate the video clips
    if video_clips:
        final_video = concatenate_videoclips(video_clips)
        final_video.write_videofile(output_file, codec="libx264", audio_codec="aac")
    else:
        print("No clips to combine. Video creation failed.")