|
|
|
@ -13,6 +13,9 @@ from moviepy.video.io.VideoFileClip import VideoFileClip
|
|
|
|
|
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
|
|
|
|
|
from rich.console import Console
|
|
|
|
|
|
|
|
|
|
import ffmpeg
|
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
|
|
from utils.cleanup import cleanup
|
|
|
|
|
from utils.console import print_step, print_substep
|
|
|
|
|
from utils.video import Video
|
|
|
|
@ -44,6 +47,7 @@ def name_normalize(name: str) -> str:
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_final_video(
|
|
|
|
|
number_of_clips: int,
|
|
|
|
|
length: int,
|
|
|
|
@ -57,106 +61,54 @@ def make_final_video(
|
|
|
|
|
reddit_obj (dict): The reddit object that contains the posts to read.
|
|
|
|
|
background_config (Tuple[str, str, str, Any]): The background config to use.
|
|
|
|
|
"""
|
|
|
|
|
# try: # if it isn't found (i.e you just updated and copied over config.toml) it will throw an error
|
|
|
|
|
# VOLUME_MULTIPLIER = settings.config["settings"]['background']["background_audio_volume"]
|
|
|
|
|
# except (TypeError, KeyError):
|
|
|
|
|
# print('No background audio volume found in config.toml. Using default value of 1.')
|
|
|
|
|
# VOLUME_MULTIPLIER = 1
|
|
|
|
|
id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"])
|
|
|
|
|
print_step("Creating the final video 🎥")
|
|
|
|
|
VideoFileClip.reW = lambda clip: clip.resize(width=W)
|
|
|
|
|
VideoFileClip.reH = lambda clip: clip.resize(width=H)
|
|
|
|
|
opacity = settings.config["settings"]["opacity"]
|
|
|
|
|
transition = settings.config["settings"]["transition"]
|
|
|
|
|
background_clip = (
|
|
|
|
|
VideoFileClip(f"assets/temp/{id}/background.mp4")
|
|
|
|
|
.without_audio()
|
|
|
|
|
.resize(height=H)
|
|
|
|
|
.crop(x1=1166.6, y1=0, x2=2246.6, y2=1920)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Gather all audio clips
|
|
|
|
|
audio_clips = [AudioFileClip(f"assets/temp/{id}/mp3/{i}.mp3") for i in range(number_of_clips)]
|
|
|
|
|
audio_clips.insert(0, AudioFileClip(f"assets/temp/{id}/mp3/title.mp3"))
|
|
|
|
|
audio_concat = concatenate_audioclips(audio_clips)
|
|
|
|
|
audio_composite = CompositeAudioClip([audio_concat])
|
|
|
|
|
audio_clips = [ffmpeg.input(f"assets/temp/{id}/mp3/{i}.mp3") for i in range(number_of_clips)]
|
|
|
|
|
audio_clips.insert(0, ffmpeg.input(f"assets/temp/{id}/mp3/title.mp3"))
|
|
|
|
|
|
|
|
|
|
audio_clips_durations = [float(ffmpeg.probe(f"assets/temp/{id}/mp3/{i}.mp3")['format']['duration']) for i in range(number_of_clips)]
|
|
|
|
|
audio_clips_durations.insert(0, float(ffmpeg.probe(f"assets/temp/{id}/mp3/title.mp3")['format']['duration']))
|
|
|
|
|
audio_concat = ffmpeg.concat(*audio_clips, a=1, v=0)
|
|
|
|
|
ffmpeg.output(audio_concat, f"assets/temp/{id}/audio.mp3").overwrite_output().run()
|
|
|
|
|
|
|
|
|
|
console.log(f"[bold green] Video Will Be: {length} Seconds Long")
|
|
|
|
|
# add title to video
|
|
|
|
|
image_clips = []
|
|
|
|
|
|
|
|
|
|
# Remove the audio from the background video and crop it
|
|
|
|
|
ffmpeg.input(f"assets/temp/{id}/background.mp4").filter('crop', "ih*(9/16)", "ih").output(f"assets/temp/{id}/background_noaudio.mp4", an=None, **{"c:v": "h264"}).overwrite_output().run()
|
|
|
|
|
|
|
|
|
|
audio = ffmpeg.input(f"assets/temp/{id}/audio.mp3")
|
|
|
|
|
video = ffmpeg.input(f"assets/temp/{id}/background_noaudio.mp4")
|
|
|
|
|
|
|
|
|
|
# Gather all images
|
|
|
|
|
new_opacity = 1 if opacity is None or float(opacity) >= 1 else float(opacity)
|
|
|
|
|
new_transition = 0 if transition is None or float(transition) > 2 else float(transition)
|
|
|
|
|
image_clips = list()
|
|
|
|
|
image_clips.insert(
|
|
|
|
|
0,
|
|
|
|
|
ImageClip(f"assets/temp/{id}/png/title.png")
|
|
|
|
|
.set_duration(audio_clips[0].duration)
|
|
|
|
|
.resize(width=W - 100)
|
|
|
|
|
.set_opacity(new_opacity)
|
|
|
|
|
.crossfadein(new_transition)
|
|
|
|
|
.crossfadeout(new_transition),
|
|
|
|
|
ffmpeg.input(f"assets/temp/{id}/png/title.png")['v']
|
|
|
|
|
.filter('scale', 'iw-200', -1)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for i in range(0, number_of_clips):
|
|
|
|
|
image_clips.append(
|
|
|
|
|
ImageClip(f"assets/temp/{id}/png/comment_{i}.png")
|
|
|
|
|
.set_duration(audio_clips[i + 1].duration)
|
|
|
|
|
.resize(width=W - 100)
|
|
|
|
|
.set_opacity(new_opacity)
|
|
|
|
|
.crossfadein(new_transition)
|
|
|
|
|
.crossfadeout(new_transition)
|
|
|
|
|
ffmpeg.input(f"assets/temp/{id}/png/comment_{i}.png")['v']
|
|
|
|
|
.filter('scale', 'iw-200', -1)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# if os.path.exists("assets/mp3/posttext.mp3"):
|
|
|
|
|
# image_clips.insert(
|
|
|
|
|
# 0,
|
|
|
|
|
# ImageClip("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: story mode stuff
|
|
|
|
|
img_clip_pos = background_config[3]
|
|
|
|
|
image_concat = concatenate_videoclips(image_clips).set_position(
|
|
|
|
|
img_clip_pos
|
|
|
|
|
) # note transition kwarg for delay in imgs
|
|
|
|
|
image_concat.audio = audio_composite
|
|
|
|
|
final = CompositeVideoClip([background_clip, image_concat])
|
|
|
|
|
current_time = 0
|
|
|
|
|
for i in range(0, number_of_clips):
|
|
|
|
|
video = video.overlay(image_clips[i], enable=f'between(t,{current_time},{current_time + audio_clips_durations[i]})', x='(main_w-overlay_w)/2', y='(main_h-overlay_h)/2')
|
|
|
|
|
current_time += audio_clips_durations[i]
|
|
|
|
|
|
|
|
|
|
title = re.sub(r"[^\w\s-]", "", reddit_obj["thread_title"])
|
|
|
|
|
idx = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"])
|
|
|
|
|
|
|
|
|
|
filename = f"{name_normalize(title)[:251]}.mp4"
|
|
|
|
|
subreddit = settings.config["reddit"]["thread"]["subreddit"]
|
|
|
|
|
|
|
|
|
|
if not exists(f"./results/{subreddit}"):
|
|
|
|
|
print_substep("The results folder didn't exist so I made it")
|
|
|
|
|
os.makedirs(f"./results/{subreddit}")
|
|
|
|
|
|
|
|
|
|
# if settings.config["settings"]['background']["background_audio"] and exists(f"assets/backgrounds/background.mp3"):
|
|
|
|
|
# audioclip = mpe.AudioFileClip(f"assets/backgrounds/background.mp3").set_duration(final.duration)
|
|
|
|
|
# audioclip = audioclip.fx( volumex, 0.2)
|
|
|
|
|
# final_audio = mpe.CompositeAudioClip([final.audio, audioclip])
|
|
|
|
|
# # lowered_audio = audio_background.multiply_volume( # todo get this to work
|
|
|
|
|
# # VOLUME_MULTIPLIER) # lower volume by background_audio_volume, use with fx
|
|
|
|
|
# final.set_audio(final_audio)
|
|
|
|
|
final = Video(final).add_watermark(
|
|
|
|
|
text=f"Background credit: {background_config[2]}", opacity=0.4, redditid=reddit_obj
|
|
|
|
|
)
|
|
|
|
|
final.write_videofile(
|
|
|
|
|
f"assets/temp/{id}/temp.mp4",
|
|
|
|
|
fps=30,
|
|
|
|
|
audio_codec="aac",
|
|
|
|
|
audio_bitrate="192k",
|
|
|
|
|
verbose=False,
|
|
|
|
|
threads=multiprocessing.cpu_count(),
|
|
|
|
|
)
|
|
|
|
|
ffmpeg_extract_subclip(
|
|
|
|
|
f"assets/temp/{id}/temp.mp4",
|
|
|
|
|
0,
|
|
|
|
|
length,
|
|
|
|
|
targetname=f"results/{subreddit}/{filename}",
|
|
|
|
|
)
|
|
|
|
|
output = ffmpeg.output(video, audio, f"results/{subreddit}/{filename}", f='mp4', **{"c:v": "h264"}).overwrite_output()
|
|
|
|
|
output.run()
|
|
|
|
|
|
|
|
|
|
save_data(subreddit, filename, title, idx, background_config[2])
|
|
|
|
|
print_step("Removing temporary files 🗑")
|
|
|
|
|
cleanups = cleanup(id)
|
|
|
|
|