diff --git a/.gitignore b/.gitignore index 3da725f..5f4115f 100644 --- a/.gitignore +++ b/.gitignore @@ -232,6 +232,7 @@ fabric.properties .idea/caches/build_file_checksums.ser assets/ +/.vscode out .DS_Store .setup-done-before @@ -243,4 +244,4 @@ video_creation/data/videos.json video_creation/data/envvars.txt config.toml -video_creation/data/videos.json +video_creation/data/videos.json \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 1f68ea0..6d090c6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/playwright +FROM python:3.10.9-slim RUN apt update RUN apt install python3-pip -y diff --git a/GUI/settings.html b/GUI/settings.html index be467bd..1f0ef2e 100644 --- a/GUI/settings.html +++ b/GUI/settings.html @@ -213,6 +213,38 @@ backgrounds +
TTS Settings
diff --git a/README.md b/README.md index 9095588..4997d3f 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ The only original thing being done is the editing and gathering of all materials ## Requirements -- Python 3.9+ +- Python 3.10 - Playwright (this should install automatically in installation) ## Installation 👩💻 @@ -70,6 +70,7 @@ In its current state, this bot does exactly what it needs to do. However, improv I have tried to simplify the code so anyone can read it and start contributing at any skill level. Don't be shy :) contribute! - [ ] Creating better documentation and adding a command line interface. +- [ ] Allowing the user to choose background music for their videos. - [x] Allowing users to choose a reddit thread instead of being randomized. - [x] Allowing users to choose a background that is picked instead of the Minecraft one. - [x] Allowing users to choose between any subreddit. @@ -86,9 +87,11 @@ Elebumm (Lewis#6305) - https://github.com/elebumm (Founder) Jason (JasonLovesDoggo#1904) - https://github.com/JasonLovesDoggo (Maintainer) +Simon (OpenSourceSimon) - https://github.com/OpenSourceSimon + CallumIO (c.#6837) - https://github.com/CallumIO -Verq (Verq#2338) - https://github.com/CordlessCoder +Verq (Verq#2338) - https://github.com/CordlessCoder LukaHietala (Pix.#0001) - https://github.com/LukaHietala diff --git a/TTS/GTTS.py b/TTS/GTTS.py index 3bf8ee3..bff100f 100644 --- a/TTS/GTTS.py +++ b/TTS/GTTS.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 import random from gtts import gTTS diff --git a/TTS/TikTok.py b/TTS/TikTok.py index b9bc1a8..543568f 100644 --- a/TTS/TikTok.py +++ b/TTS/TikTok.py @@ -78,18 +78,15 @@ vocals: Final[tuple] = ( class TikTok: """TikTok Text-to-Speech Wrapper""" - - max_chars: Final[int] = 300 - BASE_URL: Final[ - str - ] = "https://api16-normal-c-useast1a.tiktokv.com/media/api/text/speech/invoke/" - def __init__(self): headers = { "User-Agent": "com.zhiliaoapp.musically/2022600030 (Linux; U; Android 7.1.2; es_ES; SM-G988N; " "Build/NRD90M;tt-ok/3.12.13.1)", "Cookie": f"sessionid={settings.config['settings']['tts']['tiktok_sessionid']}", } + + self.URI_BASE = "https://api16-normal-c-useast1a.tiktokv.com/media/api/text/speech/invoke/" + self.max_chars = 300 self._session = requests.Session() # set the headers to the session, so we don't have to do it for every request @@ -131,10 +128,10 @@ class TikTok: # send request try: - response = self._session.post(self.BASE_URL, params=params) + response = self._session.post(self.URI_BASE, params=params) except ConnectionError: time.sleep(random.randrange(1, 7)) - response = self._session.post(self.BASE_URL, params=params) + response = self._session.post(self.URI_BASE, params=params) return response.json() diff --git a/TTS/aws_polly.py b/TTS/aws_polly.py index fa02079..58323f9 100644 --- a/TTS/aws_polly.py +++ b/TTS/aws_polly.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 import random import sys @@ -39,11 +38,17 @@ class AWSPolly: voice = self.randomvoice() else: if not settings.config["settings"]["tts"]["aws_polly_voice"]: - raise ValueError(f"Please set the TOML variable AWS_VOICE to a valid voice. options are: {voices}") - voice = str(settings.config["settings"]["tts"]["aws_polly_voice"]).capitalize() + raise ValueError( + f"Please set the TOML variable AWS_VOICE to a valid voice. options are: {voices}" + ) + voice = str( + settings.config["settings"]["tts"]["aws_polly_voice"] + ).capitalize() try: # Request speech synthesis - response = polly.synthesize_speech(Text=text, OutputFormat="mp3", VoiceId=voice, Engine="neural") + response = polly.synthesize_speech( + Text=text, OutputFormat="mp3", VoiceId=voice, Engine="neural" + ) except (BotoCoreError, ClientError) as error: # The service returned an error, exit gracefully print(error) diff --git a/TTS/engine_wrapper.py b/TTS/engine_wrapper.py index 1324118..6ca63d5 100644 --- a/TTS/engine_wrapper.py +++ b/TTS/engine_wrapper.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 import os import re from pathlib import Path @@ -18,7 +17,7 @@ from utils import settings from utils.console import print_step, print_substep from utils.voice import sanitize_text -DEFAULT_MAX_LENGTH: int = 50 # video length variable +DEFAULT_MAX_LENGTH: int = 50 # video length variable class TTSEngine: @@ -45,6 +44,7 @@ class TTSEngine: ): self.tts_module = tts_module() self.reddit_object = reddit_object + self.redditid = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"]) self.path = path + self.redditid + "/mp3" self.max_length = max_length @@ -54,39 +54,52 @@ class TTSEngine: def run(self) -> Tuple[int, int]: Path(self.path).mkdir(parents=True, exist_ok=True) - - # This file needs to be removed in case this post does not use post text, so that it won't appear in the final video - try: - Path(f"{self.path}/posttext.mp3").unlink() - except OSError: - pass - print_step("Saving Text to MP3 files...") self.call_tts("title", process_text(self.reddit_object["thread_title"])) - processed_text = process_text(self.reddit_object["thread_post"]) - if processed_text != "" and settings.config["settings"]["storymode"] == True: - self.call_tts("posttext", processed_text) - + # processed_text = ##self.reddit_object["thread_post"] != "" idx = None - for idx, comment in track(enumerate(self.reddit_object["comments"]), "Saving..."): - # ! Stop creating mp3 files if the length is greater than max length. - if self.length > self.max_length: - self.length -= self.last_clip_length - idx -= 1 - break - if len(comment["comment_body"]) > self.tts_module.max_chars: # Split the comment if it is too long - self.split_post(comment["comment_body"], idx) # Split the comment - else: # If the comment is not too long, just call the tts engine - self.call_tts(f"{idx}", process_text(comment["comment_body"])) + + if settings.config["settings"]["storymode"]: + if settings.config["settings"]["storymodemethod"] == 0: + if len(self.reddit_object["thread_post"]) > self.tts_module.max_chars: + self.split_post(self.reddit_object["thread_post"], "postaudio") + else: + self.call_tts( + "postaudio", process_text(self.reddit_object["thread_post"]) + ) + elif settings.config["settings"]["storymodemethod"] == 1: + + for idx, text in track(enumerate(self.reddit_object["thread_post"])): + self.call_tts(f"postaudio-{idx}", process_text(text)) + + else: + + for idx, comment in track( + enumerate(self.reddit_object["comments"]), "Saving..." + ): + # ! Stop creating mp3 files if the length is greater than max length. + if self.length > self.max_length and idx > 1: + self.length -= self.last_clip_length + idx -= 1 + break + if ( + len(comment["comment_body"]) > self.tts_module.max_chars + ): # Split the comment if it is too long + self.split_post(comment["comment_body"], idx) # Split the comment + else: # If the comment is not too long, just call the tts engine + self.call_tts(f"{idx}", process_text(comment["comment_body"])) print_substep("Saved Text to MP3 files successfully.", style="bold green") return self.length, idx - def split_post(self, text: str, idx: int): + def split_post(self, text: str, idx): split_files = [] split_text = [ - x.group().strip() for x in re.finditer(r" *(((.|\n){0," + str(self.tts_module.max_chars) + "})(\.|.$))", text) + x.group().strip() + for x in re.finditer( + r" *(((.|\n){0," + str(self.tts_module.max_chars) + "})(\.|.$))", text + ) ] self.create_silence_mp3() @@ -100,15 +113,19 @@ class TTSEngine: continue else: self.call_tts(f"{idx}-{idy}.part", newtext) - with open(f"{self.path}/list.txt", 'w') as f: + with open(f"{self.path}/list.txt", "w") as f: for idz in range(0, len(split_text)): f.write("file " + f"'{idx}-{idz}.part.mp3'" + "\n") split_files.append(str(f"{self.path}/{idx}-{idy}.part.mp3")) f.write("file " + f"'silence.mp3'" + "\n") - os.system("ffmpeg -f concat -y -hide_banner -loglevel panic -safe 0 " + - "-i " + f"{self.path}/list.txt " + - "-c copy " + f"{self.path}/{idx}.mp3") + os.system( + "ffmpeg -f concat -y -hide_banner -loglevel panic -safe 0 " + + "-i " + + f"{self.path}/list.txt " + + "-c copy " + + f"{self.path}/{idx}.mp3" + ) try: for i in range(0, len(split_files)): os.unlink(split_files[i]) @@ -118,43 +135,35 @@ class TTSEngine: print("OSError") def call_tts(self, filename: str, text: str): - + self.tts_module.run(text, filepath=f"{self.path}/{filename}.mp3") + # try: + # self.length += MP3(f"{self.path}/{filename}.mp3").info.length + # except (MutagenError, HeaderNotFoundError): + # self.length += sox.file_info.duration(f"{self.path}/{filename}.mp3") try: - self.tts_module.run(text, filepath=f"{self.path}/{filename}_no_silence.mp3") - self.create_silence_mp3() - - with open(f"{self.path}/{filename}.txt", 'w') as f: - f.write("file " + f"'{filename}_no_silence.mp3'" + "\n") - f.write("file " + f"'silence.mp3'" + "\n") - f.close() - os.system("ffmpeg -f concat -y -hide_banner -loglevel panic -safe 0 " + - "-i " + f"{self.path}/{filename}.txt " + - "-c copy " + f"{self.path}/{filename}.mp3") clip = AudioFileClip(f"{self.path}/{filename}.mp3") - self.length += clip.duration self.last_clip_length = clip.duration + self.length += clip.duration clip.close() - try: - name = [f"{filename}_no_silence.mp3", "silence.mp3", f"{filename}.txt"] - for i in range(0, len(name)): - os.unlink(str(rf"{self.path}/" + name[i])) - except FileNotFoundError as e: - print("File not found: " + e.filename) - except OSError: - print("OSError") except: self.length = 0 def create_silence_mp3(self): silence_duration = settings.config["settings"]["tts"]["silence_duration"] - silence = AudioClip(make_frame=lambda t: np.sin(440 * 2 * np.pi * t), duration=silence_duration, fps=44100) + silence = AudioClip( + make_frame=lambda t: np.sin(440 * 2 * np.pi * t), + duration=silence_duration, + fps=44100, + ) silence = volumex(silence, 0) - silence.write_audiofile(f"{self.path}/silence.mp3", fps=44100, verbose=False, logger=None) + silence.write_audiofile( + f"{self.path}/silence.mp3", fps=44100, verbose=False, logger=None + ) -def process_text(text: str): +def process_text(text: str , clean : bool = True): lang = settings.config["reddit"]["thread"]["post_lang"] - new_text = sanitize_text(text) + new_text = sanitize_text(text) if clean else text if lang: print_substep("Translating Text...") translated_text = ts.google(text, to_language=lang) diff --git a/TTS/pyttsx.py b/TTS/pyttsx.py index 874d573..a80bf2d 100644 --- a/TTS/pyttsx.py +++ b/TTS/pyttsx.py @@ -21,7 +21,9 @@ class pyttsx: if voice_id == "" or voice_num == "": voice_id = 2 voice_num = 3 - raise ValueError("set pyttsx values to a valid value, switching to defaults") + raise ValueError( + "set pyttsx values to a valid value, switching to defaults" + ) else: voice_id = int(voice_id) voice_num = int(voice_num) @@ -32,7 +34,9 @@ class pyttsx: voice_id = self.randomvoice() engine = pyttsx3.init() voices = engine.getProperty("voices") - engine.setProperty("voice", voices[voice_id].id) # changing index changes voices but ony 0 and 1 are working here + engine.setProperty( + "voice", voices[voice_id].id + ) # changing index changes voices but ony 0 and 1 are working here engine.save_to_file(text, f"{filepath}") engine.runAndWait() diff --git a/TTS/streamlabs_polly.py b/TTS/streamlabs_polly.py index ce2250b..721dd78 100644 --- a/TTS/streamlabs_polly.py +++ b/TTS/streamlabs_polly.py @@ -39,8 +39,12 @@ class StreamlabsPolly: voice = self.randomvoice() else: if not settings.config["settings"]["tts"]["streamlabs_polly_voice"]: - raise ValueError(f"Please set the config variable STREAMLABS_POLLY_VOICE to a valid voice. options are: {voices}") - voice = str(settings.config["settings"]["tts"]["streamlabs_polly_voice"]).capitalize() + raise ValueError( + f"Please set the config variable STREAMLABS_POLLY_VOICE to a valid voice. options are: {voices}" + ) + voice = str( + settings.config["settings"]["tts"]["streamlabs_polly_voice"] + ).capitalize() body = {"voice": voice, "text": text, "service": "polly"} response = requests.post(self.url, data=body) if not check_ratelimit(response): diff --git a/fonts/LICENSE.txt b/fonts/LICENSE.txt new file mode 100644 index 0000000..75b5248 --- /dev/null +++ b/fonts/LICENSE.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/fonts/Roboto-Black.ttf b/fonts/Roboto-Black.ttf new file mode 100644 index 0000000..0112e7d Binary files /dev/null and b/fonts/Roboto-Black.ttf differ diff --git a/fonts/Roboto-Bold.ttf b/fonts/Roboto-Bold.ttf new file mode 100644 index 0000000..43da14d Binary files /dev/null and b/fonts/Roboto-Bold.ttf differ diff --git a/fonts/Roboto-Medium.ttf b/fonts/Roboto-Medium.ttf new file mode 100644 index 0000000..ac0f908 Binary files /dev/null and b/fonts/Roboto-Medium.ttf differ diff --git a/fonts/Roboto-Regular.ttf b/fonts/Roboto-Regular.ttf new file mode 100644 index 0000000..ddf4bfa Binary files /dev/null and b/fonts/Roboto-Regular.ttf differ diff --git a/main.py b/main.py index 02964fc..bb479d5 100755 --- a/main.py +++ b/main.py @@ -1,10 +1,11 @@ #!/usr/bin/env python import math import sys +from logging import error from os import name +from pathlib import Path from subprocess import Popen -from pathlib import Path from prawcore import ResponseException from reddit.subreddit import get_subreddit_threads @@ -19,10 +20,10 @@ from video_creation.background import ( get_background_config, ) from video_creation.final_video import make_final_video -from video_creation.screenshot_downloader import download_screenshots_of_reddit_posts +from video_creation.screenshot_downloader import get_screenshots_of_reddit_posts from video_creation.voices import save_text_to_mp3 -__VERSION__ = "2.4.2" +__VERSION__ = "2.5.0" print( """ @@ -42,12 +43,12 @@ checkversion(__VERSION__) def main(POST_ID=None): + global redditid ,reddit_object reddit_object = get_subreddit_threads(POST_ID) - global redditid redditid = id(reddit_object) length, number_of_comments = save_text_to_mp3(reddit_object) length = math.ceil(length) - download_screenshots_of_reddit_posts(reddit_object, number_of_comments) + get_screenshots_of_reddit_posts(reddit_object, number_of_comments) bg_config = get_background_config() download_background(bg_config) chop_background_video(bg_config, length, reddit_object) @@ -64,13 +65,13 @@ def run_many(times): def shutdown(): - print_markdown("## Clearing temp files") try: redditid except NameError: print("Exiting...") exit() else: + print_markdown("## Clearing temp files") cleanup(redditid) print("Exiting...") exit() @@ -79,11 +80,15 @@ def shutdown(): if __name__ == "__main__": assert sys.version_info >= (3, 9), "Python 3.10 or higher is required" directory = Path().absolute() - config = settings.check_toml(f"{directory}/utils/.config.template.toml", "config.toml") + config = settings.check_toml( + f"{directory}/utils/.config.template.toml", "config.toml" + ) config is False and exit() try: - if len(config["reddit"]["thread"]["post_id"].split("+")) > 1: - for index, post_id in enumerate(config["reddit"]["thread"]["post_id"].split("+")): + if config["reddit"]["thread"]["post_id"] : + for index, post_id in enumerate( + config["reddit"]["thread"]["post_id"].split("+") + ): index += 1 print_step( f'on the {index}{("st" if index % 10 == 1 else ("nd" if index % 10 == 2 else ("rd" if index % 10 == 3 else "th")))} post of {len(config["reddit"]["thread"]["post_id"].split("+"))}' @@ -102,5 +107,12 @@ if __name__ == "__main__": print_markdown("Please check your credentials in the config.toml file") shutdown() - + except Exception as err: + print_step(f''' + Sorry, something went wrong with this test version! Try again, and feel free to report this issue at GitHub or the Discord community.\n + Version: {__VERSION__} \n + Story mode: {str(config["settings"]["storymode"])} \n + Story mode method: {str(config["settings"]["storymodemethod"])} + ''') + raise err # todo error diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 5114d9a..8fb9e9d 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -1,14 +1,18 @@ import re +from prawcore.exceptions import ResponseException + +from utils import settings import praw from praw.models import MoreComments from prawcore.exceptions import ResponseException -from utils import settings from utils.console import print_step, print_substep from utils.subreddit import get_subreddit_undone from utils.videos import check_done from utils.voice import sanitize_text +from utils.posttextparser import posttextparser +from utils.ai_methods import sort_by_similarity def get_subreddit_threads(POST_ID: str): @@ -20,7 +24,9 @@ def get_subreddit_threads(POST_ID: str): content = {} if settings.config["reddit"]["creds"]["2fa"]: - print("\nEnter your two-factor authentication code from your authenticator app.\n") + print( + "\nEnter your two-factor authentication code from your authenticator app.\n" + ) code = input("> ") print() pw = settings.config["reddit"]["creds"]["password"] @@ -47,12 +53,15 @@ def get_subreddit_threads(POST_ID: str): # Ask user for subreddit input print_step("Getting subreddit threads...") + similarity_score = 0 if not settings.config["reddit"]["thread"][ "subreddit" ]: # note to user. you can have multiple subreddits via reddit.subreddit("redditdev+learnpython") try: subreddit = reddit.subreddit( - re.sub(r"r\/", "", input("What subreddit would you like to pull from? ")) + re.sub( + r"r\/", "", input("What subreddit would you like to pull from? ") + ) # removes the r/ from the input ) except ValueError: @@ -62,57 +71,104 @@ def get_subreddit_threads(POST_ID: str): sub = settings.config["reddit"]["thread"]["subreddit"] print_substep(f"Using subreddit: r/{sub} from TOML config") subreddit_choice = sub - if str(subreddit_choice).casefold().startswith("r/"): # removes the r/ from the input + if ( + str(subreddit_choice).casefold().startswith("r/") + ): # removes the r/ from the input subreddit_choice = subreddit_choice[2:] subreddit = reddit.subreddit(subreddit_choice) if POST_ID: # would only be called if there are multiple queued posts submission = reddit.submission(id=POST_ID) + elif ( settings.config["reddit"]["thread"]["post_id"] and len(str(settings.config["reddit"]["thread"]["post_id"]).split("+")) == 1 ): submission = reddit.submission(id=settings.config["reddit"]["thread"]["post_id"]) + elif settings.config["ai"]["ai_similarity_enabled"]: # ai sorting based on comparison + threads = subreddit.hot(limit=50) + keywords = settings.config["ai"]["ai_similarity_keywords"].split(',') + keywords = [keyword.strip() for keyword in keywords] + # Reformat the keywords for printing + keywords_print = ", ".join(keywords) + print(f'Sorting threads by similarity to the given keywords: {keywords_print}') + threads, similarity_scores = sort_by_similarity(threads, keywords) + submission, similarity_score = get_subreddit_undone(threads, subreddit, similarity_scores=similarity_scores) else: threads = subreddit.hot(limit=25) submission = get_subreddit_undone(threads, subreddit) + + if submission is None: + return get_subreddit_threads(POST_ID) # submission already done. rerun + + if settings.config["settings"]["storymode"]: + if not submission.selftext: + print_substep("You are trying to use story mode on post with no post text") + exit() + else: + # Check for the length of the post text + if len(submission.selftext) > (settings.config["settings"]["storymode_max_length"] or 2000): + print_substep( + f"Post is too long ({len(submission.selftext)}), try with a different post. ({settings.config['settings']['storymode_max_length']} character limit)" + ) + exit() + elif not submission.num_comments: + print_substep("No comments found. Skipping.") + exit() + submission = check_done(submission) # double-checking - if submission is None or not submission.num_comments: - return get_subreddit_threads(POST_ID) # submission already done. rerun + upvotes = submission.score ratio = submission.upvote_ratio * 100 num_comments = submission.num_comments + threadurl = f"https://reddit.com{submission.permalink}" print_substep(f"Video will be: {submission.title} :thumbsup:", style="bold green") + print_substep(f"Thread url is : {threadurl } :thumbsup:", style="bold green") print_substep(f"Thread has {upvotes} upvotes", style="bold blue") print_substep(f"Thread has a upvote ratio of {ratio}%", style="bold blue") print_substep(f"Thread has {num_comments} comments", style="bold blue") + if similarity_score: + print_substep(f"Thread has a similarity score up to {round(similarity_score * 100)}%", style="bold blue") - content["thread_url"] = f"https://reddit.com{submission.permalink}" + content["thread_url"] = threadurl content["thread_title"] = submission.title - content["thread_post"] = submission.selftext content["thread_id"] = submission.id content["comments"] = [] - - for top_level_comment in submission.comments: - if isinstance(top_level_comment, MoreComments): - continue - if top_level_comment.body in ["[removed]", "[deleted]"]: - continue # # see https://github.com/JasonLovesDoggo/RedditVideoMakerBot/issues/78 - if not top_level_comment.stickied: - sanitised = sanitize_text(top_level_comment.body) - if not sanitised or sanitised == " ": + if settings.config["settings"]["storymode"]: + if settings.config["settings"]["storymodemethod"] == 1: + content["thread_post"] = posttextparser(submission.selftext) + else: + content["thread_post"] = submission.selftext + else: + for top_level_comment in submission.comments: + if isinstance(top_level_comment, MoreComments): continue - if len(top_level_comment.body) <= int(settings.config["reddit"]["thread"]["max_comment_length"]): - if ( - top_level_comment.author is not None and sanitize_text(top_level_comment.body) is not None - ): # if errors occur with this change to if not. - content["comments"].append( - { - "comment_body": top_level_comment.body, - "comment_url": top_level_comment.permalink, - "comment_id": top_level_comment.id, - } - ) + + if top_level_comment.body in ["[removed]", "[deleted]"]: + continue # # see https://github.com/JasonLovesDoggo/RedditVideoMakerBot/issues/78 + if not top_level_comment.stickied: + sanitised = sanitize_text(top_level_comment.body) + if not sanitised or sanitised == " ": + continue + if len(top_level_comment.body) <= int( + settings.config["reddit"]["thread"]["max_comment_length"] + ): + if len(top_level_comment.body) >= int( + settings.config["reddit"]["thread"]["min_comment_length"] + ): + + if ( + top_level_comment.author is not None + and sanitize_text(top_level_comment.body) is not None + ): # if errors occur with this change to if not. + content["comments"].append( + { + "comment_body": top_level_comment.body, + "comment_url": top_level_comment.permalink, + "comment_id": top_level_comment.id, + } + ) + print_substep("Received subreddit threads Successfully.", style="bold green") return content diff --git a/requirements.txt b/requirements.txt index a2ccadf..3382509 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ botocore==1.27.24 gTTS==2.2.4 moviepy==1.0.3 playwright==1.23.0 -praw==7.6.0 +praw==7.6.1 prawcore~=2.3.0 pytube==12.1.0 requests==2.28.1 @@ -11,6 +11,11 @@ rich==12.5.1 toml==0.10.2 translators==5.3.1 pyttsx3==2.90 -Pillow~=9.1.1 +Pillow~=9.3.0 tomlkit==0.11.4 Flask==2.2.2 +clean-text==0.6.0 +unidecode==1.3.2 +spacy==3.4.1 +torch==1.12.1 +transformers==4.25.1 \ No newline at end of file diff --git a/utils/.config.template.toml b/utils/.config.template.toml index 7374579..ec5bb2a 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -8,12 +8,17 @@ password = { optional = false, nmin = 8, explanation = "The password of your red [reddit.thread] random = { optional = true, options = [true, false, ], default = false, type = "bool", explanation = "If set to no, it will ask you a thread link to extract the thread, if yes it will randomize it. Default: 'False'", example = "True" } -subreddit = { optional = false, regex = "[_0-9a-zA-Z]+$", nmin = 3, explanation = "What subreddit to pull posts from, the name of the sub, not the URL. You can have multiple subreddits, add an + with no spaces.", example = "AskReddit+Redditdev", oob_error = "A subreddit name HAS to be between 3 and 20 characters" } +subreddit = { optional = false, regex = "[_0-9a-zA-Z\\+]+$", nmin = 3, explanation = "What subreddit to pull posts from, the name of the sub, not the URL. You can have multiple subreddits, add an + with no spaces.", example = "AskReddit+Redditdev", oob_error = "A subreddit name HAS to be between 3 and 20 characters" } post_id = { optional = true, default = "", regex = "^((?!://|://)[+a-zA-Z0-9])*$", explanation = "Used if you want to use a specific post.", example = "urdtfx" } max_comment_length = { default = 500, optional = false, nmin = 10, nmax = 10000, type = "int", explanation = "max number of characters a comment can have. default is 500", example = 500, oob_error = "the max comment length should be between 10 and 10000" } -post_lang = { default = "", optional = true, explanation = "The language you would like to translate to.", example = "es-cr" } -min_comments = { default = 20, optional = false, nmin = 15, type = "int", explanation = "The minimum number of comments a post should have to be included. default is 20", example = 29, oob_error = "the minimum number of comments should be between 15 and 999999" } +min_comment_length = { default = 1, optional = true, nmin = 0, nmax = 10000, type = "int", explanation = "min_comment_length number of characters a comment can have. default is 0", example = 50, oob_error = "the max comment length should be between 1 and 100" } +post_lang = { default = "", optional = true, explanation = "The language you would like to translate to.", example = "es-cr", options = ['','af', 'ak', 'am', 'ar', 'as', 'ay', 'az', 'be', 'bg', 'bho', 'bm', 'bn', 'bs', 'ca', 'ceb', 'ckb', 'co', 'cs', 'cy', 'da', 'de', 'doi', 'dv', 'ee', 'el', 'en', 'en-US', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'fy', 'ga', 'gd', 'gl', 'gn', 'gom', 'gu', 'ha', 'haw', 'hi', 'hmn', 'hr', 'ht', 'hu', 'hy', 'id', 'ig', 'ilo', 'is', 'it', 'iw', 'ja', 'jw', 'ka', 'kk', 'km', 'kn', 'ko', 'kri', 'ku', 'ky', 'la', 'lb', 'lg', 'ln', 'lo', 'lt', 'lus', 'lv', 'mai', 'mg', 'mi', 'mk', 'ml', 'mn', 'mni-Mtei', 'mr', 'ms', 'mt', 'my', 'ne', 'nl', 'no', 'nso', 'ny', 'om', 'or', 'pa', 'pl', 'ps', 'pt', 'qu', 'ro', 'ru', 'rw', 'sa', 'sd', 'si', 'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'st', 'su', 'sv', 'sw', 'ta', 'te', 'tg', 'th', 'ti', 'tk', 'tl', 'tr', 'ts', 'tt', 'ug', 'uk', 'ur', 'uz', 'vi', 'xh', 'yi', 'yo', 'zh-CN', 'zh-TW', 'zu'] } +min_comments = { default = 20, optional = false, nmin = 10, type = "int", explanation = "The minimum number of comments a post should have to be included. default is 20", example = 29, oob_error = "the minimum number of comments should be between 15 and 999999" } +#post_url = { optional = true, default = "", regex = "^https:\\/\\/www\\.reddit\\.com\\/r\\/[a-zA-Z0-9]+\\/comments\\/[a-zA-Z0-9]+\\/[a-zA-Z0-9_]+\\/$", explanation = "Not working currently Use if you want to use a specific post.", example = "https://www.reddit.com/r/buildapc/comments/yzh07p/have_you_switched_to_windows_11/" } +[ai] +ai_similarity_enabled = {optional = true, option = [true, false], default = false, type = "bool", explanation = "Threads read from Reddit are sorted based on their similarity to the keywords given below"} +ai_similarity_keywords = {optional = true, type="str", example= 'Elon Musk, Twitter, Stocks', explanation = "Every keyword or even sentence, seperated with comma, is used to sort the reddit threads based on similarity"} [settings] allow_nsfw = { optional = false, type = "bool", default = false, example = false, options = [true, false, ], explanation = "Whether to allow NSFW content, True or False" } @@ -21,14 +26,21 @@ theme = { optional = false, default = "dark", example = "light", options = ["dar times_to_run = { optional = false, default = 1, example = 2, explanation = "Used if you want to run multiple times. Set to an int e.g. 4 or 29 or 1", type = "int", nmin = 1, oob_error = "It's very hard to run something less than once." } 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" } transition = { optional = true, default = 0.2, example = 0.2, explanation = "Sets the transition time (in seconds) between the comments. Set to 0 if you want to disable it.", type = "float", nmin = 0, nmax = 2, oob_error = "The transition HAS to be between 0 and 2", input_error = "The opacity HAS to be a decimal number between 0 and 2" } -storymode = { optional = true, type = "bool", default = false, example = false, options = [true, false, ], explanation = "Only read out title and post content, not yet implemented" } - +storymode = { optional = true, type = "bool", default = false, example = false, options = [true, false,], explanation = "Only read out title and post content, great for subreddits with stories" } +storymodemethod= { optional = true, default = 1, example = 1, explanation = "Style that's used for the storymode. Set to 0 for single picture display in whole video, set to 1 for fancy looking video ", type = "int", nmin = 0, oob_error = "It's very hard to run something less than once.", options = [0, 1] } +storymode_max_length = { optional = true, default = 1000, example = 1000, explanation = "Max length of the storymode video in characters. 200 characters are approximately 50 seconds.", type = "int", nmin = 1, oob_error = "It's very hard to make a video under a second." } +fps = { optional = false, default = 30, example = 30, explanation = "Sets the FPS of the video, 30 is default for best performance. 60 FPS is smoother.", type = "int", nmin = 1, nmax = 60, oob_error = "The FPS HAS to be between 1 and 60" } +resolution_w = { optional = false, default = 1080, example = 1440, explantation = "Sets the width in pixels of the final video" } +resolution_h = { optional = false, default = 1920, example = 2560, explantation = "Sets the height in pixels of the final video" } [settings.background] -background_choice = { optional = true, default = "minecraft", example = "rocket-league", options = ["", "minecraft", "gta", "rocket-league", "motor-gta", "csgo-surf", "cluster-truck"], explanation = "Sets the background for the video based on game name" } +background_choice = { optional = true, default = "minecraft", example = "rocket-league", options = ["minecraft", "gta", "rocket-league", "motor-gta", "csgo-surf", "cluster-truck", "minecraft-2","multiversus","fall-guys","steep", ""], explanation = "Sets the background for the video based on game name" } #background_audio = { optional = true, type = "bool", default = false, example = false, options = [true, false,], explanation = "Sets a audio to play in the background (put a background.mp3 file in the assets/backgrounds directory for it to be used.)" } #background_audio_volume = { optional = true, type = "float", default = 0.3, example = 0.1, explanation="Sets the volume of the background audio. only used if the background_audio is also set to true" } - +background_thumbnail = { optional = true, type = "bool", default = false, example = false, options = [true, false,], explanation = "Generate a thumbnail for the video (put a thumbnail.png file in the assets/backgrounds directory.)" } +background_thumbnail_font_family = { optional = true, default = "arial", example = "arial", explanation = "Font family for the thumbnail text" } +background_thumbnail_font_size = { optional = true, type = "int", default = 96, example = 96, explanation = "Font size in pixels for the thumbnail text" } +background_thumbnail_font_color = { optional = true, default = "255,255,255", example = "255,255,255", explanation = "Font color in RGB format for the thumbnail text" } [settings.tts] voice_choice = { optional = false, default = "tiktok", options = ["streamlabspolly", "tiktok", "googletranslate", "awspolly", "pyttsx", ], example = "tiktok", explanation = "The voice platform used for TTS generation. This can be left blank and you will be prompted to choose at runtime." } @@ -39,3 +51,4 @@ tiktok_sessionid = { optional = true, example = "c76bcc3a7625abcc27b508c7db457ff python_voice = { optional = false, default = "1", example = "1", explanation = "The index of the system tts voices (can be downloaded externally, run ptt.py to find value, start from zero)" } py_voice_num = { optional = false, default = "2", example = "2", explanation = "The number of system voices (2 are pre-installed in Windows)" } silence_duration = { optional = true, example = "0.1", explanation = "Time in seconds between TTS comments", default = 0.3, type = "float" } +no_emojis = { optional = false, type = "bool", default = false, example = false, options = [true, false,], explanation = "Whether to remove emojis from the comments" } \ No newline at end of file diff --git a/utils/ai_methods.py b/utils/ai_methods.py new file mode 100644 index 0000000..244cfff --- /dev/null +++ b/utils/ai_methods.py @@ -0,0 +1,58 @@ +import numpy as np +from transformers import AutoTokenizer, AutoModel +import torch + + +# Mean Pooling - Take attention mask into account for correct averaging +def mean_pooling(model_output, attention_mask): + token_embeddings = model_output[0] # First element of model_output contains all token embeddings + input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float() + return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9) + + +# This function sort the given threads based on their total similarity with the given keywords +def sort_by_similarity(thread_objects, keywords): + # Initialize tokenizer + model. + tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2') + model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2') + + # Transform the generator to a list of Submission Objects, so we can sort later based on context similarity to + # keywords + thread_objects = list(thread_objects) + + threads_sentences = [] + for i, thread in enumerate(thread_objects): + threads_sentences.append(' '.join([thread.title, thread.selftext])) + + # Threads inference + encoded_threads = tokenizer(threads_sentences, padding=True, truncation=True, return_tensors='pt') + with torch.no_grad(): + threads_embeddings = model(**encoded_threads) + threads_embeddings = mean_pooling(threads_embeddings, encoded_threads['attention_mask']) + + # Keywords inference + encoded_keywords = tokenizer(keywords, padding=True, truncation=True, return_tensors='pt') + with torch.no_grad(): + keywords_embeddings = model(**encoded_keywords) + keywords_embeddings = mean_pooling(keywords_embeddings, encoded_keywords['attention_mask']) + + # Compare every keyword w/ every thread embedding + threads_embeddings_tensor = torch.tensor(threads_embeddings) + total_scores = torch.zeros(threads_embeddings_tensor.shape[0]) + cosine_similarity = torch.nn.CosineSimilarity() + for keyword_embedding in keywords_embeddings: + keyword_embedding = torch.tensor(keyword_embedding).repeat(threads_embeddings_tensor.shape[0], 1) + similarity = cosine_similarity(keyword_embedding, threads_embeddings_tensor) + total_scores += similarity + + similarity_scores, indices = torch.sort(total_scores, descending=True) + + threads_sentences = np.array(threads_sentences)[indices.numpy()] + + thread_objects = np.array(thread_objects)[indices.numpy()].tolist() + + #print('Similarity Thread Ranking') + #for i, thread in enumerate(thread_objects): + # print(f'{i}) {threads_sentences[i]} score {similarity_scores[i]}') + + return thread_objects, similarity_scores diff --git a/utils/backgrounds.json b/utils/backgrounds.json index af800c5..8cb01d1 100644 --- a/utils/backgrounds.json +++ b/utils/backgrounds.json @@ -35,5 +35,29 @@ "cluster_truck.mp4", "No Copyright Gameplay", 480 + ], + "minecraft-2": [ + "https://www.youtube.com/watch?v=Pt5_GSKIWQM", + "minecraft-2.mp4", + "Itslpsn", + "center" + ], + "multiversus": [ + "https://www.youtube.com/watch?v=66oK1Mktz6g", + "multiversus.mp4", + "MKIceAndFire", + "center" + ], + "fall-guys": [ + "https://www.youtube.com/watch?v=oGSsgACIc6Q", + "fall-guys.mp4", + "Throneful", + "center" + ], + "steep": [ + "https://www.youtube.com/watch?v=EnGiQrWBrko", + "steep.mp4", + "joel", + "center" ] -} \ No newline at end of file +} diff --git a/utils/cleanup.py b/utils/cleanup.py index f7bde35..e035980 100644 --- a/utils/cleanup.py +++ b/utils/cleanup.py @@ -12,18 +12,18 @@ def cleanup(id) -> int: Returns: int: How many files were deleted """ - if exists("./assets/temp"): + if exists(f"../assets/temp/{id}/"): count = 0 - files = [f for f in os.listdir(".") if f.endswith(".mp4") and "temp" in f.lower()] + files = [f for f in os.listdir(f"../assets/temp/{id}/") if f.endswith(".mp4")] count += len(files) for f in files: - os.remove(f) - REMOVE_DIRS = [f"./assets/temp/{id}/mp3/", f"./assets/temp/{id}/png/"] - files_to_remove = list(map(_listdir, REMOVE_DIRS)) - for directory in files_to_remove: - for file in directory: - count += 1 - os.remove(file) + os.remove(f"../assets/temp/{id}/{f}") + REMOVE_DIRS = [f"../assets/temp/{id}/mp3/", f"../assets/temp/{id}/png/"] + for d in REMOVE_DIRS: + if exists(d): + count += len(_listdir(d)) + for f in _listdir(d): + os.remove(f) + os.rmdir(d) + os.rmdir(f"../assets/temp/{id}/") return count - - return 0 diff --git a/utils/console.py b/utils/console.py index ce1b8a4..3419f05 100644 --- a/utils/console.py +++ b/utils/console.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 import re from rich.columns import Columns @@ -50,12 +49,19 @@ def handle_input( optional=False, ): if optional: - console.print(message + "\n[green]This is an optional value. Do you want to skip it? (y/n)") + console.print( + message + + "\n[green]This is an optional value. Do you want to skip it? (y/n)" + ) if input().casefold().startswith("y"): return default if default is not NotImplemented else "" if default is not NotImplemented: console.print( - "[green]" + message + '\n[blue bold]The default value is "' + str(default) + '"\nDo you want to use it?(y/n)' + "[green]" + + message + + '\n[blue bold]The default value is "' + + str(default) + + '"\nDo you want to use it?(y/n)' ) if input().casefold().startswith("y"): return default @@ -68,7 +74,9 @@ def handle_input( if check_type is not False: try: user_input = check_type(user_input) - if (nmin is not None and user_input < nmin) or (nmax is not None and user_input > nmax): + if (nmin is not None and user_input < nmin) or ( + nmax is not None and user_input > nmax + ): # FAILSTATE Input out of bounds console.print("[red]" + oob_error) continue @@ -78,13 +86,19 @@ def handle_input( console.print("[red]" + err_message) continue elif match != "" and re.match(match, user_input) is None: - console.print("[red]" + err_message + "\nAre you absolutely sure it's correct?(y/n)") + console.print( + "[red]" + + err_message + + "\nAre you absolutely sure it's correct?(y/n)" + ) if input().casefold().startswith("y"): break continue else: # FAILSTATE Input STRING out of bounds - if (nmin is not None and len(user_input) < nmin) or (nmax is not None and len(user_input) > nmax): + if (nmin is not None and len(user_input) < nmin) or ( + nmax is not None and len(user_input) > nmax + ): console.print("[red bold]" + oob_error) continue break # SUCCESS Input STRING in bounds @@ -98,8 +112,20 @@ def handle_input( isinstance(eval(user_input), check_type) return check_type(user_input) except: - console.print("[red bold]" + err_message + "\nValid options are: " + ", ".join(map(str, options)) + ".") + console.print( + "[red bold]" + + err_message + + "\nValid options are: " + + ", ".join(map(str, options)) + + "." + ) continue if user_input in options: return user_input - console.print("[red bold]" + err_message + "\nValid options are: " + ", ".join(map(str, options)) + ".") + console.print( + "[red bold]" + + err_message + + "\nValid options are: " + + ", ".join(map(str, options)) + + "." + ) diff --git a/utils/imagenarator.py b/utils/imagenarator.py new file mode 100644 index 0000000..ac53d82 --- /dev/null +++ b/utils/imagenarator.py @@ -0,0 +1,75 @@ +import re +import textwrap + +from PIL import Image, ImageDraw, ImageFont +from rich.progress import track +from TTS.engine_wrapper import process_text + +def draw_multiple_line_text(image, text, font, text_color, padding, wrap=50): + """ + Draw multiline text over given image + """ + draw = ImageDraw.Draw(image) + Fontperm = font.getsize(text) + image_width, image_height = image.size + lines = textwrap.wrap(text, width=wrap) + y = (image_height / 2) - ( + ((Fontperm[1] + (len(lines) * padding) / len(lines)) * len(lines)) / 2 + ) + for line in lines: + line_width, line_height = font.getsize(line) + draw.text(((image_width - line_width) / 2, y), line, font=font, fill=text_color) + y += line_height + padding + + +# theme=bgcolor,reddit_obj=reddit_object,txtclr=txtcolor +def imagemaker(theme, reddit_obj: dict, txtclr, padding=5): + """ + Render Images for video + """ + title = process_text(reddit_obj["thread_title"], False) #TODO if second argument cause any error + texts = reddit_obj["thread_post"] + id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"]) + + tfont = ImageFont.truetype("fonts\\Roboto-Bold.ttf", 27) # for title + font = ImageFont.truetype( + "fonts\\Roboto-Regular.ttf", 20 + ) # for despcription|comments + size = (500, 176) + + image = Image.new("RGBA", size, theme) + draw = ImageDraw.Draw(image) + + # for title + if len(title) > 40: + draw_multiple_line_text(image, title, tfont, txtclr, padding, wrap=30) + else: + + Fontperm = tfont.getsize(title) + draw.text( + ((image.size[0] - Fontperm[0]) / 2, (image.size[1] - Fontperm[1]) / 2), + font=tfont, + text=title, + ) # (image.size[1]/2)-(Fontperm[1]/2) + + image.save(f"assets/temp/{id}/png/title.png") + + # for comment|description + + for idx, text in track(enumerate(texts), "Rendering Image"):#, total=len(texts)): + + image = Image.new("RGBA", size, theme) + draw = ImageDraw.Draw(image) + text = process_text(text,False) + if len(text) > 50: + draw_multiple_line_text(image, text, font, txtclr, padding) + + else: + + Fontperm = font.getsize(text) + draw.text( + ((image.size[0] - Fontperm[0]) / 2, (image.size[1] - Fontperm[1]) / 2), + font=font, + text=text, + ) # (image.size[1]/2)-(Fontperm[1]/2) + image.save(f"assets/temp/{id}/png/img{idx}.png") diff --git a/utils/posttextparser.py b/utils/posttextparser.py new file mode 100644 index 0000000..4b3ae7e --- /dev/null +++ b/utils/posttextparser.py @@ -0,0 +1,29 @@ +import re + +import spacy + +from utils.console import print_step +from utils.voice import sanitize_text + + +# working good +def posttextparser(obj): + text = re.sub("\n", "", obj) + + try: + nlp = spacy.load('en_core_web_sm') + except OSError: + print_step("The spacy model can't load. You need to install it with \npython -m spacy download en") + exit() + + doc = nlp(text) + + newtext: list = [] + + # to check for space str + for line in doc.sents: + if sanitize_text(line.text): + newtext.append(line.text) + # print(line) + + return newtext diff --git a/utils/settings.py b/utils/settings.py index d2974d5..221a09c 100755 --- a/utils/settings.py +++ b/utils/settings.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python import re from typing import Tuple, Dict from pathlib import Path @@ -34,12 +33,17 @@ def check(value, checks, name): except: incorrect = True - if not incorrect and "options" in checks and value not in checks["options"]: # FAILSTATE Value is not one of the options + if ( + not incorrect and "options" in checks and value not in checks["options"] + ): # FAILSTATE Value is not one of the options incorrect = True if ( not incorrect and "regex" in checks - and ((isinstance(value, str) and re.match(checks["regex"], value) is None) or not isinstance(value, str)) + and ( + (isinstance(value, str) and re.match(checks["regex"], value) is None) + or not isinstance(value, str) + ) ): # FAILSTATE Value doesn't match regex, or has regex but is not a string. incorrect = True @@ -48,7 +52,11 @@ def check(value, checks, name): and not hasattr(value, "__iter__") and ( ("nmin" in checks and checks["nmin"] is not None and value < checks["nmin"]) - or ("nmax" in checks and checks["nmax"] is not None and value > checks["nmax"]) + or ( + "nmax" in checks + and checks["nmax"] is not None + and value > checks["nmax"] + ) ) ): incorrect = True @@ -56,8 +64,16 @@ def check(value, checks, name): not incorrect and hasattr(value, "__iter__") and ( - ("nmin" in checks and checks["nmin"] is not None and len(value) < checks["nmin"]) - or ("nmax" in checks and checks["nmax"] is not None and len(value) > checks["nmax"]) + ( + "nmin" in checks + and checks["nmin"] is not None + and len(value) < checks["nmin"] + ) + or ( + "nmax" in checks + and checks["nmax"] is not None + and len(value) > checks["nmax"] + ) ) ): incorrect = True @@ -65,9 +81,15 @@ def check(value, checks, name): if incorrect: value = handle_input( message=( - (("[blue]Example: " + str(checks["example"]) + "\n") if "example" in checks else "") + ( + ("[blue]Example: " + str(checks["example"]) + "\n") + if "example" in checks + else "" + ) + "[red]" - + ("Non-optional ", "Optional ")["optional" in checks and checks["optional"] is True] + + ("Non-optional ", "Optional ")[ + "optional" in checks and checks["optional"] is True + ] ) + "[#C0CAF5 bold]" + str(name) @@ -79,7 +101,9 @@ def check(value, checks, name): err_message=get_check_value("input_error", "Incorrect input"), nmin=get_check_value("nmin", None), nmax=get_check_value("nmax", None), - oob_error=get_check_value("oob_error", "Input out of bounds(Value too high/low/long/short)"), + oob_error=get_check_value( + "oob_error", "Input out of bounds(Value too high/low/long/short)" + ), options=get_check_value("options", None), optional=get_check_value("optional", False), ) @@ -106,7 +130,9 @@ def check_toml(template_file, config_file) -> Tuple[bool, Dict]: try: template = toml.load(template_file) except Exception as error: - console.print(f"[red bold]Encountered error when trying to to load {template_file}: {error}") + console.print( + f"[red bold]Encountered error when trying to to load {template_file}: {error}" + ) return False try: config = toml.load(config_file) diff --git a/utils/subreddit.py b/utils/subreddit.py index b0b7ae5..cec8b46 100644 --- a/utils/subreddit.py +++ b/utils/subreddit.py @@ -3,9 +3,10 @@ from os.path import exists from utils import settings from utils.console import print_substep +from utils.ai_methods import sort_by_similarity -def get_subreddit_undone(submissions: list, subreddit, times_checked=0): +def get_subreddit_undone(submissions: list, subreddit, times_checked=0, similarity_scores=None): """_summary_ Args: @@ -15,13 +16,20 @@ def get_subreddit_undone(submissions: list, subreddit, times_checked=0): Returns: Any: The submission that has not been done """ + # Second try of getting a valid Submission + if times_checked and settings.config["ai"]["ai_similarity_enabled"]: + print('Sorting based on similarity for a different date filter and thread limit..') + submissions = sort_by_similarity(submissions, keywords=settings.config["ai"]["ai_similarity_enabled"]) + # recursively checks if the top submission in the list was already done. if not exists("./video_creation/data/videos.json"): with open("./video_creation/data/videos.json", "w+") as f: json.dump([], f) - with open("./video_creation/data/videos.json", "r", encoding="utf-8") as done_vids_raw: + with open( + "./video_creation/data/videos.json", "r", encoding="utf-8" + ) as done_vids_raw: done_videos = json.load(done_vids_raw) - for submission in submissions: + for i, submission in enumerate(submissions): if already_done(done_videos, submission): continue if submission.over_18: @@ -34,11 +42,17 @@ def get_subreddit_undone(submissions: list, subreddit, times_checked=0): if submission.stickied: print_substep("This post was pinned by moderators. Skipping...") continue - if submission.num_comments <= int(settings.config["reddit"]["thread"]["min_comments"]): + if submission.num_comments <= int( + settings.config["reddit"]["thread"]["min_comments"] + ) and not settings.config["settings"]["storymode"]: print_substep( f'This post has under the specified minimum of comments ({settings.config["reddit"]["thread"]["min_comments"]}). Skipping...' ) continue + if settings.config["settings"]["storymode"] and not submission.is_self: + continue + if similarity_scores is not None: + return submission, similarity_scores[i].item() return submission print("all submissions have been done going by top submission order") VALID_TIME_FILTERS = [ @@ -54,7 +68,10 @@ def get_subreddit_undone(submissions: list, subreddit, times_checked=0): print("all time filters have been checked you absolute madlad ") return get_subreddit_undone( - subreddit.top(time_filter=VALID_TIME_FILTERS[index], limit=(50 if int(index) == 0 else index + 1 * 50)), + subreddit.top( + time_filter=VALID_TIME_FILTERS[index], + limit=(50 if int(index) == 0 else index + 1 * 50), + ), subreddit, times_checked=index, ) # all the videos in hot have already been done diff --git a/utils/thumbnail.py b/utils/thumbnail.py new file mode 100644 index 0000000..6f01e6c --- /dev/null +++ b/utils/thumbnail.py @@ -0,0 +1,37 @@ +from PIL import ImageDraw, ImageFont + + +def create_thumbnail(thumbnail, font_family, font_size, font_color, width, height, title): + + font = ImageFont.truetype(font_family + ".ttf", font_size) + Xaxis = width - (width * 0.2) # 20% of the width + sizeLetterXaxis = font_size * 0.5 # 50% of the font size + XaxisLetterQty = round(Xaxis / sizeLetterXaxis) # Quantity of letters that can fit in the X axis + MarginYaxis = (height * 0.12) # 12% of the height + MarginXaxis = (width * 0.05) # 5% of the width + # 1.1 rem + LineHeight = font_size * 1.1 + # rgb = "255,255,255" transform to list + rgb = font_color.split(",") + rgb = (int(rgb[0]), int(rgb[1]), int(rgb[2])) + + arrayTitle = [] + for word in title.split(): + if len(arrayTitle) == 0: + # colocar a primeira palavra no arrayTitl# put the first word in the arrayTitle + arrayTitle.append(word) + else: + # if the size of arrayTitle is less than qtLetters + if len(arrayTitle[-1]) + len(word) < XaxisLetterQty: + arrayTitle[-1] = arrayTitle[-1] + " " + word + else: + arrayTitle.append(word) + + draw = ImageDraw.Draw(thumbnail) + # loop for put the title in the thumbnail + for i in range(0, len(arrayTitle)): + # 1.1 rem + draw.text((MarginXaxis, MarginYaxis + (LineHeight * i)), + arrayTitle[i], rgb, font=font) + + return thumbnail diff --git a/utils/version.py b/utils/version.py index 8cad1d8..0818c87 100644 --- a/utils/version.py +++ b/utils/version.py @@ -3,13 +3,19 @@ import requests from utils.console import print_step -def checkversion(__VERSION__): - response = requests.get("https://api.github.com/repos/elebumm/RedditVideoMakerBot/releases/latest") +def checkversion(__VERSION__: str): + response = requests.get( + "https://api.github.com/repos/elebumm/RedditVideoMakerBot/releases/latest" + ) latestversion = response.json()["tag_name"] if __VERSION__ == latestversion: print_step(f"You are using the newest version ({__VERSION__}) of the bot") return True - else: + elif __VERSION__ < latestversion: print_step( f"You are using an older version ({__VERSION__}) of the bot. Download the newest version ({latestversion}) from https://github.com/elebumm/RedditVideoMakerBot/releases/latest" ) + else: + print_step( + f"Welcome to the test version ({__VERSION__}) of the bot. Thanks for testing and feel free to report any bugs you find." + ) diff --git a/utils/video.py b/utils/video.py index 2d212df..a785df4 100644 --- a/utils/video.py +++ b/utils/video.py @@ -36,15 +36,28 @@ class Video: im.save(path) return ImageClip(path) - def add_watermark(self, text, redditid, opacity=0.5, duration: int | float = 5, position: Tuple = (0.7, 0.9), fontsize=15): + def add_watermark( + self, + text, + redditid, + opacity=0.5, + duration: int | float = 5, + position: Tuple = (0.7, 0.9), + fontsize=15, + ): compensation = round( - (position[0] / ((len(text) * (fontsize / 5) / 1.5) / 100 + position[0] * position[0])), + ( + position[0] + / ((len(text) * (fontsize / 5) / 1.5) / 100 + position[0] * position[0]) + ), ndigits=2, ) position = (compensation, position[1]) # print(f'{compensation=}') # print(f'{position=}') - img_clip = self._create_watermark(text, redditid, fontsize=fontsize, opacity=opacity) + img_clip = self._create_watermark( + text, redditid, fontsize=fontsize, opacity=opacity + ) img_clip = img_clip.set_opacity(opacity).set_duration(duration) img_clip = img_clip.set_position( position, relative=True diff --git a/utils/videos.py b/utils/videos.py index 7c756fc..c30cb2c 100755 --- a/utils/videos.py +++ b/utils/videos.py @@ -19,7 +19,9 @@ def check_done( Returns: Submission|None: Reddit object in args """ - with open("./video_creation/data/videos.json", "r", encoding="utf-8") as done_vids_raw: + with open( + "./video_creation/data/videos.json", "r", encoding="utf-8" + ) as done_vids_raw: done_videos = json.load(done_vids_raw) for video in done_videos: if video["id"] == str(redditobj): @@ -33,7 +35,9 @@ def check_done( return redditobj -def save_data(subreddit: str, filename: str, reddit_title: str, reddit_id: str, credit: str): +def save_data( + subreddit: str, filename: str, reddit_title: str, reddit_id: str, credit: str +): """Saves the videos that have already been generated to a JSON file in video_creation/data/videos.json Args: diff --git a/utils/voice.py b/utils/voice.py index 0ff6b37..a88c87d 100644 --- a/utils/voice.py +++ b/utils/voice.py @@ -6,6 +6,9 @@ from time import sleep from requests import Response +from utils import settings +from cleantext import clean + if sys.version_info[0] >= 3: from datetime import timezone @@ -40,7 +43,9 @@ def sleep_until(time): if sys.version_info[0] >= 3 and time.tzinfo: end = time.astimezone(timezone.utc).timestamp() else: - zoneDiff = pytime.time() - (datetime.now() - datetime(1970, 1, 1)).total_seconds() + zoneDiff = ( + pytime.time() - (datetime.now() - datetime(1970, 1, 1)).total_seconds() + ) end = (time - datetime(1970, 1, 1)).total_seconds() + zoneDiff # Type check @@ -84,5 +89,10 @@ def sanitize_text(text: str) -> str: regex_expr = r"\s['|’]|['|’]\s|[\^_~@!&;#:\-%—“”‘\"%\*/{}\[\]\(\)\\|<>=+]" result = re.sub(regex_expr, " ", result) result = result.replace("+", "plus").replace("&", "and") + + # emoji removal if the setting is enabled + if settings.config["settings"]["tts"]["no_emojis"]: + result = clean(result, no_emoji=True) + # remove extra whitespace return " ".join(result.split()) diff --git a/video_creation/background.py b/video_creation/background.py index c451025..0405e66 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -45,7 +45,9 @@ def get_start_and_end_times(video_length: int, length_of_clip: int) -> Tuple[int def get_background_config(): """Fetch the background/s configuration""" try: - choice = str(settings.config["settings"]["background"]["background_choice"]).casefold() + choice = str( + settings.config["settings"]["background"]["background_choice"] + ).casefold() except AttributeError: print_substep("No background selected. Picking random background'") choice = None @@ -65,16 +67,20 @@ def download_background(background_config: Tuple[str, str, str, Any]): uri, filename, credit, _ = background_config if Path(f"assets/backgrounds/{credit}-{filename}").is_file(): return - print_step("We need to download the backgrounds videos. they are fairly large but it's only done once. 😎") + print_step( + "We need to download the backgrounds videos. they are fairly large but it's only done once. 😎" + ) print_substep("Downloading the backgrounds videos... please be patient 🙏 ") print_substep(f"Downloading {filename} from {uri}") - YouTube(uri, on_progress_callback=on_progress).streams.filter(res="1080p").first().download( - "assets/backgrounds", filename=f"{credit}-{filename}" - ) + YouTube(uri, on_progress_callback=on_progress).streams.filter( + res="1080p" + ).first().download("assets/backgrounds", filename=f"{credit}-{filename}") print_substep("Background video downloaded successfully! 🎉", style="bold green") -def chop_background_video(background_config: Tuple[str, str, str, Any], video_length: int, reddit_object: dict): +def chop_background_video( + background_config: Tuple[str, str, str, Any], video_length: int, reddit_object: dict +): """Generates the background footage to be used in the video and writes it to assets/temp/background.mp4 Args: diff --git a/video_creation/final_video.py b/video_creation/final_video.py index ad675c5..5d9b74f 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -1,9 +1,12 @@ -#!/usr/bin/env python3 -import multiprocessing import os import re +import multiprocessing from os.path import exists +from typing import Tuple, Any, Final +import translators as ts +import shutil from typing import Tuple, Any +from PIL import Image from moviepy.audio.AudioClip import concatenate_audioclips, CompositeAudioClip from moviepy.audio.io.AudioFileClip import AudioFileClip @@ -13,15 +16,17 @@ from moviepy.video.compositing.concatenate import concatenate_videoclips from moviepy.video.io.VideoFileClip import VideoFileClip from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip from rich.console import Console +from rich.progress import track -from utils import settings from utils.cleanup import cleanup from utils.console import print_step, print_substep from utils.video import Video from utils.videos import save_data +from utils.thumbnail import create_thumbnail +from utils import settings +from utils.thumbnail import create_thumbnail console = Console() -W, H = 1080, 1920 def name_normalize(name: str) -> str: @@ -31,20 +36,34 @@ def name_normalize(name: str) -> str: name = re.sub(r"(\d+)\s?\/\s?(\d+)", r"\1 of \2", name) name = re.sub(r"(\w+)\s?\/\s?(\w+)", r"\1 or \2", name) name = re.sub(r"\/", r"", name) - name[:30] lang = settings.config["reddit"]["thread"]["post_lang"] if lang: - import translators as ts - print_substep("Translating filename...") translated_name = ts.google(name, to_language=lang) return translated_name - else: return name +def prepare_background(reddit_id: str, W: int, H: int) -> VideoFileClip: + clip = ( + VideoFileClip(f"assets/temp/{reddit_id}/background.mp4") + .without_audio() + .resize(height=H) + ) + + # calculate the center of the background clip + c = clip.w // 2 + + # calculate the coordinates where to crop + half_w = W // 2 + x1 = c - half_w + x2 = c + half_w + + return clip.crop(x1=x1, y1=0, x2=x2, y2=H) + + def make_final_video( number_of_clips: int, length: int, @@ -58,27 +77,47 @@ def make_final_video( reddit_obj (dict): The reddit object that contains the posts to read. background_config (Tuple[str, str, str, Any]): The background config to use. """ + # settings values + W: Final[int] = int(settings.config["settings"]["resolution_w"]) + H: Final[int] = int(settings.config["settings"]["resolution_h"]) + # try: # if it isn't found (i.e you just updated and copied over config.toml) it will throw an error # VOLUME_MULTIPLIER = settings.config["settings"]['background']["background_audio_volume"] # except (TypeError, KeyError): # print('No background audio volume found in config.toml. Using default value of 1.') # VOLUME_MULTIPLIER = 1 - id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"]) + + reddit_id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"]) print_step("Creating the final video 🎥") + VideoFileClip.reW = lambda clip: clip.resize(width=W) VideoFileClip.reH = lambda clip: clip.resize(width=H) + opacity = settings.config["settings"]["opacity"] transition = settings.config["settings"]["transition"] - background_clip = ( - VideoFileClip(f"assets/temp/{id}/background.mp4") - .without_audio() - .resize(height=H) - .crop(x1=1166.6, y1=0, x2=2246.6, y2=1920) - ) + + background_clip = prepare_background(reddit_id, W=W, H=H) # Gather all audio clips - audio_clips = [AudioFileClip(f"assets/temp/{id}/mp3/{i}.mp3") for i in range(number_of_clips)] - audio_clips.insert(0, AudioFileClip(f"assets/temp/{id}/mp3/title.mp3")) + if settings.config["settings"]["storymode"]: + if settings.config["settings"]["storymodemethod"] == 0: + audio_clips = [AudioFileClip(f"assets/temp/{reddit_id}/mp3/title.mp3")] + audio_clips.insert(1, AudioFileClip(f"assets/temp/{reddit_id}/mp3/postaudio.mp3")) + elif settings.config["settings"]["storymodemethod"] == 1: + audio_clips = [ + AudioFileClip(f"assets/temp/{reddit_id}/mp3/postaudio-{i}.mp3") + for i in track( + range(number_of_clips + 1), "Collecting the audio files..." + ) + ] + audio_clips.insert(0, AudioFileClip(f"assets/temp/{reddit_id}/mp3/title.mp3")) + + else: + audio_clips = [ + AudioFileClip(f"assets/temp/{reddit_id}/mp3/{i}.mp3") + for i in range(number_of_clips) + ] + audio_clips.insert(0, AudioFileClip(f"assets/temp/{reddit_id}/mp3/title.mp3")) audio_concat = concatenate_audioclips(audio_clips) audio_composite = CompositeAudioClip([audio_concat]) @@ -87,51 +126,129 @@ def make_final_video( image_clips = [] # Gather all images new_opacity = 1 if opacity is None or float(opacity) >= 1 else float(opacity) - new_transition = 0 if transition is None or float(transition) > 2 else float(transition) + new_transition = ( + 0 if transition is None or float(transition) > 2 else float(transition) + ) + screenshot_width = int((W * 90) // 100) image_clips.insert( 0, - ImageClip(f"assets/temp/{id}/png/title.png") + ImageClip(f"assets/temp/{reddit_id}/png/title.png") .set_duration(audio_clips[0].duration) - .resize(width=W - 100) + .resize(width=screenshot_width) .set_opacity(new_opacity) .crossfadein(new_transition) .crossfadeout(new_transition), ) + if settings.config["settings"]["storymode"]: + if settings.config["settings"]["storymodemethod"] == 0: + image_clips.insert( + 1, + ImageClip(f"assets/temp/{reddit_id}/png/story_content.png") + .set_duration(audio_clips[1].duration) + .set_position("center") + .resize(width=screenshot_width) + .set_opacity(float(opacity)), + ) + elif settings.config["settings"]["storymodemethod"] == 1: + for i in track( + range(0, number_of_clips + 1), "Collecting the image files..." + ): + image_clips.append( + ImageClip(f"assets/temp/{reddit_id}/png/img{i}.png") + .set_duration(audio_clips[i + 1].duration) + .resize(width=screenshot_width) + .set_opacity(new_opacity) + # .crossfadein(new_transition) + # .crossfadeout(new_transition) + ) + else: + for i in range(0, number_of_clips): + image_clips.append( + ImageClip(f"assets/temp/{reddit_id}/png/comment_{i}.png") + .set_duration(audio_clips[i + 1].duration) + .resize(width=screenshot_width) + .set_opacity(new_opacity) + .crossfadein(new_transition) + .crossfadeout(new_transition) + ) - for i in range(0, number_of_clips): - image_clips.append( - ImageClip(f"assets/temp/{id}/png/comment_{i}.png") - .set_duration(audio_clips[i + 1].duration) - .resize(width=W - 100) - .set_opacity(new_opacity) - .crossfadein(new_transition) - .crossfadeout(new_transition) - ) - - # if os.path.exists("assets/mp3/posttext.mp3"): - # image_clips.insert( - # 0, - # ImageClip("assets/png/title.png") - # .set_duration(audio_clips[0].duration + audio_clips[1].duration) - # .set_position("center") - # .resize(width=W - 100) - # .set_opacity(float(opacity)), - # ) - # else: story mode stuff img_clip_pos = background_config[3] - image_concat = concatenate_videoclips(image_clips).set_position(img_clip_pos) # note transition kwarg for delay in imgs + image_concat = concatenate_videoclips(image_clips).set_position( + img_clip_pos + ) # note transition kwarg for delay in imgs image_concat.audio = audio_composite 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"]) + title_thumb = reddit_obj["thread_title"] - filename = f"{name_normalize(title)[:251]}.mp4" + filename = f"{name_normalize(title)[:251]}" subreddit = settings.config["reddit"]["thread"]["subreddit"] if not exists(f"./results/{subreddit}"): print_substep("The results folder didn't exist so I made it") os.makedirs(f"./results/{subreddit}") + # create a tumbnail for the video + settingsbackground = settings.config["settings"]["background"] + + if settingsbackground["background_thumbnail"]: + if not exists(f"./results/{subreddit}/thumbnails"): + print_substep( + "The results/thumbnails folder didn't exist so I made it") + os.makedirs(f"./results/{subreddit}/thumbnails") + # get the first file with the .png extension from assets/backgrounds and use it as a background for the thumbnail + first_image = next( + ( + file + for file in os.listdir("assets/backgrounds") + if file.endswith(".png") + ), + None, + ) + if first_image is None: + print_substep("No png files found in assets/backgrounds", "red") + + if settingsbackground["background_thumbnail"] and first_image: + font_family = settingsbackground["background_thumbnail_font_family"] + font_size = settingsbackground["background_thumbnail_font_size"] + font_color = settingsbackground["background_thumbnail_font_color"] + thumbnail = Image.open(f"assets/backgrounds/{first_image}") + width, height = thumbnail.size + thumbnailSave = create_thumbnail(thumbnail, font_family, font_size, font_color, width, height, title_thumb) + thumbnailSave.save(f"./assets/temp/{reddit_id}/thumbnail.png") + print_substep(f"Thumbnail - Building Thumbnail in assets/temp/{reddit_id}/thumbnail.png") + + # create a tumbnail for the video + settingsbackground = settings.config["settings"]["background"] + + if settingsbackground["background_thumbnail"]: + if not exists(f"./results/{subreddit}/thumbnails"): + print_substep( + "The results/thumbnails folder didn't exist so I made it") + os.makedirs(f"./results/{subreddit}/thumbnails") + # get the first file with the .png extension from assets/backgrounds and use it as a background for the thumbnail + first_image = next( + ( + file + for file in os.listdir("assets/backgrounds") + if file.endswith(".png") + ), + None, + ) + if first_image is None: + print_substep("No png files found in assets/backgrounds", "red") + + if settingsbackground["background_thumbnail"] and first_image: + font_family = settingsbackground["background_thumbnail_font_family"] + font_size = settingsbackground["background_thumbnail_font_size"] + font_color = settingsbackground["background_thumbnail_font_color"] + thumbnail = Image.open(f"assets/backgrounds/{first_image}") + width, height = thumbnail.size + thumbnailSave = create_thumbnail(thumbnail, font_family, font_size, font_color, width, height, title_thumb) + thumbnailSave.save(f"./assets/temp/{reddit_id}/thumbnail.png") + print_substep(f"Thumbnail - Building Thumbnail in assets/temp/{reddit_id}/thumbnail.png") + # if settings.config["settings"]['background']["background_audio"] and exists(f"assets/backgrounds/background.mp3"): # audioclip = mpe.AudioFileClip(f"assets/backgrounds/background.mp3").set_duration(final.duration) # audioclip = audioclip.fx( volumex, 0.2) @@ -139,25 +256,37 @@ def make_final_video( # # lowered_audio = audio_background.multiply_volume( # todo get this to work # # VOLUME_MULTIPLIER) # lower volume by background_audio_volume, use with fx # final.set_audio(final_audio) - final = Video(final).add_watermark(text=f"Background credit: {background_config[2]}", opacity=0.4, redditid=reddit_obj) + + final = Video(final).add_watermark( + text=f"Background credit: {background_config[2]}", + opacity=0.4, + redditid=reddit_obj, + ) final.write_videofile( - f"assets/temp/{id}/temp.mp4", - fps=30, + f"assets/temp/{reddit_id}/temp.mp4", + fps=int(settings.config["settings"]["fps"]), audio_codec="aac", audio_bitrate="192k", verbose=False, threads=multiprocessing.cpu_count(), + preset="ultrafast", #TODO debug ) ffmpeg_extract_subclip( - f"assets/temp/{id}/temp.mp4", + f"assets/temp/{reddit_id}/temp.mp4", 0, length, - targetname=f"results/{subreddit}/{filename}", + targetname=f"results/{subreddit}/{filename}.mp4", ) - save_data(subreddit, filename, title, idx, background_config[2]) + #get the thumbnail image from assets/temp/id/thumbnail.png and save it in results/subreddit/thumbnails + if settingsbackground["background_thumbnail"] and exists(f"assets/temp/{id}/thumbnail.png"): + shutil.move(f"assets/temp/{id}/thumbnail.png", f"./results/{subreddit}/thumbnails/{filename}.png") + + save_data(subreddit, filename+".mp4", title, idx, background_config[2]) print_step("Removing temporary files 🗑") - cleanups = cleanup(id) + cleanups = cleanup(reddit_id) print_substep(f"Removed {cleanups} temporary files 🗑") 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]}' + ) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index ba7835f..6df08a9 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -1,48 +1,81 @@ import json import re from pathlib import Path -from typing import Dict +from typing import Dict, Final import translators as ts -from playwright.sync_api import sync_playwright, ViewportSize +from playwright.async_api import async_playwright # pylint: disable=unused-import +from playwright.sync_api import ViewportSize, sync_playwright from rich.progress import track from utils import settings from utils.console import print_step, print_substep +from utils.imagenarator import imagemaker -# do not remove the above line -storymode = False +__all__ = ["download_screenshots_of_reddit_posts"] - -def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): +def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): """Downloads screenshots of reddit posts as seen on the web. Downloads to assets/temp/png Args: reddit_object (Dict): Reddit object received from reddit/subreddit.py screenshot_num (int): Number of screenshots to download """ + # settings values + W: Final[int] = int(settings.config["settings"]["resolution_w"]) + H: Final[int] = int(settings.config["settings"]["resolution_h"]) + lang: Final[str] = settings.config["reddit"]["thread"]["post_lang"] + storymode: Final[bool] = settings.config["settings"]["storymode"] + print_step("Downloading screenshots of reddit posts...") - id = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"]) + reddit_id = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"]) # ! Make sure the reddit screenshots folder exists - Path(f"assets/temp/{id}/png").mkdir(parents=True, exist_ok=True) + Path(f"assets/temp/{reddit_id}/png").mkdir(parents=True, exist_ok=True) + screenshot_num: int with sync_playwright() as p: print_substep("Launching Headless Browser...") - browser = p.chromium.launch(headless=True) # add headless=False for debug + browser = p.chromium.launch() # headless=False #to check for chrome view context = browser.new_context() - + # Device scale factor (or dsf for short) allows us to increase the resolution of the screenshots + # When the dsf is 1, the width of the screenshot is 600 pixels + # so we need a dsf such that the width of the screenshot is greater than the final resolution of the video + dsf = (W // 600) + 1 + + context = browser.new_context( + locale=lang or "en-us", + color_scheme="dark", + viewport=ViewportSize(width=W, height=H), + device_scale_factor=dsf, + ) + # set the theme and disable non-essential cookies if settings.config["settings"]["theme"] == "dark": - cookie_file = open("./video_creation/data/cookie-dark-mode.json", encoding="utf-8") + cookie_file = open( + "./video_creation/data/cookie-dark-mode.json", encoding="utf-8" + ) + bgcolor = (33, 33, 36, 255) + txtcolor = (240, 240, 240) else: - cookie_file = open("./video_creation/data/cookie-light-mode.json", encoding="utf-8") + cookie_file = open( + "./video_creation/data/cookie-light-mode.json", encoding="utf-8" + ) + bgcolor = (255, 255, 255, 255) + txtcolor = (0, 0, 0) + if settings.config["settings"]["storymodemethod"] == 1: + # for idx,item in enumerate(reddit_object["thread_post"]): + imagemaker(theme=bgcolor, reddit_obj=reddit_object, txtclr=txtcolor) cookies = json.load(cookie_file) + cookie_file.close() + context.add_cookies(cookies) # load preference cookies + # Get the thread screenshot page = context.new_page() page.goto(reddit_object["thread_url"], timeout=0) - page.set_viewport_size(ViewportSize(width=1920, height=1080)) + page.set_viewport_size(ViewportSize(width=W, height=H)) + if page.locator('[data-testid="content-gate"]').is_visible(): # This means the post is NSFW and requires to click the proceed button. @@ -51,15 +84,17 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in page.wait_for_load_state() # Wait for page to fully load if page.locator('[data-click-id="text"] button').is_visible(): - page.locator('[data-click-id="text"] button').click() # Remove "Click to see nsfw" Button in Screenshot + page.locator( + '[data-click-id="text"] button' + ).click() # Remove "Click to see nsfw" Button in Screenshot - # translate code + # translate code - if settings.config["reddit"]["thread"]["post_lang"]: + if lang: print_substep("Translating post...") texts_in_tl = ts.google( reddit_object["thread_title"], - to_language=settings.config["reddit"]["thread"]["post_lang"], + to_language=lang, ) page.evaluate( @@ -69,13 +104,20 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in else: print_substep("Skipping translation...") - postcontentpath = f"assets/temp/{id}/png/title.png" + postcontentpath = f"assets/temp/{reddit_id}/png/title.png" page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath) if storymode: - page.locator('[data-click-id="text"]').screenshot(path=f"assets/temp/{id}/png/story_content.png") + page.locator('[data-click-id="text"]').first.screenshot( + path=f"assets/temp/{reddit_id}/png/story_content.png" + ) else: - for idx, comment in enumerate(track(reddit_object["comments"], "Downloading screenshots...")): + for idx, comment in enumerate( + track( + reddit_object["comments"][:screenshot_num], + "Downloading screenshots...", + ) + ): # Stop if we have reached the screenshot_num if idx >= screenshot_num: break @@ -97,10 +139,18 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in [comment_tl, comment["comment_id"]], ) try: - page.locator(f"#t1_{comment['comment_id']}").screenshot(path=f"assets/temp/{id}/png/comment_{idx}.png") + page.locator(f"#t1_{comment['comment_id']}").screenshot( + path=f"assets/temp/{reddit_id}/png/comment_{idx}.png" + ) except TimeoutError: del reddit_object["comments"] screenshot_num += 1 print("TimeoutError: Skipping screenshot...") continue - print_substep("Screenshots downloaded Successfully.", style="bold green") + + # close browser instance when we are done using it + browser.close() + + + + print_substep("Screenshots downloaded Successfully.", style="bold green") \ No newline at end of file diff --git a/video_creation/voices.py b/video_creation/voices.py index 511b7ff..425f589 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - from typing import Tuple from rich.console import Console @@ -36,7 +34,9 @@ def save_text_to_mp3(reddit_obj) -> Tuple[int, int]: voice = settings.config["settings"]["tts"]["voice_choice"] if str(voice).casefold() in map(lambda _: _.casefold(), TTSProviders): - text_to_mp3 = TTSEngine(get_case_insensitive_key_value(TTSProviders, voice), reddit_obj) + text_to_mp3 = TTSEngine( + get_case_insensitive_key_value(TTSProviders, voice), reddit_obj + ) else: while True: print_step("Please choose one of the following TTS providers: ") @@ -45,12 +45,18 @@ def save_text_to_mp3(reddit_obj) -> Tuple[int, int]: if choice.casefold() in map(lambda _: _.casefold(), TTSProviders): break print("Unknown Choice") - text_to_mp3 = TTSEngine(get_case_insensitive_key_value(TTSProviders, choice), reddit_obj) + text_to_mp3 = TTSEngine( + get_case_insensitive_key_value(TTSProviders, choice), reddit_obj + ) return text_to_mp3.run() def get_case_insensitive_key_value(input_dict, key): 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() + ), None, )