-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvideos_to_gif_frontend.py
More file actions
155 lines (122 loc) · 4.48 KB
/
Copy pathvideos_to_gif_frontend.py
File metadata and controls
155 lines (122 loc) · 4.48 KB
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
# coding: utf-8
import Tkinter as tk
import ttk as ttk
import tkFileDialog, pysrt, os
import unicodecsv as csv
from slugify import slugify
from videos_to_gif import makeGif, striptags
from unidecode import unidecode
# TODO: right now we're magically toggling this
generate_csv = True
def onFrameConfigure(canvas):
canvas.configure(scrollregion=canvas.bbox("all"))
def onMousewheel(event):
# lol doesn't work on linux
canvas.yview_scroll(-1*(event.delta/120), "units")
def selectAll():
for button in sub_buttons:
button.select()
def generateGifs():
if video_path and subs:
to_generate_list = []
for index, item in enumerate(sub_list):
if (item.get() != 0):
item.set(0) # uncheck after it's been processed
to_generate_list.append(index)
else:
return
popup = tk.Toplevel()
tk.Label(popup, text="GIFs generating").grid(row=0,column=0)
progress = 0
progress_var = tk.DoubleVar()
progress_bar = ttk.Progressbar(popup, variable=progress_var, maximum=100)
progress_bar.grid(row=1, column=0)
popup.pack_slaves() # do what now?
progress_step = float(100.0/len(to_generate_list))
if generate_csv:
csvfile = open('names.csv', 'w')
fieldnames = ['index', 'filename', 'dialog']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
i = 0
for index in to_generate_list:
texts = []
starts = []
ends = []
filename = ""
sub = subs[index]
starts.append(str(sub.start).replace(',', '.'))
ends.append(str(sub.end - sub.start).replace(',', '.'))
texts.append(sub.text)
filename = format(i, '05') + '-' + slugify(unidecode(sub.text))
append_offset = index
while True:
if (merge_list[append_offset].get() != 0):
sub = subs[append_offset+1]
starts.append(str(sub.start).replace(',', '.'))
ends.append(str(sub.end - sub.start).replace(',', '.'))
texts.append(sub.text)
filename = filename + "-" + slugify(unidecode(sub.text))
append_offset += 1
else:
break
gif_filename = os.path.join(filename + ".gif")
if not os.path.exists(gif_filename):
try:
makeGif(video_path, starts, ends, texts, gif_filename)
except UnicodeEncodeError:
texts = map(lambda t: unidecode(t), texts)
makeGif(video_path, starts, ends, texts, gif_filename)
if generate_csv:
writer.writerow({'index': i, 'filename': gif_filename, 'dialog': sub.text})
popup.update()
progress += progress_step
progress_var.set(progress)
i+=1
popup.destroy()
if generate_csv:
csvfile.close()
subs = None
video_path = None
subtitle_path = None
root = tk.Tk()
root.title("Videos to Gif Frontend")
file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a subtitle file', filetypes = [("srt file", "*.srt")])
if file != None:
subs = pysrt.open(file.name)
file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a video file')
if file != None:
video_path = file.name
if video_path:
w = tk.Label(root, text="Possible gifs for: " + video_path)
w.pack()
if subtitle_path:
w = tk.Label(root, text="Subs for: " + subtitle_path)
w.pack()
b = tk.Button(root, text="Generate GIFs for Selected Quotes", command=generateGifs, bg="white")
b.pack()
b_all = tk.Button(root, text="Select All", command=selectAll, bg="white")
b_all.pack()
sub_list = []
merge_list = []
sub_buttons = []
index = 0
canvas = tk.Canvas(root, borderwidth=0)
frame = tk.Frame(canvas)
vsb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((4,4), window=frame, anchor="nw")
frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))
canvas.bind_all("<MouseWheel>", onMousewheel)
for sub in subs:
sub_list.append(tk.IntVar())
merge_list.append(tk.IntVar())
check = tk.Checkbutton(frame, text=striptags(sub.text), variable=sub_list[index])
sub_buttons.append(check)
check.grid(row=index, sticky=tk.W)
merge_check = tk.Checkbutton(frame, variable=merge_list[index])
merge_check.grid(row=index, sticky=tk.E)
index+=1
root.mainloop()