diff --git a/main.py b/main.py index b2d4449..183dd4c 100755 --- a/main.py +++ b/main.py @@ -33,7 +33,9 @@ print_markdown( print_step(f"You are using V{VERSION} of the bot") -async def main(POST_ID=None): +async def main( + POST_ID=None +): cleanup() reddit_object = get_subreddit_threads(POST_ID) comments_created = await save_text_to_mp3(reddit_object) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index b64a52a..2ce80ce 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -9,7 +9,9 @@ from utils.subreddit import get_subreddit_undone from utils.videos import check_done -def get_subreddit_threads(POST_ID: str): +def get_subreddit_threads( + POST_ID: str +): """ Returns a list of threads from the AskReddit subreddit. """ @@ -87,6 +89,7 @@ def get_subreddit_threads(POST_ID: str): content["thread_title"] = submission.title content["thread_post"] = submission.selftext content["thread_id"] = submission.id + content["is_nsfw"] = 'nsfw' in submission.whitelist_status content["comments"] = [] for top_level_comment in submission.comments: diff --git a/video_creation/final_video.py b/video_creation/final_video.py index c4580e9..a363718 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -28,7 +28,12 @@ from video_creation.background import download_background, chop_background_video console = Console() 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( @@ -82,12 +87,13 @@ def make_final_video( # Gather all audio clips audio_clips = list() + correct_audio_offset = time_before_tts * 2 + time_between_pictures audio_title = create_audio_clip( 'title', - 0, + time_before_first_picture + time_before_tts, ) - video_duration += audio_title.duration + video_duration += audio_title.duration + time_before_first_picture + time_before_tts audio_clips.append(audio_title) indexes_for_videos = list() @@ -97,16 +103,18 @@ def make_final_video( ): temp_audio_clip = create_audio_clip( idx, - video_duration, + correct_audio_offset + video_duration, ) - if video_duration + temp_audio_clip.duration <= max_length: - video_duration += temp_audio_clip.duration + if video_duration + temp_audio_clip.duration + correct_audio_offset + delay_before_end <= max_length: + video_duration += temp_audio_clip.duration + correct_audio_offset audio_clips.append(temp_audio_clip) indexes_for_videos.append(idx) + video_duration += delay_before_end + audio_composite = concatenate_audioclips(audio_clips) - console.log('[bold green] Video Will Be: %.2f Seconds Long' % audio_composite.end) + console.log('[bold green] Video Will Be: %.2f Seconds Long' % video_duration) # Gather all images new_opacity = 1 if opacity is None or float(opacity) >= 1 else float(opacity) # TODO move to pydentic and percents @@ -118,9 +126,9 @@ def make_final_video( ) -> 'ImageClip': return ( ImageClip(f'assets/temp/png/{image_title}.png') - .set_start(audio_start) - .set_end(audio_end) - .set_duration(audio_duration, change_end=False) + .set_start(audio_start - time_before_tts) + .set_end(audio_end + time_before_tts) + .set_duration(time_before_tts * 2 + audio_duration, change_end=False) .set_opacity(new_opacity) .resize(width=W - 100) ) @@ -166,12 +174,34 @@ def make_final_video( background_clip = ( VideoFileClip('assets/temp/background.mp4') .set_start(0) - .set_end(video_duration) + .set_end(video_duration + delay_before_end) .without_audio() .resize(height=H) - .crop(x1=1166.6, y1=0, x2=2246.6, y2=1920) ) + back_video_width, back_video_height = background_clip.size + + # Fix for crop with vertical videos + if back_video_width < H: + background_clip = ( + background_clip + .resize(width=W) + ) + back_video_width, back_video_height = background_clip.size + background_clip = background_clip.crop( + x1=0, + x2=back_video_width, + y1=back_video_height / 2 - H / 2, + y2=back_video_height / 2 + H / 2 + ) + else: + background_clip = background_clip.crop( + x1=back_video_width / 2 - W / 2, + x2=back_video_width / 2 + W / 2, + y1=0, + y2=back_video_height + ) + final = CompositeVideoClip([background_clip, image_concat]) title = re.sub(r'[^\w\s-]', '', reddit_obj['thread_title']) idx = re.sub(r'[^\w\s-]', '', reddit_obj['thread_id'])