refactored final_video.py

updated requirements.txt
simplified cleanup.py
pull/984/head
Jason 2 years ago
parent 7dcac10d1e
commit 36aa256668

@ -9,5 +9,3 @@ requests==2.28.1
rich==12.5.1 rich==12.5.1
toml==0.10.2 toml==0.10.2
translators==5.3.1 translators==5.3.1
Pillow~=9.1.1

@ -2,6 +2,10 @@ import os
from os.path import exists from os.path import exists
def _listdir(d): # listdir with full path
return [os.path.join(d, f) for f in os.listdir(d)]
def cleanup() -> int: def cleanup() -> int:
"""Deletes all temporary assets in assets/temp """Deletes all temporary assets in assets/temp
@ -14,14 +18,12 @@ def cleanup() -> int:
count += len(files) count += len(files)
for f in files: for f in files:
os.remove(f) os.remove(f)
try: REMOVE_DIRS = ["./assets/temp/mp3/", "./assets/temp/png/"]
for file in os.listdir("./assets/temp/mp4"): files_to_remove = list(map(_listdir, REMOVE_DIRS))
for directory in files_to_remove:
for file in directory:
count += 1 count += 1
os.remove("./assets/temp/mp4/" + file) os.remove(file)
except FileNotFoundError:
pass
for file in os.listdir("./assets/temp/mp3"):
count += 1
os.remove("./assets/temp/mp3/" + file)
return count return count
return 0 return 0

@ -43,8 +43,12 @@ def name_normalize(name: str) -> str:
return name return name
def make_final_video(number_of_clips: int, length: int, reddit_obj: dict, def make_final_video(
background_config: Tuple[str, str, str, Any], ): number_of_clips: int,
length: int,
reddit_obj: dict,
background_config: Tuple[str, str, str, Any],
):
"""Gathers audio clips, gathers all screenshots, stitches them together and saves the final video to assets/temp """Gathers audio clips, gathers all screenshots, stitches them together and saves the final video to assets/temp
Args: Args:
number_of_clips (int): Index to end at when going through the screenshots' number_of_clips (int): Index to end at when going through the screenshots'
@ -62,8 +66,11 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict,
VideoFileClip.reH = lambda clip: clip.resize(width=H) VideoFileClip.reH = lambda clip: clip.resize(width=H)
opacity = settings.config["settings"]["opacity"] opacity = settings.config["settings"]["opacity"]
background_clip = ( background_clip = (
VideoFileClip("assets/temp/background.mp4").without_audio().resize(height=H).crop(x1=1166.6, y1=0, x2=2246.6, VideoFileClip("assets/temp/background.mp4")
y2=1920)) .without_audio()
.resize(height=H)
.crop(x1=1166.6, y1=0, x2=2246.6, y2=1920)
)
# Gather all audio clips # Gather all audio clips
audio_clips = [AudioFileClip(f"assets/temp/mp3/{i}.mp3") for i in range(number_of_clips)] audio_clips = [AudioFileClip(f"assets/temp/mp3/{i}.mp3") for i in range(number_of_clips)]
@ -76,15 +83,21 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict,
image_clips = [] image_clips = []
# Gather all images # Gather all images
new_opacity = 1 if opacity is None or float(opacity) >= 1 else float(opacity) new_opacity = 1 if opacity is None or float(opacity) >= 1 else float(opacity)
image_clips.insert(0, image_clips.insert(
ImageClip("assets/temp/png/title.png").set_duration(audio_clips[0].duration).resize( 0,
width=W - 100).set_opacity( ImageClip("assets/temp/png/title.png")
new_opacity), ) .set_duration(audio_clips[0].duration)
.resize(width=W - 100)
.set_opacity(new_opacity),
)
for i in range(0, number_of_clips): for i in range(0, number_of_clips):
image_clips.append( image_clips.append(
ImageClip(f"assets/temp/png/comment_{i}.png").set_duration(audio_clips[i + 1].duration).resize( ImageClip(f"assets/temp/png/comment_{i}.png")
width=W - 100).set_opacity(new_opacity)) .set_duration(audio_clips[i + 1].duration)
.resize(width=W - 100)
.set_opacity(new_opacity)
)
# if os.path.exists("assets/mp3/posttext.mp3"): # if os.path.exists("assets/mp3/posttext.mp3"):
# image_clips.insert( # image_clips.insert(
@ -98,7 +111,8 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict,
# else: story mode stuff # else: story mode stuff
img_clip_pos = background_config[3] img_clip_pos = background_config[3]
image_concat = concatenate_videoclips(image_clips).set_position( image_concat = concatenate_videoclips(image_clips).set_position(
img_clip_pos) # note transition kwarg for delay in imgs img_clip_pos
) # note transition kwarg for delay in imgs
image_concat.audio = audio_composite image_concat.audio = audio_composite
final = CompositeVideoClip([background_clip, image_concat]) final = CompositeVideoClip([background_clip, image_concat])
title = re.sub(r"[^\w\s-]", "", reddit_obj["thread_title"]) title = re.sub(r"[^\w\s-]", "", reddit_obj["thread_title"])
@ -118,14 +132,29 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict,
# # lowered_audio = audio_background.multiply_volume( # todo get this to work # # lowered_audio = audio_background.multiply_volume( # todo get this to work
# # VOLUME_MULTIPLIER) # lower volume by background_audio_volume, use with fx # # VOLUME_MULTIPLIER) # lower volume by background_audio_volume, use with fx
# final.set_audio(final_audio) # final.set_audio(final_audio)
final = Video(final).add_watermark(text=f'Background credit: {background_config[2]}', opacity=0.4) final = Video(final).add_watermark(
final.write_videofile("assets/temp/temp.mp4", fps=30, audio_codec="aac", audio_bitrate="192k", verbose=False, text=f"Background credit: {background_config[2]}", opacity=0.4
threads=multiprocessing.cpu_count(), ) )
ffmpeg_extract_subclip("assets/temp/temp.mp4", 0, final.duration, targetname=f"results/{subreddit}/{filename}", ) final.write_videofile(
"assets/temp/temp.mp4",
fps=30,
audio_codec="aac",
audio_bitrate="192k",
verbose=False,
threads=multiprocessing.cpu_count(),
)
ffmpeg_extract_subclip(
"assets/temp/temp.mp4",
0,
final.duration,
targetname=f"results/{subreddit}/{filename}",
)
save_data(subreddit, filename, title, idx, background_config[2]) save_data(subreddit, filename, title, idx, background_config[2])
print_step("Removing temporary files 🗑") print_step("Removing temporary files 🗑")
cleanups = cleanup() cleanups = cleanup()
print_substep(f"Removed {cleanups} temporary files 🗑") print_substep(f"Removed {cleanups} temporary files 🗑")
print_substep("See result in the results folder!") print_substep("See result in the results folder!")
print_step(f'Reddit title: {reddit_obj["thread_title"]} \n Background Credit: {background_config[2]}') print_step(
f'Reddit title: {reddit_obj["thread_title"]} \n Background Credit: {background_config[2]}'
)

Loading…
Cancel
Save