Bot can run multiple instances at the same time

pull/1091/head
Simon 3 years ago committed by GitHub
parent b81ee722b8
commit 48460f6672
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -34,13 +34,14 @@ class TTSEngine:
self,
tts_module,
reddit_object: dict,
path: str = "assets/temp/mp3",
path: str = "assets/temp/",
max_length: int = DEFAULT_MAX_LENGTH,
last_clip_length: int = 0,
):
self.tts_module = tts_module()
self.reddit_object = reddit_object
self.path = path
self.redditid = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"])
self.path = path + self.redditid + "/mp3"
self.max_length = max_length
self.length = 0
self.last_clip_length = last_clip_length

@ -1,5 +1,6 @@
#!/usr/bin/env python
import math
import re
from subprocess import Popen
from os import name
@ -7,7 +8,7 @@ from prawcore import ResponseException
from reddit.subreddit import get_subreddit_threads
from utils.cleanup import cleanup
from utils.console import print_markdown, print_step
from utils.console import print_markdown, print_step, print_substep
from utils import settings
from video_creation.background import (
@ -36,20 +37,27 @@ print(
print_markdown(
"### Thanks for using this tool! [Feel free to contribute to this project on GitHub!](https://lewismenelaws.com) If you have any questions, feel free to reach out to me on Twitter or submit a GitHub issue. You can find solutions to many common problems in the [Documentation](https://luka-hietala.gitbook.io/documentation-for-the-reddit-bot/)"
)
print_step(f"You are using v{__VERSION__} of the bot")
print_step(f"You are using v{__VERSION__} of the bot in the {__BRANCH__} branch.")
def main(POST_ID=None):
cleanup()
reddit_object = get_subreddit_threads(POST_ID)
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)
bg_config = get_background_config()
download_background(bg_config)
chop_background_video(bg_config, length)
chop_background_video(bg_config, length, reddit_object)
make_final_video(number_of_comments, length, reddit_object, bg_config)
def id(reddit_obj: dict):
"""
This function takes a reddit object and returns the post id
"""
id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"])
print_substep(f"Thread ID is {id}", style="bold blue")
return id
def run_many(times):
for x in range(1, times + 1):
@ -61,8 +69,6 @@ def run_many(times):
def shutdown():
print_markdown("## Clearing temp files")
cleanup()
print("Exiting...")
exit()

@ -6,7 +6,7 @@ def _listdir(d): # listdir with full path
return [os.path.join(d, f) for f in os.listdir(d)]
def cleanup() -> int:
def cleanup(id) -> int:
"""Deletes all temporary assets in assets/temp
Returns:
@ -18,7 +18,7 @@ def cleanup() -> int:
count += len(files)
for f in files:
os.remove(f)
REMOVE_DIRS = ["./assets/temp/mp3/", "./assets/temp/png/"]
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:

@ -1,6 +1,7 @@
from pathlib import Path
import random
from random import randrange
import re
from typing import Any, Tuple
@ -62,7 +63,7 @@ def download_background(background_config: Tuple[str, str, str, Any]):
print_substep("Background video downloaded successfully! 🎉", style="bold green")
def chop_background_video(background_config: Tuple[str, str, str, Any], video_length: int):
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:
@ -72,7 +73,7 @@ def chop_background_video(background_config: Tuple[str, str, str, Any], video_le
print_step("Finding a spot in the backgrounds video to chop...✂️")
choice = f"{background_config[2]}-{background_config[1]}"
id = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"])
background = VideoFileClip(f"assets/backgrounds/{choice}")
start_time, end_time = get_start_and_end_times(video_length, background.duration)
@ -81,12 +82,12 @@ def chop_background_video(background_config: Tuple[str, str, str, Any], video_le
f"assets/backgrounds/{choice}",
start_time,
end_time,
targetname="assets/temp/background.mp4",
targetname=f"assets/temp/{id}/background.mp4",
)
except (OSError, IOError): # ffmpeg issue see #348
print_substep("FFMPEG issue. Trying again...")
with VideoFileClip(f"assets/backgrounds/{choice}") as video:
new = video.subclip(start_time, end_time)
new.write_videofile("assets/temp/background.mp4")
new.write_videofile(f"assets/temp/{id}/background.mp4")
print_substep("Background video chopped successfully!", style="bold green")
return background_config[2]

@ -1 +1,114 @@
[]
[
{
"subreddit": "AskReddit",
"id": "wau5wa",
"time": "1659091419",
"background_credit": "bbswitzer",
"reddit_title": "which animal should go extinct",
"filename": "which animal should go extinct.mp4"
},
{
"subreddit": "AskReddit",
"id": "waemn4",
"time": "1659091617",
"background_credit": "bbswitzer",
"reddit_title": "What if you found the Death Note",
"filename": "What if you found the Death Note.mp4"
},
{
"subreddit": "AskReddit",
"id": "wad2a5",
"time": "1659091827",
"background_credit": "bbswitzer",
"reddit_title": "What single ingredient will spoil an entire meal for you if its included",
"filename": "What single ingredient will spoil an entire meal for you if its included.mp4"
},
{
"subreddit": "AskReddit",
"id": "wav2ge",
"time": "1659092067",
"background_credit": "bbswitzer",
"reddit_title": "You are held at gunpoint and your kidnappers will not kill you if you can speak about a topic for 30 minutes straight What are you talking about",
"filename": "You are held at gunpoint and your kidnappers will not kill you if you can speak about a topic for 30 minutes straight What are you talking about.mp4"
},
{
"subreddit": "AskReddit",
"id": "wag2zd",
"time": "1659092442",
"background_credit": "bbswitzer",
"reddit_title": "Men what advice do you have for men whose wives want to bring a third into the bedroom",
"filename": "Men what advice do you have for men whose wives want to bring a third into the bedroom.mp4"
},
{
"subreddit": "AskReddit",
"id": "waa1bn",
"time": "1659092709",
"background_credit": "bbswitzer",
"reddit_title": "Whats a quote from history that gives you chills",
"filename": "Whats a quote from history that gives you chills.mp4"
},
{
"subreddit": "AskReddit",
"id": "wabnds",
"time": "1659093024",
"background_credit": "bbswitzer",
"reddit_title": "What immediately makes you think less of someone",
"filename": "What immediately makes you think less of someone.mp4"
},
{
"subreddit": "AskReddit",
"id": "waheci",
"time": "1659093225",
"background_credit": "bbswitzer",
"reddit_title": "Whats your wildest story that sounds too far-fetched to be true",
"filename": "Whats your wildest story that sounds too far-fetched to be true.mp4"
},
{
"subreddit": "AskReddit",
"id": "watszs",
"time": "1659093457",
"background_credit": "bbswitzer",
"reddit_title": "Whats the best response to I hate you",
"filename": "Whats the best response to I hate you.mp4"
},
{
"subreddit": "AskReddit",
"id": "waf9bi",
"time": "1659093744",
"background_credit": "bbswitzer",
"reddit_title": "What is something fucked up that we just ignore",
"filename": "What is something fucked up that we just ignore.mp4"
},
{
"subreddit": "AskReddit",
"id": "wax7i4",
"time": "1659094206",
"background_credit": "bbswitzer",
"reddit_title": "If your life was a game what would loading screen tips be",
"filename": "If your life was a game what would loading screen tips be.mp4"
},
{
"subreddit": "AskReddit",
"id": "walxy5",
"time": "1659094418",
"background_credit": "bbswitzer",
"reddit_title": "Whats your trick for falling asleep",
"filename": "Whats your trick for falling asleep.mp4"
},
{
"subreddit": "AskReddit",
"id": "wb1rql",
"time": "1659094606",
"background_credit": "bbswitzer",
"reddit_title": "Enough sex questions whats your favourite fruit and why",
"filename": "Enough sex questions whats your favourite fruit and why.mp4"
},
{
"subreddit": "AskReddit",
"id": "way799",
"time": "1659094868",
"background_credit": "bbswitzer",
"reddit_title": "You have to survive a horror movie with just one weapon What is your weapon of choice",
"filename": "You have to survive a horror movie with just one weapon What is your weapon of choice.mp4"
}
]

@ -62,21 +62,22 @@ def make_final_video(
# 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"])
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("assets/temp/background.mp4")
VideoFileClip(f"assets/temp/{id}/background.mp4")
.without_audio()
.resize(height=H)
.crop(x1=1166.6, y1=0, x2=2246.6, y2=1920)
)
# Gather all audio clips
audio_clips = [AudioFileClip(f"assets/temp/mp3/{i}.mp3") for i in range(number_of_clips)]
audio_clips.insert(0, AudioFileClip("assets/temp/mp3/title.mp3"))
audio_clips = [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"))
audio_concat = concatenate_audioclips(audio_clips)
audio_composite = CompositeAudioClip([audio_concat])
@ -88,7 +89,7 @@ def make_final_video(
new_transition = 0 if transition is None or float(transition) > 2 else float(transition)
image_clips.insert(
0,
ImageClip("assets/temp/png/title.png")
ImageClip(f"assets/temp/{id}/png/title.png")
.set_duration(audio_clips[0].duration)
.resize(width=W - 100)
.set_opacity(new_opacity)
@ -98,7 +99,7 @@ def make_final_video(
for i in range(0, number_of_clips):
image_clips.append(
ImageClip(f"assets/temp/png/comment_{i}.png")
ImageClip(f"assets/temp/{id}/png/comment_{i}.png")
.set_duration(audio_clips[i + 1].duration)
.resize(width=W - 100)
.set_opacity(new_opacity)
@ -139,11 +140,11 @@ 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(
'''final = Video(final).add_watermark(
text=f"Background credit: {background_config[2]}", opacity=0.4
)
)''' # todo add watermark
final.write_videofile(
"assets/temp/temp.mp4",
f"assets/temp/{id}/temp.mp4",
fps=30,
audio_codec="aac",
audio_bitrate="192k",
@ -151,14 +152,14 @@ def make_final_video(
threads=multiprocessing.cpu_count(),
)
ffmpeg_extract_subclip(
"assets/temp/temp.mp4",
f"assets/temp/{id}/temp.mp4",
0,
length,
targetname=f"results/{subreddit}/{filename}",
)
save_data(subreddit, filename, title, idx, background_config[2])
print_step("Removing temporary files 🗑")
cleanups = cleanup()
cleanups = cleanup(id)
print_substep(f"Removed {cleanups} temporary files 🗑")
print_substep("See result in the results folder!")

@ -1,6 +1,7 @@
import json
from pathlib import Path
import re
from typing import Dict
from utils import settings
from playwright.async_api import async_playwright # pylint: disable=unused-import
@ -24,9 +25,9 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in
screenshot_num (int): Number of screenshots to download
"""
print_step("Downloading screenshots of reddit posts...")
id = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"])
# ! Make sure the reddit screenshots folder exists
Path("assets/temp/png").mkdir(parents=True, exist_ok=True)
Path(f"assets/temp/{id}/png").mkdir(parents=True, exist_ok=True)
with sync_playwright() as p:
print_substep("Launching Headless Browser...")
@ -72,11 +73,12 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in
else:
print_substep("Skipping translation...")
page.locator('[data-test-id="post-content"]').screenshot(path="assets/temp/png/title.png")
postcontentpath = f"assets/temp/{id}/png/title.png"
page.locator('[data-test-id="post-content"]').screenshot(path= postcontentpath)
if storymode:
page.locator('[data-click-id="text"]').screenshot(
path="assets/temp/png/story_content.png"
path=f"assets/temp/{id}/png/story_content.png"
)
else:
for idx, comment in enumerate(
@ -104,7 +106,7 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in
)
try:
page.locator(f"#t1_{comment['comment_id']}").screenshot(
path=f"assets/temp/png/comment_{idx}.png"
path=f"assets/temp/{id}/png/comment_{idx}.png"
)
except TimeoutError:
del reddit_object["comments"]

Loading…
Cancel
Save