added delays defore/after TTS, before first picture and end delay

pull/933/head
Drugsosos 3 years ago
parent c08afd8e6a
commit 9ffbce78d2
No known key found for this signature in database
GPG Key ID: 8E35176FE617E28D

@ -26,26 +26,26 @@ class BaseApiTTS:
async def write_file( async def write_file(
self, self,
output_text: str, output_text: str,
filename: str, filepath: str,
) -> None: ) -> None:
decoded_text = base64.b64decode(output_text) if self.decode_base64 else output_text decoded_text = base64.b64decode(output_text) if self.decode_base64 else output_text
async with open(filename, 'wb') as out: async with open(filepath, 'wb') as out:
await out.write(decoded_text) await out.write(decoded_text)
async def run( async def run(
self, self,
req_text: str, text: str,
filename: str, filepath: str,
) -> None: ) -> None:
output_text = '' output_text = ''
if len(req_text) > self.max_chars: if len(text) > self.max_chars:
for part in self.text_len_sanitize(req_text, self.max_chars): for part in self.text_len_sanitize(text, self.max_chars):
if part: if part:
output_text += await self.make_request(part) output_text += await self.make_request(part)
else: else:
output_text = await self.make_request(req_text) output_text = await self.make_request(text)
await self.write_file(output_text, filename) await self.write_file(output_text, filepath)
def get_random_voice( def get_random_voice(

@ -17,6 +17,7 @@ from moviepy.editor import (
) )
from moviepy.video.io.ffmpeg_tools import ffmpeg_merge_video_audio, ffmpeg_extract_subclip 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 rich.progress import track
from utils.cleanup import cleanup from utils.cleanup import cleanup
from utils.console import print_step, print_substep from utils.console import print_step, print_substep
@ -28,6 +29,12 @@ console = Console()
W, H = 1080, 1920 # TODO move to config W, H = 1080, 1920 # TODO move to config
max_length: int = 50 # TODO move to config
time_before_first_picture: float = 1 # TODO move to config
time_before_tts: float = 0.5 # TODO move to config
time_between_pictures: float = 1 # TODO move to config
delay_before_end: int = 1
def name_normalize( def name_normalize(
name: str name: str
@ -67,18 +74,73 @@ def make_final_video(
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']
final_length = 0 def create_audio_clip(
clip_title: str | int,
clip_start: float,
) -> 'AudioFileClip':
return (
AudioFileClip(f'assets/audio/{clip_title}.mp3')
.set_start(clip_start)
)
video_duration = 0
# Gather all audio clips # Gather all audio clips
audio_clips = [AudioFileClip(f'assets/temp/mp3/{i}.mp3') for i in indexes_of_clips] audio_clips = list()
audio_clips.insert(0, AudioFileClip('assets/temp/mp3/title.mp3')) correct_audio_offset = time_before_tts * 2 + time_between_pictures
audio_title = create_audio_clip(
'title',
time_before_first_picture + time_before_tts,
)
video_duration += audio_title.duration + time_before_first_picture + time_before_tts
audio_clips.append(audio_title)
indexes_for_videos = list()
for audio in track(
indexes_of_clips,
description='Gathering audio clips...',
):
temp_audio_clip = create_audio_clip(
audio,
correct_audio_offset + video_duration,
)
if video_duration + temp_audio_clip.duration + correct_audio_offset + delay_before_end > max_length:
continue
video_duration += temp_audio_clip.duration + correct_audio_offset
audio_clips.append(temp_audio_clip)
indexes_for_videos.append(audio)
video_duration += delay_before_end
for idx in indexes_of_clips:
audio_clips.append(AudioFileClip(f'assets/temp/mp3/{idx}.mp3'))
audio_composite = concatenate_audioclips(audio_clips) audio_composite = concatenate_audioclips(audio_clips)
console.log(f'[bold green] Video Will Be: {audio_composite.length} Seconds Long') console.log(f'[bold green] Video Will Be: {audio_composite.length} Seconds Long')
# add title to video # add title to video
image_clips = [] image_clips = list()
# 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) # TODO move to pydentic and percents
def create_image_clip(
self,
image_title: str | int,
audio_start: float,
audio_end: float,
audio_duration: float,
) -> 'ImageClip':
return (
ImageClip(f'assets/temp/png/{image_title}.png')
.set_start(audio_start - self.time_before_tts)
.set_end(audio_end + self.time_before_tts)
.set_duration(self.time_before_tts * 2 + audio_duration, change_end=False)
.set_opacity(new_opacity)
.resize(width=W - 100)
)
index_offset = 1
image_clips.insert( image_clips.insert(
0, 0,
ImageClip('assets/temp/png/title.png') ImageClip('assets/temp/png/title.png')
@ -87,12 +149,14 @@ def make_final_video(
.set_opacity(new_opacity), .set_opacity(new_opacity),
) )
for i in indexes_of_clips: for photo_idx in indexes_of_clips:
image_clips.append( image_clips.append(
ImageClip(f'assets/temp/png/comment_{i}.png') create_image_clip(
.set_duration(audio_clips[i + 1].duration) f'comment_{photo_idx}',
.resize(width=W - 100) audio_clips[photo_idx + index_offset].start,
.set_opacity(new_opacity) audio_clips[photo_idx + index_offset].end,
audio_clips[photo_idx + index_offset].duration
)
) )
# if os.path.exists("assets/mp3/posttext.mp3"): # if os.path.exists("assets/mp3/posttext.mp3"):
@ -113,7 +177,7 @@ def make_final_video(
background_clip = ( background_clip = (
VideoFileClip('assets/temp/background.mp4') VideoFileClip('assets/temp/background.mp4')
.set_start(0) .set_start(0)
.set_end() .set_end(video_duration + delay_before_end)
.without_audio() .without_audio()
.resize(height=H) .resize(height=H)
) )
@ -171,7 +235,7 @@ def make_final_video(
ffmpeg_extract_subclip( ffmpeg_extract_subclip(
'assets/temp/temp.mp4', 'assets/temp/temp.mp4',
0, 0,
final.duration, video_duration,
targetname=f'results/{subreddit}/{filename}', targetname=f'results/{subreddit}/{filename}',
) )
else: else:
@ -183,7 +247,7 @@ def make_final_video(
ffmpeg_extract_subclip( # check if this gets run ffmpeg_extract_subclip( # check if this gets run
'assets/temp/temp_audio.mp4', 'assets/temp/temp_audio.mp4',
0, 0,
final.duration, video_duration,
targetname=f"results/{subreddit}/{filename}", targetname=f"results/{subreddit}/{filename}",
) )
else: else:
@ -191,7 +255,7 @@ def make_final_video(
ffmpeg_extract_subclip( ffmpeg_extract_subclip(
'assets/temp/temp.mp4', 'assets/temp/temp.mp4',
0, 0,
final.duration, video_duration,
targetname=f'results/{subreddit}/{filename}', targetname=f'results/{subreddit}/{filename}',
) )
print_step('Removing temporary files 🗑') print_step('Removing temporary files 🗑')

@ -214,8 +214,8 @@ class RedditScreenshot(Browser, Wait):
reddit_object: dict reddit_object: dict
screenshot_idx: list = attrib() screenshot_idx: list = attrib()
@screenshot_num.validator @screenshot_idx.validator
def validate_screenshot_num(self, attribute, value): def validate_screenshot_idx(self, attribute, value):
if value <= 0: if value <= 0:
raise ValueError('Check screenshot_num in config') raise ValueError('Check screenshot_num in config')

Loading…
Cancel
Save