-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_short_video.py
More file actions
98 lines (83 loc) · 4.28 KB
/
Copy pathcreate_short_video.py
File metadata and controls
98 lines (83 loc) · 4.28 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
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_short_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
audioMusic = audioMusic.set_duration(voiceover_duration + 1)
# 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((540, 960))
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) + ".mp4"
finished_video_path = new_video_path + mp4name # Provide the path of the new folder
combined.write_videofile(finished_video_path)
return images_used
# 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)