You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RedditVideoMakerBot/video_creation/final_video.py

97 lines
2.9 KiB

3 years ago
from moviepy.editor import (
VideoFileClip,
AudioFileClip,
ImageClip,
concatenate_videoclips,
concatenate_audioclips,
CompositeAudioClip,
CompositeVideoClip,
)
import reddit.subreddit
import re
from utils.console import print_step
from dotenv import load_dotenv
import os
from rich.console import Console
console = Console()
3 years ago
W, H = 1080, 1920
def make_final_video(number_of_clips):
# Calls opacity from the .env
load_dotenv()
opacity = os.getenv('OPACITY')
3 years ago
print_step("Creating the final video...")
3 years ago
VideoFileClip.reW = lambda clip: clip.resize(width=W)
VideoFileClip.reH = lambda clip: clip.resize(width=H)
background_clip = (
VideoFileClip("assets/mp4/clip.mp4")
.without_audio()
.resize(height=H)
.crop(x1=1166.6, y1=0, x2=2246.6, y2=1920)
)
3 years ago
# Gather all audio clips
audio_clips = []
for i in range(0, number_of_clips):
audio_clips.append(AudioFileClip(f"assets/mp3/{i}.mp3"))
audio_clips.insert(0, AudioFileClip(f"assets/mp3/title.mp3"))
try:
audio_clips.insert(1, AudioFileClip(f"assets/mp3/posttext.mp3"))
except:
OSError()
3 years ago
audio_concat = concatenate_audioclips(audio_clips)
audio_composite = CompositeAudioClip([audio_concat])
#Get sum of all clip lengths
total_length = sum([clip.duration for clip in audio_clips])
#round total_length to an integer
int_total_length=round(total_length)
#Output Length
console.log(f"[bold green] Video Will Be: {int_total_length} Seconds Long")
3 years ago
# Gather all images
image_clips = []
for i in range(0, number_of_clips):
image_clips.append(
ImageClip(f"assets/png/comment_{i}.png")
.set_duration(audio_clips[i + 1].duration)
.set_position("center")
.resize(width=W - 100)
.set_opacity(float(opacity)),
3 years ago
)
if os.path.exists(f"assets/mp3/posttext.mp3"):
image_clips.insert(
0,
ImageClip(f"assets/png/title.png")
.set_duration(audio_clips[0].duration + audio_clips[1].duration)
.set_position("center")
.resize(width=W - 100)
.set_opacity(float(opacity)),
)
else:
image_clips.insert(
0,
ImageClip(f"assets/png/title.png")
.set_duration(audio_clips[0].duration)
.set_position("center")
.resize(width=W - 100)
.set_opacity(float(opacity)),
)
3 years ago
image_concat = concatenate_videoclips(image_clips).set_position(
("center", "center")
)
image_concat.audio = audio_composite
final = CompositeVideoClip([background_clip, image_concat])
filename = (re.sub('[?\"%*:|<>]', '', ("assets/" + reddit.subreddit.submission.title + ".mp4")))
final.write_videofile(filename, fps=30, audio_codec="aac", audio_bitrate="192k")
3 years ago
for i in range(0, number_of_clips):
pass