feat: background audio implementation

closes #11
(note: not my finest work, I wrote this sleep-deprived)
pull/908/head
Jason 2 years ago
parent 3e816b9f8a
commit e44718ffdb

@ -28,9 +28,11 @@ times_to_run = { optional = false, default = 1, example = 2, explanation = "used
opacity = { optional = false, default = 0.9, example = 0.8, explanation = "Sets the opacity of the comments when overlayed over the background", type = "float", nmin = 0, nmax = 1, oob_error = "The opacity HAS to be between 0 and 1", input_error = "The opacity HAS to be a decimal number between 0 and 1" } opacity = { optional = false, default = 0.9, example = 0.8, explanation = "Sets the opacity of the comments when overlayed over the background", type = "float", nmin = 0, nmax = 1, oob_error = "The opacity HAS to be between 0 and 1", input_error = "The opacity HAS to be a decimal number between 0 and 1" }
storymode = { optional = true, type = "bool", default = false, example = false, options = [true, storymode = { optional = true, type = "bool", default = false, example = false, options = [true,
false, false,
] } ], explanation = "not yet implemented" }
background_choice = { optional = true, default = "minecraft", example = "minecraft", options = ["minecraft", "gta", "rocket-league", "motor-gta"], explanation = "Sets the background for the video" } background_choice = { optional = true, default = "minecraft", example = "minecraft", options = ["minecraft", "gta", "rocket-league", "motor-gta"], explanation = "Sets the background for the video" }
background_audio = { optional = true, type = "bool", default = false, example = false, options = [true,
false,
], explaination="Sets a audio to play in the background (put a background.mp3 file in the assets/backgrounds directory for it to be used.)" }
[settings.tts] [settings.tts]
choice = { optional = false, default = "", options = ["streamlabspolly", "tiktok", "googletranslate", "awspolly", ], example = "streamlabspolly", explanation = "The backend used for TTS generation. This can be left blank and you will be prompted to choose at runtime." } choice = { optional = false, default = "", options = ["streamlabspolly", "tiktok", "googletranslate", "awspolly", ], example = "streamlabspolly", explanation = "The backend used for TTS generation. This can be left blank and you will be prompted to choose at runtime." }
aws_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for AWS Polly" } aws_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for AWS Polly" }

@ -4,6 +4,7 @@ import os
import re import re
from os.path import exists from os.path import exists
from typing import Dict, Tuple, Any from typing import Dict, Tuple, Any
import translators as ts import translators as ts
from moviepy.editor import ( from moviepy.editor import (
@ -15,7 +16,7 @@ from moviepy.editor import (
CompositeAudioClip, CompositeAudioClip,
CompositeVideoClip, CompositeVideoClip,
) )
from moviepy.video.io import ffmpeg_tools from moviepy.video.io.ffmpeg_tools import ffmpeg_merge_video_audio, ffmpeg_extract_subclip
from rich.console import Console from rich.console import Console
from utils.cleanup import cleanup from utils.cleanup import cleanup
@ -46,7 +47,10 @@ def name_normalize(name: str) -> str:
else: else:
return name return name
def make_final_video(number_of_clips: int, length: int, reddit_obj: dict, background_config: Tuple[str, str, str, Any]):
def make_final_video(
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'
@ -66,9 +70,7 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict, backgr
) )
# Gather all audio clips # Gather all audio clips
audio_clips = [ audio_clips = [AudioFileClip(f"assets/temp/mp3/{i}.mp3") for i in range(number_of_clips)]
AudioFileClip(f"assets/temp/mp3/{i}.mp3") for i in range(number_of_clips)
]
audio_clips.insert(0, AudioFileClip("assets/temp/mp3/title.mp3")) audio_clips.insert(0, AudioFileClip("assets/temp/mp3/title.mp3"))
audio_concat = concatenate_audioclips(audio_clips) audio_concat = concatenate_audioclips(audio_clips)
audio_composite = CompositeAudioClip([audio_concat]) audio_composite = CompositeAudioClip([audio_concat])
@ -105,8 +107,7 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict, backgr
# ) # )
# 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_concat = concatenate_videoclips(image_clips).set_position(img_clip_pos)
image_clips).set_position(img_clip_pos)
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"])
@ -129,14 +130,30 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict, backgr
verbose=False, verbose=False,
threads=multiprocessing.cpu_count(), threads=multiprocessing.cpu_count(),
) )
ffmpeg_tools.ffmpeg_extract_subclip( if settings.config["settings"]["background_audio"]: # background.mp3
"assets/temp/temp.mp4", if not exists(f"assets/mp3/background.mp3"):
0, print_substep("optional background audio file didn't so skipping.")
final.duration, ffmpeg_extract_subclip(
targetname=f"results/{subreddit}/{filename}", "assets/temp/temp.mp4",
) 0,
# os.remove("assets/temp/temp.mp4") final.duration,
targetname=f"results/{subreddit}/{filename}",
)
else:
ffmpeg_merge_video_audio("assets/temp/temp.mp4", "assets/backgrounds/background.mp3", "assets/temp/temp_audio.mp4")
ffmpeg_extract_subclip(
"assets/temp/temp_audio.mp4",
0,
final.duration,
targetname=f"results/{subreddit}/{filename}",
)
else:
ffmpeg_extract_subclip(
"assets/temp/temp.mp4",
0,
final.duration,
targetname=f"results/{subreddit}/{filename}",
)
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 🗑")

Loading…
Cancel
Save