-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_video.py
More file actions
156 lines (129 loc) · 7.01 KB
/
Copy pathgenerate_video.py
File metadata and controls
156 lines (129 loc) · 7.01 KB
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
from moviepy.editor import VideoFileClip, concatenate_audioclips, concatenate_videoclips, AudioFileClip, CompositeAudioClip, vfx, ImageClip
import cv2
import numpy as np
import os
def Zoom(clip,mode='in',position='center',speed=1):
fps = clip.fps
duration = clip.duration
total_frames = int(duration*fps)
def tata(getframe,t):
frame = getframe(t)
h,w = frame.shape[:2]
i = t*fps
if mode == 'out':
i = total_frames-i
zoom = 1+(i*((0.1*speed)/total_frames))
positions = {'center':[(w-(w*zoom))/2,(h-(h*zoom))/2],
'left':[0,(h-(h*zoom))/2],
'right':[(w-(w*zoom)),(h-(h*zoom))/2],
'top':[(w-(w*zoom))/2,0],
'topleft':[0,0],
'topright':[(w-(w*zoom)),0],
'bottom':[(w-(w*zoom))/2,(h-(h*zoom))],
'bottomleft':[0,(h-(h*zoom))],
'bottomright':[(w-(w*zoom)),(h-(h*zoom))]}
tx,ty = positions[position]
M = np.array([[zoom,0,tx], [0,zoom,ty]])
frame = cv2.warpAffine(frame,M,(w,h))
return frame
return clip.fl(tata)
def create_video_with_image(image_files, interval, audio_file_name, title, musicFileName, images_directory, channel):
# Load the audio file
file_path = os.path.join("output", "audio", audio_file_name)
voiceover = AudioFileClip(file_path)
voiceover_duration = int(voiceover.duration)
audioMusic = AudioFileClip(musicFileName)
# Repeat music to match voiceover duration if necessary
if audioMusic.duration < voiceover_duration:
num_repeats = int(voiceover_duration / audioMusic.duration) + 1
audioMusic = concatenate_audioclips([audioMusic] * num_repeats)
audioMusic = audioMusic.set_duration(voiceover_duration + 2)
else:
audioMusic = audioMusic.set_duration(voiceover_duration + 2)
# Adjust volume levels
voiceover = voiceover.volumex(2.0) # doubles audio volume
backgroundSong = audioMusic.volumex(0.2)
# Calculate the number of intervals
num_intervals = int(voiceover_duration / interval)
# Create video clips for each image
image_clips = []
images_used = []
for i in range(num_intervals):
# Check if it's the last image
if i == num_intervals - 1:
images_used.append(image_files[i % len(image_files)])
image_clip = ImageClip(images_directory + image_files[i % len(image_files)], duration=interval).set_fps(60).fx(vfx.fadein, 0.2)
else:
images_used.append(image_files[i % len(image_files)])
image_clip = ImageClip(images_directory + image_files[i % len(image_files)], duration=interval).set_fps(60).fx(vfx.fadein, 0.2).fx(vfx.fadeout, 0.2)
# Apply custom transformation
image_clip = image_clip.resize((1920, 1080))
image_clips.append(Zoom(image_clip, mode='in',position='center',speed=1.2))
print(images_used)
# Concatenate the image clips
combined = concatenate_videoclips(image_clips)
# Set audio for the combined video
combined.audio = CompositeAudioClip([voiceover, backgroundSong])
# Write the combined video to a file
new_video_path = "output/videos/" + channel + "/" + title
mp4name = "/" + str(title).capitalize().replace("_", " ") + ".mp4"
finished_video_path = new_video_path + mp4name # Provide the path of the new folder
combined.write_videofile(finished_video_path)
return images_used
def create_video_with_video(video_files, interval, audio_file_name, title, musicFileName, videos_directory, channel):
# Load the audio file
file_path = os.path.join("output", "audio", audio_file_name)
voiceover = AudioFileClip(file_path)
voiceover_duration = int(voiceover.duration)
audioMusic = AudioFileClip(musicFileName)
# Check if voiceover is longer than music
if voiceover_duration > audioMusic.duration:
# Calculate how many times to repeat the music
num_repeats = int(voiceover_duration / audioMusic.duration) + 1
# Concatenate the music clips to extend its duration
audioMusic = concatenate_audioclips([audioMusic] * num_repeats)
# Trim the concatenated music clip to match the duration of the voiceover
audioMusic = audioMusic.set_duration(voiceover_duration + 2)
# Adjust volume levels
voiceover = voiceover.volumex(2.0) # doubles audio volume
backgroundSong = audioMusic.volumex(0.2)
# Calculate the number of intervals
num_intervals = int(voiceover_duration / interval)
# Create video clips for each image
video_clips = []
for i in range(num_intervals):
# Check if it's the last image
if i == num_intervals - 1:
video_clip = VideoFileClip(videos_directory + video_files[i % len(video_files)]).set_duration(interval).set_fps(24).fx(vfx.fadein, 0.2)
else:
video_clip = VideoFileClip(videos_directory + video_files[i % len(video_files)]).set_duration(interval).set_fps(24).fx(vfx.fadein, 0.2).fx(vfx.fadeout, 0.2)
# Apply custom transformation
video_clips.append(Zoom(video_clip, mode='in',position='center',speed=1.2))
# Concatenate the video clips
combined = concatenate_videoclips(video_clips)
# Set audio for the combined video
combined.audio = CompositeAudioClip([voiceover, backgroundSong])
# Write the combined video to a file
new_video_path = "output/videos/" + channel + "/" + title
mp4name = "/" + str(title).capitalize().replace("_", " ") + ".mp4"
finished_video_path = new_video_path + mp4name # Provide the path of the new folder
# finished_subtitle_video_path = new_video_path + "/finished_subtitles.mp4"
combined.write_videofile(finished_video_path)
# audio_path = "output/audio/" + audio_file_name
# generate_subtitle_file(new_video_path, audio_path, finished_video_path, finished_subtitle_video_path)
# Example usage:
# video_files = ["1476223_People_1920x1080.mp4", "6013785_Nature_Lake_Forest_3840x2160.mp4" ]
# images_directory = os.path.join("channels", "the_universe_decoded", "images") # Replace with your images directory
# all_files_images = os.listdir(images_directory)
# # Filter image files
# IMAGE_FILES = []
# for file in all_files_images:
# IMAGE_FILES.append(file)
# image_namechange(images_directory)
# interval = 15 # Replace with your interval
# audio_file_name = "2024-05-131711.mp3" # Replace with your audio file
# title = "TEST_TITLE" # Replace with your desired title
# musicFileName = "channels/the_universe_decoded/audio/nature_and_background.mp3" # Replace with your background music file
# videos_directory = "channels/the_universe_decoded/videos/" # Replace with your images directory
# create_video_with_video(video_files, interval, audio_file_name, title, musicFileName, videos_directory)
# create_video_with_image(IMAGE_FILES, interval, audio_file_name, title, musicFileName, images_directory)