-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pharmor.py
243 lines (232 loc) · 9.12 KB
/
Pharmor.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import random, time, os, pytesseract, platform
import moviepy.editor as mpy
from pytube import YouTube
from PIL import Image
from moviepy.editor import *
from moviepy.video.fx.all import crop
from gtts import gTTS
global randomid
randomid = str(random.randint(1111, 9999))
# $$$$$$\ $$$$$$\
# $$ __$$\ $$ __$$\
# $$$$$$\$$$$\ $$$$$$\ $$$$$$$\ $$ / $$ |$$ / \__|
# $$ _$$ _$$\ \____$$\ $$ _____|$$ | $$ |\$$$$$$\
# $$ / $$ / $$ | $$$$$$$ |$$ / $$ | $$ | \____$$\
# $$ | $$ | $$ |$$ __$$ |$$ | $$ | $$ |$$\ $$ |
# $$ | $$ | $$ |\$$$$$$$ |\$$$$$$$\ $$$$$$ |\$$$$$$ |
# \__| \__| \__| \_______| \_______| \______/ \______/
if platform.system() == 'Darwin':
clear = lambda: os.system('clear')
def printcenter(s):
cols, _ = os.get_terminal_size(0)
print((s.center(cols)))
with open(('header.txt'), 'r') as f:
header = f.read()
def title():
clear()
printcenter(header + "\n\n\n\n")
def begin():
global videoinput
global randomid
imagefiles = []
for f in os.listdir('images'):
if f.lower().endswith(('.png', '.jpg', '.jpeg')):
imagefiles.append(f)
if len(imagefiles) == 0:
title()
printcenter('Folder is empty or does not contain enough image files!')
time.sleep(2)
exit()
title()
printcenter('Downloading youtube video: ' + videoinput)
yt = YouTube(videoinput)
video_stream = yt.streams.get_highest_resolution()
video_stream.download((''), filename=randomid + '.mp4')
lengthlimit = 65
if yt.length > lengthlimit:
title()
printcenter(f'Video is longer than {str(lengthlimit)} seconds! Cutting it...')
clip = mpy.VideoFileClip(randomid + '.mp4').subclip(0, 65)
oldid = randomid
time.sleep(1)
randomid = str(random.randint(1111, 9999))
clip.write_videofile(randomid + '.mp4')
clip.close()
os.remove(oldid + '.mp4')
else:
clip = mpy.VideoFileClip(randomid + '.mp4')
time.sleep(1)
title()
printcenter('Cropping video for mobile devices...')
clip = mpy.VideoFileClip(randomid + '.mp4')
(w, h) = clip.size
crop_width = h * 9/16
x1, x2 = (w - crop_width)//2, (w+crop_width)//2
y1, y2 = 0, h
cropped_clip = crop(clip, x1=x1, y1=y1, x2=x2, y2=y2)
oldid = randomid
time.sleep(1)
randomid = str(random.randint(1111, 9999))
cropped_clip.write_videofile(randomid + '.mp4')
clip.close()
os.remove(oldid + '.mp4')
# fix the code
title()
printcenter('Done!')
time.sleep(2)
exit()
def downloadfunc():
global videoinput
title()
printcenter('Enter YouTube video URL (rec 1920x1080):')
videoinput = input("")
if "https://www.youtube.com/watch?v=" or "https://youtu.be/" in videoinput:
if not os.path.exists('images'):
os.mkdir('images')
title()
printcenter('Please put your image files in the "images" folder.\nFor more information visit https://github.com/t-a-go/Pharmor')
time.sleep(2)
else:
begin()
else:
title()
printcenter('Invalid URL, please try again')
time.sleep(2)
downloadfunc()
downloadfunc()
# $$\ $$\ $$\ $$\
# $$ | $\ $$ |\__| $$ |
# $$ |$$$\ $$ |$$\ $$$$$$$\ $$$$$$$ | $$$$$$\ $$\ $$\ $$\ $$$$$$$\
# $$ $$ $$\$$ |$$ |$$ __$$\ $$ __$$ |$$ __$$\ $$ | $$ | $$ |$$ _____|
# $$$$ _$$$$ |$$ |$$ | $$ |$$ / $$ |$$ / $$ |$$ | $$ | $$ |\$$$$$$\
# $$$ / \$$$ |$$ |$$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ | $$ | \____$$\
# $$ / \$$ |$$ |$$ | $$ |\$$$$$$$ |\$$$$$$ |\$$$$$\$$$$ |$$$$$$$ |
# \__/ \__|\__|\__| \__| \_______| \______/ \_____\____/ \_______/
elif platform.system() == 'Windows':
import colorama, shutil
from colorama import Fore, Back
from PIL import Image, ImageDraw, ImageFont
colorama.init()
clear = lambda: os.system('cls')
def printcenter(s):
print(s.center(shutil.get_terminal_size().columns))
with open(('header.txt'), 'r') as f:
header = f.read()
def title():
clear()
printcenter(Fore.LIGHTYELLOW_EX + header + "\n\n\n\n")
def begin():
global videoinput
global randomid
global filechosen
global titlechosen
global storychosen
imagefiles = []
for f in os.listdir('frames'):
if f.lower().endswith(('.png', '.jpg', '.jpeg')):
imagefiles.append(f)
if len(imagefiles) == 0:
title()
printcenter(Fore.WHITE + 'Folder is empty or does not contain enough image files!')
time.sleep(2)
exit()
title()
printcenter(Fore.WHITE + 'Downloading youtube video: ' + videoinput)
yt = YouTube(videoinput)
video_stream = yt.streams.get_highest_resolution()
video_stream.download((''), filename=randomid + '.mp4')
lengthlimit = 65
if yt.length > lengthlimit:
title()
printcenter(Fore.WHITE + f'Video is longer than {str(lengthlimit)} seconds! Cutting it...')
clip = mpy.VideoFileClip(randomid + '.mp4').subclip(0, 65)
oldid = randomid
time.sleep(1)
randomid = str(random.randint(1111, 9999))
clip.write_videofile(randomid + '.mp4')
clip.close()
os.remove(oldid + '.mp4')
else:
clip = mpy.VideoFileClip(randomid + '.mp4')
time.sleep(1)
title()
printcenter(Fore.WHITE + 'Cropping video for mobile devices...')
clip = mpy.VideoFileClip(randomid + '.mp4')
(w, h) = clip.size
crop_width = h * 9/16
x1, x2 = (w - crop_width)//2, (w+crop_width)//2
y1, y2 = 0, h
cropped_clip = crop(clip, x1=x1, y1=y1, x2=x2, y2=y2)
oldid = randomid
time.sleep(1)
randomid = str(random.randint(1111, 9999))
cropped_clip.write_videofile(randomid + '.mp4')
clip.close()
os.remove(oldid + '.mp4')
image = Image.open('frames/' + filechosen)
draw = ImageDraw.Draw(image)
font_path = "arial.ttf"
font_size = 36
font = ImageFont.load_default()
text_width, text_height = draw.textsize(titlechosen, font)
image_width, image_height = image.size
text_x = (image_width - text_width) // 2
text_y = (image_height - text_height) // 2
draw.text((text_x, text_y), titlechosen, font=font, fill="white")
name, extension = os.path.splitext('frames/' + filechosen)
saveas = f"{str(random.randint(1111, 9999))}_with_text{extension}"
image.save(saveas)
title()
printcenter(Fore.WHITE + 'Done!')
time.sleep(2)
exit()
def downloadfunc():
global videoinput
title()
videoinput = input(Fore.WHITE + 'Enter YouTube video URL (rec 1920x1080): ')
if "https://www.youtube.com/watch?v=" in videoinput:
chooseheader()
else:
if "test" in videoinput:
chooseheader()
else:
title()
printcenter(Fore.WHITE + 'Invalid URL, please try again')
time.sleep(2)
downloadfunc()
def chooseheader():
title()
global filechosen
files = os.listdir('frames')
for i, file in enumerate(files):
print(Fore.YELLOW + f"[{i+1}] {file}")
while True:
choice = input(Fore.WHITE + "\nWhich frame header are you using (Enter the corresponding number): ")
try:
choice = int(choice)
if 1 <= choice <= len(files):
filechosen = files[choice - 1]
print('You choose the file ' + filechosen)
time.sleep(1)
inputtitletext()
else:
title()
printcenter(Fore.WHITE + "Invalid choice. Please enter a number within the range.")
time.sleep(2)
chooseheader()
except ValueError:
title()
printcenter(Fore.WHITE + "Invalid input. Please enter a number.")
time.sleep(2)
chooseheader()
def inputtitletext():
title()
global titlechosen
titlechosen = input(Fore.WHITE + "\nWhat is the story title?: ")
inputstorytext()
def inputstorytext():
title()
global storychosen
storychosen = input(Fore.WHITE + "\nPaste or type the contents of the story:\n")
begin()
downloadfunc()