-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtogif.py
156 lines (119 loc) · 6.04 KB
/
togif.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
import os
import re
from moviepy.editor import *
import tkinter
from tkinter.messagebox import showerror, showwarning, askyesno
import customtkinter as ctk
import moviepy.video.fx.all as vfx
def change_appearance_mode_event(new_appearance_mode: str): # Function for the appearance change button
ctk.set_appearance_mode(new_appearance_mode)
def change_language(new_language: str): # Changing the UI language from a file
print(new_language)
if new_language == "English":
file = open("Eng_language", "r")
lines = file.readlines()
else:
with open('Ru_language', 'r', encoding='utf-8') as file:
lines = file.readlines()
# label1.configure(text="Make a gif")
label2.configure(text=lines[17])
entry1.configure(placeholder_text=lines[18])
button_gif.configure(text=lines[19])
add_text.configure(text=lines[20])
entry2.configure(placeholder_text=lines[21])
entry3.configure(placeholder_text=lines[22])
loop_switch.configure(values=[lines[23], lines[24]], font=("Ariel", 15), width=250, height=45)
def time_symetrize(clip): # Returns a clip that is time-symmetrized by concatenating the original clip with
# its time-reversed version.
return concatenate([clip, clip.fx(vfx.time_mirror)])
def fade_in(clip): # Applies a fade-in effect to the input clip by crossfading it from transparent
# to opaque over half of its duration.
d = clip.duration
clip = clip.crossfadein(d / 2)
composition = (CompositeVideoClip([clip,
clip.set_start(d / 2),
clip.set_start(d)])
.subclip(d / 2, 3 * d / 2))
return composition
def makegif(): # Function to create a GIF from a video clip with optional effects.
file_name = os.path.basename(entry1.get())
file_name = file_name.split('.')[0]
clip = (VideoFileClip(entry1.get(), audio=False)
.resize(0.6))
if loop_switch.get() == "Time-symetrization":
print("Time-symetrization")
clip.fx(time_symetrize)
elif loop_switch.get() == "Fade in":
print("Fade in")
clip = fade_in(clip)
if (add_text.get() == "on"):
clip = text_edit(clip)
gif_path = os.path.split(entry1.get())[0] + '\\' + str(file_name) + '.gif'
clip.write_gif(gif_path, program='imageio', fuzz=2, opt='wu')
clip.close()
def path_valid(path): # Checks if the entered path is a path to folder or file
print(path)
print(os.path.isdir(path))
print(os.path.exists(path))
if os.path.isdir(path) or os.path.exists(path):
button_gif.configure(state="normal")
else:
button_gif.configure(state="disabled")
# label1.configure(text=f"Duration:{clip.duration}")
return os.path.isdir(path)
def run(toplevel_window): # Function to set up and run the application GUI.
global frame, text_frame, label2, check, entry1, entry2, entry3, button_gif, add_text, font_menu, color_menu, loop_switch
toplevel_window.geometry(f"{350}x{330}")
frame = ctk.CTkFrame(master=toplevel_window) # main background frame
text_frame = ctk.CTkFrame(master=frame)
# Main Frame
label2 = ctk.CTkLabel(master=frame, text="Loop method:", font=ctk.CTkFont(size=11, weight="bold"))
check = toplevel_window.register(path_valid) # checks if input is correct
entry1 = ctk.CTkEntry(master=frame, placeholder_text="Path to mp4 file", validate="focusout",
validatecommand=(check, "%P"))
button_gif = ctk.CTkButton(master=frame, text="Make a Gif",
command=makegif) # button to download a Video
add_text = ctk.CTkCheckBox(master=frame, text="Add text", onvalue="on", offvalue="off",
command=enable_all_widgets)
font_menu = ctk.CTkOptionMenu(master=text_frame, state="disabled",
values=["Comic Sans", "Ariel", "Amiri-Bold"])
color_menu = ctk.CTkOptionMenu(master=text_frame, state="disabled", values=["white", "black"])
entry2 = ctk.CTkEntry(master=text_frame, state="disabled", placeholder_text="Position x,y",
validate="focusout")
entry3 = ctk.CTkEntry(master=text_frame, state="disabled", placeholder_text="Text")
loop_switch = ctk.CTkSegmentedButton(master=frame, values=["Fade in", "Time-symetrization"])
def appear(toplevel_window,language):
frame.grid(row=0, column=1, rowspan=2, padx=5, sticky="nsew")
frame.grid_rowconfigure(4, weight=1)
text_frame.grid(row=2, column=0, rowspan=2, padx=5, pady=5, sticky="nsew")
entry1.grid(row=0, padx=10, pady=(10, 10), sticky="nsew", columnspan=2)
add_text.grid(row=1, column=0, padx=5)
font_menu.grid(row=2, column=0, padx=5)
color_menu.grid(row=2, column=1, padx=5, pady=(10, 10))
entry2.grid(row=3, column=0, padx=20, pady=10)
entry3.grid(row=3, column=1, padx=0, pady=10)
label2.grid(row=4, column=0, padx=10, pady=(5, 5), columnspan=2)
loop_switch.grid(row=5, column=0, padx=10, pady=5, columnspan=2)
button_gif.grid(row=7, column=0, padx=20, pady=10, sticky="nsew", columnspan=2)
change_language(language)
toplevel_window.resizable(False, False)
toplevel_window.mainloop()
def text_edit(clip): # Adds text to the input video clip.
pos_x = entry2.get().split(',')[0]
pos_y = entry2.get().split(',')[1]
print(pos_x)
print(pos_y)
text = (TextClip(entry3.get(),
fontsize=30, color=color_menu.get(),
font=font_menu.get(), interline=-25)
.set_pos((pos_x, pos_y))
.set_duration(clip.duration))
composition = CompositeVideoClip([clip, text])
return composition
def enable_all_widgets(): # Enables or disables all widgets based on the state of the "Add text" checkbox.
print(add_text.get())
for child in text_frame.winfo_children():
if (add_text.get() == "on"):
child.configure(state='normal')
else:
child.configure(state='disabled')