-
Notifications
You must be signed in to change notification settings - Fork 0
/
videorenderscript.py
384 lines (326 loc) · 13.5 KB
/
videorenderscript.py
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
from logger import Logger
from database import Database
from logger import Config
from discord_logger import Discord_logger
from moviepy.editor import *
import time as pytime
import os
import ast
import datetime
import random
from pydub import AudioSegment
import numpy as np
from mutagen.mp3 import MP3
import glob
from pydub import AudioSegment
#composing video and audio
global codecr, font_path, video_info_parsed, stockvideo, stockmusic, channelname, video_infos,tts_path,module,video_pause_time, fact_count
def realtime():
now = datetime.datetime.now()
return now.strftime('%Y-%m-%d %H:%M:%S')
def get_mp3_length(file_path):
audio = MP3(file_path)
return audio.info.length
def all_matching_tts_files(tts_path,id):
pattern = tts_path + '/' + str(id) + '_*'
files = glob.glob(pattern)
files.sort()
return files
def get_audio_length():
global video_info_parsed,tts_path,module,video_pause_time
id = video_info_parsed[0]
music = []
music_files_sorted = []
matching_files = all_matching_tts_files(tts_path,id)
if matching_files == '':
printmsg = 'no matching tts files were found'
errorlevel = 3
logger.error(printmsg,errorlevel)
discord_logger.error(printmsg,module)
final_video_lenght = []
else:
intro = str(tts_path) + '/' + str(id) + '_0.mp3'
introlength = get_mp3_length(intro)
music.append(introlength)
music.append(video_pause_time)
music_files_sorted.append(intro)
music_files_sorted.append('FILLER')
if intro in matching_files:
matching_files.remove(intro)
for file in matching_files:
factlength = get_mp3_length(file)
music.append(factlength)
music.append(video_pause_time)
music_files_sorted.append(file)
music_files_sorted.append('FILLER')
final_video_lenght = 0
for element in music:
final_video_lenght = final_video_lenght + float(element)
return music,music_files_sorted,final_video_lenght
def read_main():
global module,database
errorlevel = 0
values = 0 # zuerst auf null setzten
while values == 0:
database.connect() #main Database öffnen
values = database.getvalues() #main Database Daten auslesen
realtimevalue = realtime()
database.close() #main Database schließen
if values == 0:
printmsg = 'failed to open ' + module + ' database'
logger.error(printmsg,errorlevel)
realtimevalue = realtime()
if errorlevel < 3:
errorlevel += 1
else:
pytime.sleep(120)
discord_logger.error(printmsg,module)
else:
printmsg = 'opening main database'
logger.success(printmsg)
discord_logger.success(printmsg,module)
return values
def parse_new_video(values):
video_infos = []
array = np.array(values)
if len(array) == 0:
logger.error('main database is empty',errorlevel = 3)
discord_logger.error('main database is empty',module)
else:
seccond_column = array[:, 1]
i = 0
for status in seccond_column:
if status == 'tts_generated':
id = array[i,0]
facts_list = array[i,2]
facts = ast.literal_eval(facts_list)
topic = array[i,3]
videoid = array[i,6]
musicid = array[i,8]
video_infos.append(id)
video_infos.append(facts)
video_infos.append(topic)
video_infos.append(videoid)
video_infos.append(musicid)
return video_infos
i += 1
def findvideo(videotag:str) -> list[str]:
global video_infos
database_video = Database("stockvideo")
errorlevel = 0
values = 0 # zuerst auf null setzten
while values == 0:
database_video.connect() #video Database öffnen
values = database_video.getvalues() #video Database Daten auslesen
realtimevalue = realtime()
database_video.close() #video Database schließen
if values == 0:
printmsg = 'failed to open ' + 'video' + ' database'
logger.error(printmsg,errorlevel)
realtimevalue = realtime()
if errorlevel < 3:
errorlevel += 1
else:
pytime.sleep(120)
discord_logger.error(printmsg,module)
else:
printmsg = 'opening video database'
logger.success(printmsg)
discord_logger.success(printmsg,module)
results = []
for row in values:
if videotag in row[1]:
if float(row[3]) >= float(video_infos[2]):
#result = ",".join(map(str, row[0]))
result = "stockvideo/" + (videotag) + ".mp4"
results.append(result)
else:
result = "stockvideo/default.mp4"
results.append(result)
else:
result = "stockvideo/default.mp4"
results.append(result)
return results
def findmusic(musictag:str) -> list[str]:
global video_infos
database_video = Database("music")
errorlevel = 0
values = 0 # zuerst auf null setzten
while values == 0:
database_video.connect() #music Database öffnen
values = database_video.getvalues() #music Database Daten auslesen
realtimevalue = realtime()
database_video.close() #music Database schließen
if values == 0:
printmsg = 'failed to open ' + 'music' + ' database'
logger.error(printmsg,errorlevel)
realtimevalue = realtime()
if errorlevel < 3:
errorlevel += 1
else:
pytime.sleep(120)
discord_logger.error(printmsg,module)
else:
printmsg = 'opening music database'
logger.success(printmsg)
discord_logger.success(printmsg,module)
results = []
for row in values:
if musictag in row[1]:
if float(row[3]) >= float(video_infos[2]):
#result = ",".join(map(str, row[0]))
result = "stockmusic/" + str(musictag) + ".mp3"
results.append(result)
else:
result = "stockmusic/default.mp3"
results.append(result)
return results
def create_complete_sound():
global video_infos, video_info_parsed, stockmusic
audio_files = video_infos[1]
timings = video_infos[0]
if os.path.exists("createdaudio/" + str(video_info_parsed[0]) + "_99.mp3"):
os.remove("createdaudio/" + str(video_info_parsed[0]) + "_99.mp3")
timings_array = np.array(timings) * 1000
stock_music = AudioSegment.from_file(str(stockmusic))
final_audio = AudioSegment.empty()
start_times = []
start_times.append(0)
i = 0
times_added_up = 0
while i < len(timings):
times_added_up = times_added_up + timings_array[i]
start_times.append(times_added_up)
i += 1
#print(start_times)
final_audio = AudioSegment.empty()
start_times_array = np.array(start_times)
audio_files_array = np.array(audio_files)
x = 0
while x < len(audio_files):
audiofile = audio_files_array[x]
if audiofile != 'FILLER':
audio = AudioSegment.from_file(audiofile)
start_time = int(start_times_array[x]/1000)
print('start time: ' + str(start_time))
audio = audio[start_time:]
final_audio += audio
else:
break_duration = timings_array[x]
print('pause, duration: ' + str(break_duration))
silent_segment = AudioSegment.silent(duration=break_duration)
final_audio += silent_segment
x += 1
final_audio_duration = len(final_audio)
stock_music_cut = stock_music[:final_audio_duration]
stock_music_cut = stock_music_cut.fade_in(2000).fade_out(4000)
final_audio_with_music = final_audio.overlay(stock_music_cut - 20)
output_filename = "createdaudio/" + str(video_info_parsed[0]) + "_99.mp3"
final_audio_with_music.export(output_filename, format="mp3")
total_length = len(final_audio_with_music) / 1000
video_infos.append(total_length)
def render():
global codecr, font_path, video_info_parsed, stockvideo, stockmusic, channelname, fact_count
stock_video_path = str(stockvideo)
stock_music_path = str(stockmusic)
create_complete_sound()
video_clip = VideoFileClip(stock_video_path).set_duration(video_infos[3])
try:
audio_clip = AudioFileClip("createdaudio/" + str(video_info_parsed[0]) + "_99.mp3").set_duration(video_infos[3])
except:
audio_clip = AudioFileClip(stock_music_path).set_duration(video_infos[2])
target_width = video_clip.h * 9 // 16
video_clip = video_clip.crop(x1=(video_clip.w - target_width) / 2, x2=(video_clip.w + target_width) / 2)
video_clip = video_clip.resize(height=1080)
channel_name = "@" + str(channelname)
title = str(fact_count).capitalize() + " Facts \n about " + str(video_info_parsed[2])
facts = video_info_parsed[1]
timings = video_infos[0]
clips = []
timings_array = np.array(timings) * 1000
start_times = []
#start_times.append(0)
i = 0
times_added_up = 0
while i < len(timings):
times_added_up = times_added_up + timings_array[i]
start_times.append(times_added_up)
i += 1
#print(start_times)
all_file_names = np.array(video_infos[1])
start_times_without_pause = []
x = 0
while x < len(video_infos[1]):
file_name = all_file_names[x]
if file_name != 'FILLER':
start_times_without_pause.append(start_times[x])
x += 1
#print(start_times_without_pause)
#pytime.sleep(10000000)
start_times_array = np.array(start_times)
print(start_times_without_pause)
# Erstellung der Textclips für die Fakten
for i in range(len(facts)):
print(facts[i])
fact = facts[i]
start_time = int(start_times_without_pause[i]/1000)
end_time = int(start_times_without_pause[i + 1]/1000) if i + 1 < len(start_times_without_pause) else video_clip.duration
break_duration = 1.0
fact_text = (
TextClip(fact, fontsize=40, color='white', font=font_path, method='caption', align='center',
size=(video_clip.w, None))
.set_start(start_time)
.set_end(end_time + break_duration)
.set_position(('center','center'))
)
clips.append(fact_text)
start_times_without_pause.insert(0,0)
# Erstellung des Textclips für den Channelnamen und den Titel
channel_text = TextClip(channel_name, fontsize=20, color='white', font=font_path, method='caption',
align='center', size=(video_clip.w, None)).set_duration(video_clip.duration)
title_text = TextClip(title, fontsize=70, color='white', font=font_path, method='caption', align='center',
size=(video_clip.w, None)).set_duration((start_times_without_pause[1]/1000)-1).set_start(0)
# Erstellung der Hintergrundboxen für den Titel und den Fakt
title_text = title_text.set_position(('center','center'))
#title_box = ColorClip(size=(video_clip.w, title_text.h + 100), color=(0, 0, 0), duration=start_times_without_pause[0]).set_position(('center', 'center')).set_start(0).set_end((start_times_without_pause[1]/1000)+3)
# Positionierung der Textclips in den Hintergrundboxen
#channel_text = channel_text.set_position(('center', 'center'))
vertical_offset = 150
channel_text = channel_text.set_position(('center', video_clip.h/2 + vertical_offset))
# Overlay der Textclips und Hintergrundboxen auf dem Video
video_with_text = CompositeVideoClip([video_clip, channel_text, title_text] + clips)
# Repeat the video clip in an endless loop with a smooth transition
loop_duration = video_clip.duration + 1.0 # Add 1 second for smooth transition
looped_video = concatenate_videoclips([video_with_text.crossfadein(0.5).crossfadeout(1)])
final_video = looped_video.set_audio(audio_clip) # Stockmusik mit geringer Lautstärke: .volumex(0.2)
final_video.write_videofile("videos/" + str(video_info_parsed[0]) + ".mp4", codec=codecr, fps=24)
#columns_to_update = ['Laenge','Status']
#values_to_update = [3, 'video_generated']
#identifizierung = "ID"
#identifizierung_value = int(video_info_parsed[0])
#database.update("main", columns_to_update, values_to_update, identifizierung, identifizierung_value)
pass
def main():
global video_info_parsed, stockvideo, stockmusic, video_infos
video_infos = []
values = read_main()
video_info_parsed = parse_new_video(values)
music,music_files_sorted,final_video_lenght = get_audio_length()
video_infos.append(music)
video_infos.append(music_files_sorted)
video_infos.append(final_video_lenght)
stockvideo = random.choice(findvideo(str(video_info_parsed[2])))
stockmusic = random.choice(findmusic(str(video_info_parsed[2])))
render()
config = Config('RENDER')
database = Database(str(config.getvalue('database')))
logger = Logger(str(config.getvalue('logger')))
module = str(config.getvalue('module'))
codecr = str(config.getvalue('codec'))
font_path = str(config.getvalue('font_path'))
discord_logger = Discord_logger()
channelname = str(config.getvalue('channelname'))
tts_path = str(config.getvalue('tts_path'))
video_pause_time = float(config.getvalue('video_pause_time'))
fact_count = str(config.getothervalue('GPT','factcount'))
main()