-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoscript.py
97 lines (81 loc) · 3.38 KB
/
videoscript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from datetime import datetime
from moviepy.editor import AudioFileClip
import voiceover, re
MAX_WORDS_PER_COMMENT = 300
MIN_COMMENTS_FOR_FINISH = 4
MIN_DURATION = 30
MAX_DURATION = 59
class VideoScript:
title = ""
fileName = ""
titleSCFile = ""
url = ""
totalDuration = 0
frames = []
read_comments = True
def __init__(self, url, title, fileId, read_comments=True) -> None:
self.read_comments = read_comments
if not read_comments:
global MAX_WORDS_PER_COMMENT, MIN_COMMENTS_FOR_FINISH, MIN_DURATION, MAX_DURATION
MAX_WORDS_PER_COMMENT = 9999
MIN_COMMENTS_FOR_FINISH = 0
MIN_DURATION = 0
MAX_DURATION = 9999
self.fileName = f"{datetime.today().strftime('%Y-%m-%d')}-{fileId}"
self.url = url
self.title = title
self.titleAudioClip = self.__createVoiceOver("title", title)
def canBeFinished(self) -> bool:
return (len(self.frames) > 0) and (self.totalDuration > MIN_DURATION)
def canQuickFinish(self) -> bool:
return (len(self.frames) >= MIN_COMMENTS_FOR_FINISH) and (self.totalDuration > MIN_DURATION)
def addCommentScene(self, text, commentId) -> None:
wordCount = len(text.split())
if (wordCount > MAX_WORDS_PER_COMMENT):
return True
frame = ScreenshotScene(text, commentId)
frame.audioClip = self.__createVoiceOver(commentId, text)
if (frame.audioClip == None):
return True
self.frames.append(frame)
def addStoryScene(self, text, paragraph_number) -> None:
frame = ScreenshotScene(text, paragraph_number)
frame.audioClip = self.__createVoiceOver(paragraph_number, text)
if (frame.audioClip == None):
return True
self.frames.append(frame)
def getDuration(self):
return self.totalDuration
def getFileName(self):
return self.fileName
def clean_text(self, text):
url_pattern = re.compile(r'https?://\S+|www\.\S+')
cleaned_text = url_pattern.sub('', text)
cleaned_text = cleaned_text.lower().replace("aita", "am I the Ay hole", 10)
cleaned_text = cleaned_text.lower().replace("aitah", "am I the Ay hole", 10)
cleaned_text = cleaned_text.lower().replace("wibta", "would I be the Ay hole", 10)
cleaned_text = cleaned_text.lower().replace("wibtah", "would I be the Ay hole", 10)
cleaned_text = cleaned_text.lower().replace("tifu", "today I f'd up", 10)
return cleaned_text
def __createVoiceOver(self, name, text):
text = self.clean_text(text)
script_path = f"Scripts/{self.fileName}"
with open(script_path, 'w') as file:
file.write(text)
special_voice = False
if len(text) < 10 and name != "title":
special_voice = True
file_path = voiceover.create_voice_over(f"{self.fileName}-{name}", script_path, special_voice, self.read_comments)
print(f"Created voice over: {file_path}")
audioClip = AudioFileClip(file_path)
if (self.totalDuration + audioClip.duration > MAX_DURATION):
return None
self.totalDuration += audioClip.duration
return audioClip
class ScreenshotScene:
text = ""
screenShotFile = ""
commentId = ""
def __init__(self, text, commentId) -> None:
self.text = text
self.commentId = commentId