Skip to content

Commit d4670c5

Browse files
committed
Improve post-format filtering
1 parent 0b73fb5 commit d4670c5

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

src/main.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
background_audio_path = "assets/background_audios/"
1111
output_video_path = "outputs/videos/"
1212
output_audio_path = "outputs/audios/"
13-
iterations = 2
13+
iterations = 1
1414

1515
# Check if output directories are missing
1616
if not os.path.exists(output_video_path) or not os.path.exists(output_audio_path):
@@ -24,22 +24,25 @@
2424
post_grabber = PostGrabber(subreddit, category)
2525
tts = TikTokTTS("en_us_006")
2626

27-
for i in range(1, iterations):
27+
for i in range(iterations):
2828
# Scrape subreddit and obtain post text
2929
post = post_grabber.next_post()
3030
text = post_grabber.get_post_text(post)
31-
32-
audio_destination = output_audio_path + "audio" + str(i) + ".mp3"
33-
video_desination = output_video_path + "final" + str(i) + ".mp4"
31+
print(text)
32+
33+
file_index = len(os.listdir(output_video_path))
34+
audio_destination = output_audio_path + "audio" + str(file_index) + ".mp3"
35+
video_desination = output_video_path + "final" + str(file_index) + ".mp4"
3436

3537
# Generate TTS using TikTok's text-to-speech
3638
tts.create_tts(text, audio_destination)
3739

3840
# Generate and save video
3941
creator = VideoCreator(text, "Impact", 65, 2)
4042
comp = creator.create_composition(background_video_path, background_audio_path, audio_destination)
43+
print(f"Writing file to {video_desination}...")
4144
comp.write_videofile(video_desination, threads = 4, logger = None, fps = 60)
42-
print(f"File written to {video_desination}")
45+
print(f"File successfully written")
4346

4447
creator.free_memory()
4548

src/post_grabber.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from praw import Reddit
22
import random
33
import string
4+
import re
45

56
class PostGrabber:
67
def __init__(self, subreddit_name: str, post_category: str):
@@ -49,17 +50,29 @@ def change_post_category(self, new_post_category: str):
4950

5051
# Format post text and merge with title
5152
def get_post_text(self, post: str) -> str:
52-
full = ((post.title + " " + post.selftext)
53-
.replace("\n", " ")
54-
.replace("\t", " ")
55-
.replace("AITA", "Am I the Asshole")
56-
.replace("SIL", "sister-in-law")
57-
.replace("GF", "girlfriend")
58-
.replace("”", "")
59-
.replace("“", "")
60-
.replace("\"", "")
61-
.replace("\'", "")
62-
)
53+
full = post.title + " " + post.selftext
54+
formatting = {
55+
"[\n\t]": " ",
56+
"[“”\"]": "",
57+
"[-]": ".",
58+
"​": " ",
59+
"aita": "am I the asshole",
60+
"sil": "sister-in-law",
61+
"gf": "girlfriend",
62+
"cuz": "because",
63+
"ffs": "for fuck's sake",
64+
"imo": "in my opinion",
65+
"pos": "piece-of-shit",
66+
"bf": "boyfriend",
67+
"bil": "brother-in-law"
68+
}
69+
70+
# Filter any post-specific formatting or abreviations
71+
for key in formatting:
72+
if key[0] == '[':
73+
full = re.sub(r'{0}'.format(key), formatting[key], full, flags=re.IGNORECASE)
74+
else:
75+
full = re.sub(r'\b{0}\b'.format(key), formatting[key], full, flags=re.IGNORECASE)
6376

6477
# Removes last punctuation mark from the text
6578
if (full[-1] in string.punctuation ):

src/video_creator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_background_audio(self, path: str, duration: float) -> AudioFileClip:
3535
# Create subtitles using OpenAI Whisper
3636
def create_subtitles(self, audio_path: str) -> list[TextClip]:
3737
result = whisper_timestamped.transcribe(self.whisper, audio_path, compression_ratio_threshold=1.8)
38-
print("Successfully transcribed video")
38+
print("Transcriptions created")
3939
clips = []
4040
segments = result["segments"]
4141
for i in range(len(segments)):
@@ -47,7 +47,7 @@ def create_subtitles(self, audio_path: str) -> list[TextClip]:
4747
clips.append(self.create_clip(word, start, end))
4848
start = end
4949

50-
print("Successfully created subtitles")
50+
print("Subtitles created")
5151
return clips
5252

5353
# Create a textclip with the given duration
@@ -82,7 +82,7 @@ def create_composition(self, video_background_folder: str, audio_background_fold
8282
comp = comp.set_duration(tts.duration)
8383
self.memory_pool.append(comp)
8484

85-
print("Successfully created composition")
85+
print("Composition created")
8686
return comp
8787

8888
def free_memory(self):

0 commit comments

Comments
 (0)