-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
152 lines (119 loc) · 6.54 KB
/
main.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
import os
import subprocess
import glob
import time
import shutil
import random
from colorama import Fore, Back, Style, init
path_to_videos = 'Input' # Path Directory
path_out_to_videos = 'Output' # Path Directory
video_extensions = ['.mp4', '.avi', '.mkv', '.mov'] # Videos Extensions Allowed
def main():
init()
# Search All Video in Input Path
search_pattern = f"{path_to_videos}/*[{'|'.join(video_extensions)}]"
video_files = glob.glob(search_pattern) # All Files Name
subdirectories = [subdir for subdir in os.listdir(path_to_videos) if os.path.isdir(os.path.join(path_to_videos, subdir))] # Sub Directories
if not subdirectories and not video_files: # If there is no video available
print(Fore.RED + "The input folder cannot be empty!" + Style.RESET_ALL)
exit()
# noinspection PyBroadException
try:
random_video = input("Random WaterMark Number (Default 0 (Without Random) - Just Number): " + Fore.GREEN) # WaterMark Lists (Phone Numbers)
random_video = 0 if random_video == '' else int(random_video)
user_text = input("Your Phone Number (list.txt): " + Fore.GREEN) # WaterMark Lists (Phone Numbers)
user_text = 'list.txt' if user_text == '' else user_text
user_time = input(Style.RESET_ALL + "Time to change the text (Just Number/Second - Default 60 Second) : " + Fore.GREEN)
user_time = 60 if user_time == '' else int(user_time)
user_time = user_time if user_time != 0 else 60 # 60 Second
user_opacity = input(Style.RESET_ALL + "Please Enter Opacity (Between 0.0 to 1.0 - Default 0.2) : " + Fore.GREEN)
user_opacity = 0.2 if user_opacity == '' else float(user_opacity)
user_opacity = user_opacity if (1.0 >= user_opacity > 0) else 0.5
user_size = input(Style.RESET_ALL + "Please Enter Font Size (Default 22) : " + Fore.GREEN)
user_size = 22 if user_size == '' else int(user_size)
user_size = user_size if (80 >= user_size >= 6) else 16
user_color = input(Style.RESET_ALL + "Please Enter Text Color (hex => #ffffff Or color => white - default white) : " + Fore.GREEN)
user_color = 'white' if user_color == '' else user_color
user_bg_color = input(Style.RESET_ALL + "Please Enter Background Color (hex => #000000 or color => black - default black) : " + Fore.GREEN)
user_bg_color = 'black' if user_bg_color == '' else user_bg_color
print(Style.RESET_ALL)
except:
print('')
print(Back.BLACK + Fore.RED + 'Be sure to pay attention to the type of value you enter' + Style.RESET_ALL)
exit()
lines = []
if os.path.exists(user_text): # Check Existed Text User (UserFile => list.txt)
with open(user_text, 'r') as file:
for line in file:
cleaned_line = line.strip()
lines.append(cleaned_line)
else:
lines.append('WaterMark')
if video_files:
for line in lines:
if not os.path.exists(f"{path_out_to_videos}/{line}"):
os.mkdir(f"{path_out_to_videos}/{line}")
loopWaterMark(video_files, line, user_size, user_color, user_opacity, user_time, user_bg_color, random_video)
if subdirectories:
for line in lines:
if not os.path.exists(f"{path_out_to_videos}/{line}"):
os.mkdir(f"{path_out_to_videos}/{line}")
for subdir in subdirectories:
input_subdir = os.path.join(path_to_videos, subdir)
output_subdir = os.path.join(f"{path_out_to_videos}/{line}", subdir)
if not os.path.exists(output_subdir):
os.makedirs(output_subdir)
video_files = glob.glob(os.path.join(input_subdir, f"*[{'|'.join(video_extensions)}]"))
if not video_files:
print(Fore.RED + f"No video files found in {input_subdir}" + Style.RESET_ALL)
continue
loopWaterMark(video_files, line, user_size, user_color, user_opacity, user_time, user_bg_color, random_video)
all_files = os.listdir(input_subdir)
for file_name in all_files:
source_file_path = os.path.join(input_subdir, file_name)
destination_file_path = os.path.join(output_subdir, file_name)
file_extension = os.path.splitext(file_name)[1]
if file_extension not in video_extensions:
if os.path.isfile(source_file_path):
shutil.copy(source_file_path, destination_file_path)
elif os.path.isdir(source_file_path):
shutil.copytree(source_file_path, destination_file_path)
print("Finish")
def loopWaterMark(videos_file, line, user_size, user_color, user_opacity, user_time, user_bg_color, random_waterMark=0):
all_video = []
videos_not_selected = []
if random_waterMark:
number_all_videos = len(videos_file)
if random_waterMark > number_all_videos:
print(Back.BLACK + Fore.RED + 'The random number is larger than all videos' + Style.RESET_ALL)
exit()
all_video = random.sample(videos_file, random_waterMark)
videos_not_selected = [vid for vid in videos_file if vid not in all_video]
else:
all_video = videos_file
for video in all_video:
# Changing the position of the text according to the user's second as animation
ffmpeg_cmd = [
"ffmpeg",
"-i", video,
"-vf",
f"drawtext=text='{line}':fontsize={user_size}:fontcolor={user_color}@{user_opacity}:fontfile=Fonts/IRANSans.ttf"
f":x='if(eq(mod(t,{user_time}),0),rand(0,(w-text_w)),x')"
f":y='if(eq(mod(t,{user_time}),0),rand(0,(h-text_h)),y)'"
# f":x='if(gte(t,{user_time}), (w-text_w)-mod((t-{user_time})*15, (w-text_w)), (w-text_w)/2)'"
# f":y='if(gte(t,{user_time}), (h-text_h)-mod((t-{user_time})*15, (h-text_h)), (h-text_h)/2)'"
f":box=1:boxcolor={user_bg_color}@{user_opacity}:boxborderw=10",
"-c:a", "copy",
video.replace(path_to_videos, (path_out_to_videos + f"\\{line}"))
]
subprocess.run(ffmpeg_cmd)
print(" ")
print(Fore.GREEN + f"{video} complete!" + Style.RESET_ALL)
print(" ")
time.sleep(2)
if len(videos_not_selected) != 0:
for video in videos_not_selected:
output = video.replace(path_to_videos, (path_out_to_videos + f"\\{line}"))
shutil.copy(video, output)
if __name__ == "__main__":
main()