fixes in async code & other fixes

pull/963/head
Drugsosos 2 years ago
parent ed279b1ea1
commit b4f6fe41ca
No known key found for this signature in database
GPG Key ID: 8E35176FE617E28D

@ -73,7 +73,7 @@ def make_final_video(
clip_start: float, clip_start: float,
) -> 'AudioFileClip': ) -> 'AudioFileClip':
return ( return (
AudioFileClip(f'assets/audio/{clip_title}.mp3') AudioFileClip(f'assets/temp/mp3/{clip_title}.mp3')
.set_start(clip_start) .set_start(clip_start)
) )
@ -90,32 +90,27 @@ def make_final_video(
audio_clips.append(audio_title) audio_clips.append(audio_title)
indexes_for_videos = list() indexes_for_videos = list()
for audio in track( for idx, audio in track(
indexes_of_clips, enumerate(indexes_of_clips),
description='Gathering audio clips...', description='Gathering audio clips...',
): ):
temp_audio_clip = create_audio_clip( temp_audio_clip = create_audio_clip(
audio, audio,
video_duration, video_duration,
) )
if video_duration + temp_audio_clip.duration > max_length: if video_duration + temp_audio_clip.duration <= max_length:
continue
video_duration += temp_audio_clip.duration video_duration += temp_audio_clip.duration
audio_clips.append(temp_audio_clip) audio_clips.append(temp_audio_clip)
indexes_for_videos.append(audio) indexes_for_videos.append(idx)
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: {video_duration} Seconds Long')
# add title to video
image_clips = list()
# Gather all images # Gather all images
new_opacity = 1 if opacity is None or float(opacity) >= 1 else float(opacity) # TODO move to pydentic and percents new_opacity = 1 if opacity is None or float(opacity) >= 1 else float(opacity) # TODO move to pydentic and percents
def create_image_clip( def create_image_clip(
self,
image_title: str | int, image_title: str | int,
audio_start: float, audio_start: float,
audio_end: float, audio_end: float,
@ -123,30 +118,32 @@ def make_final_video(
) -> 'ImageClip': ) -> 'ImageClip':
return ( return (
ImageClip(f'assets/temp/png/{image_title}.png') ImageClip(f'assets/temp/png/{image_title}.png')
.set_start(audio_start - self.time_before_tts) .set_start(audio_start)
.set_end(audio_end + self.time_before_tts) .set_end(audio_end)
.set_duration(self.time_before_tts * 2 + audio_duration, change_end=False) .set_duration(audio_duration, change_end=False)
.set_opacity(new_opacity) .set_opacity(new_opacity)
.resize(width=W - 100) .resize(width=W - 100)
) )
index_offset = 1 # add title to video
image_clips = list()
image_clips.insert( image_clips.append(
0, create_image_clip(
ImageClip('assets/temp/png/title.png') 'title',
.set_duration(audio_clips[0].duration) audio_clips[0].start,
.resize(width=W - 100) audio_clips[0].end,
.set_opacity(new_opacity), audio_clips[0].duration
)
) )
for photo_idx in indexes_of_clips: for photo_idx in indexes_for_videos:
image_clips.append( image_clips.append(
create_image_clip( create_image_clip(
f'comment_{photo_idx}', f'comment_{photo_idx}',
audio_clips[photo_idx + index_offset].start, audio_clips[photo_idx].start,
audio_clips[photo_idx + index_offset].end, audio_clips[photo_idx].end,
audio_clips[photo_idx + index_offset].duration audio_clips[photo_idx].duration
) )
) )
@ -218,7 +215,7 @@ def make_final_video(
'assets/temp/temp_audio.mp4', 'assets/temp/temp_audio.mp4',
0, 0,
video_duration, video_duration,
targetname=f"results/{subreddit}/{filename}", targetname=f'results/{subreddit}/{filename}',
) )
else: else:
print('debug duck') print('debug duck')

@ -18,7 +18,6 @@ from attr import attrs, attrib
from attr.validators import instance_of, optional from attr.validators import instance_of, optional
from typing import TypeVar, Optional, Callable, Union from typing import TypeVar, Optional, Callable, Union
_function = TypeVar('_function', bound=Callable[..., object]) _function = TypeVar('_function', bound=Callable[..., object])
_exceptions = TypeVar('_exceptions', bound=Optional[Union[type, tuple, list]]) _exceptions = TypeVar('_exceptions', bound=Optional[Union[type, tuple, list]])
@ -212,12 +211,7 @@ class RedditScreenshot(Browser, Wait):
screenshot_idx (int): List with indexes of voiced comments screenshot_idx (int): List with indexes of voiced comments
""" """
reddit_object: dict reddit_object: dict
screenshot_idx: list = attrib() screenshot_idx: list
@screenshot_idx.validator
def validate_screenshot_idx(self, attribute, value):
if value <= 0:
raise ValueError('Check screenshot_num in config')
async def __dark_theme( async def __dark_theme(
self, self,
@ -352,12 +346,21 @@ class RedditScreenshot(Browser, Wait):
self.screenshot_idx self.screenshot_idx
] ]
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
for idx, tasks in enumerate(
[chunk for chunk in chunks(async_tasks_primary, 15)],
start=1,
):
for task in track( for task in track(
as_completed(async_tasks_primary), as_completed(tasks),
description='Downloading screenshots...', description=f'Downloading comments: Chunk {idx}',
total=async_tasks_primary.__len__(), total=tasks.__len__()
): ):
await task await task
print_substep('Screenshots downloaded Successfully.', style='bold green') print_substep('Comments downloaded Successfully.', style='bold green')
await self.close_browser() await self.close_browser()

@ -37,13 +37,22 @@ async def save_text_to_mp3(
if voice.casefold() in map(lambda _: _.casefold(), TTSProviders): if voice.casefold() in map(lambda _: _.casefold(), TTSProviders):
break break
print('Unknown Choice') print('Unknown Choice')
engine_instance = TTSEngine(get_case_insensitive_key_value(TTSProviders, voice), reddit_obj) TTS_instance = get_case_insensitive_key_value(TTSProviders, voice)
return await engine_instance.run() if TTS_instance == StreamlabsPolly or TTS_instance == TikTok:
from aiohttp import ClientSession
async with ClientSession() as client:
engine_instance = TTSEngine(TTS_instance(client), reddit_obj)
results = await engine_instance.run()
else:
engine_instance = TTSEngine(TTS_instance, reddit_obj)
results = await engine_instance.run()
return results
def get_case_insensitive_key_value( def get_case_insensitive_key_value(
input_dict, input_dict,
key key,
) -> object: ) -> object:
return next( return next(
(value for dict_key, value in input_dict.items() if dict_key.lower() == key.lower()), (value for dict_key, value in input_dict.items() if dict_key.lower() == key.lower()),

Loading…
Cancel
Save