-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRhubarbBarbara.py
369 lines (276 loc) · 10.1 KB
/
RhubarbBarbara.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#RhubarbBarbara is a Python tool for using Rhubarb Lip Sync in Production, without the need of Software like AE, OpenToonz etc.
#by creating videos out of the data provided
#by Christoferis GNU GPL v3 (https://www.sites.google.com/view/christoferis)
#trying to use a minimal amount of non stock dependencies
import json
import tkinter as tk
from os import mkdir
from random import random
from subprocess import PIPE, Popen
from tkinter import filedialog as fd
from tkinter import messagebox as mb
from moviepy.editor import AudioFileClip, ImageClip, concatenate_videoclips
from PIL import Image, ImageTk
#Todo:
#V.1:
# - Loading bar
# - About Page, icon + info
# - FPS Option
# optional
# - Path Information display at image thing
# - Better UI
# - freeze
#vars
output = str()
audiopath = str()
savepath = str()
config = None
window = None
def about():
abt = tk.Toplevel()
#Creator, and Thanks
tk.Label(abt, text='''
RhubarbBarbara, an application by Christoferis\n
written in Python 3.7.8, Version 1.0\n
This App uses parts from Rhubarb Lip Sync by Daniel S. Wolf(https://github.com/DanielSWolf/rhubarb-lip-sync) and MoviePy by Zulko(https://github.com/Zulko/moviepy) \n
This App is maintained by Christoferis but feel free to help out!\n\n
Repo: https://github.com/Christoferis/RhubarbBarbara
Submit an Issue on Github if a problem persists or view the website (https://sites.google.com/view/christoferis/code-projects/rhubarbbarbara) for Troubleshooting measures.
'''
).pack()
def videoImage(data):
#function version of prior videoImage Class (Problems with class instances)
return ImageClip(img=mouthSelector.mouthshapes[data["value"]], transparent=True, duration=float(data["end"]) - float(data["start"]))
#Rhubarb integration
def rhubarb():
global audiopath
global config
#get rhubarb path
rhu = config["rhubarb"]
#check for extended shapes
extshapes = ""
for shape in mouthSelector.mouthshapes:
#g
if shape == "G":
extshapes += "G"
#h
elif shape == "H":
extshapes += "H"
#x
elif shape == "X":
extshapes += "X"
else:
pass
print(extshapes)
#open a cmd instance of Rhubarb
cmd = Popen([rhu, "-f", "json", "--extendedShapes", extshapes, audiopath], stdout=PIPE)
result = cmd.communicate()
return json.loads(result[0])["mouthCues"]
#class for mouthselector thingy
class mouthSelector:
mouthshapes = dict()
def __init__(self, window, mouthshape):
self.window = window
self.logo = mouthshape
self.button = None
#mouthshape selector:
if mouthshape in ("A","B","C","D","E","F"):
self.ext = False
else:
self.ext = True
if mouthshape == "A":
self.image = "icons\lisa-A.png"
elif mouthshape == "B":
self.image = "icons\lisa-B.png"
elif mouthshape == "C":
self.image = "icons\lisa-C.png"
elif mouthshape == "D":
self.image = "icons\lisa-D.png"
elif mouthshape == "E":
self.image = "icons\lisa-E.png"
elif mouthshape == "F":
self.image = "icons\lisa-F.png"
elif mouthshape == "G":
self.image = "icons\lisa-G.png"
elif mouthshape == "H":
self.image = "icons\lisa-H.png"
elif mouthshape == "X":
self.image = "icons\lisa-X.png"
pass
def create_wid(self):
shape = tk.Frame(self.window)
#mouthshape îdentifier
if self.ext == True:
prevstr = self.logo + "\n " + "(Optional)"
else:
prevstr = self.logo
tk.Label(shape, text=prevstr).pack(side="left")
#Mouthshape preview (https://github.com/DanielSWolf/rhubarb-lip-sync#readme)
img = ImageTk.PhotoImage(Image.open(self.image).resize((75, 62)))
imgframe = tk.Label(shape, image=img)
imgframe.image = img
imgframe.pack(side="left")
#button to remove image
rembutton = tk.Button(shape, text="X", command=self.remove_from_dict)
rembutton.pack(side="right", fill="both")
#button to open filedialog
self.button = tk.Button(shape, text="Add an Image", command=self.add_to_dict)
self.button.pack(side="right", fill="both")
return shape
def remove_from_dict(self):
#reset button color
self.button.config(background="#F0F0F0", foreground="black")
#remove from dict
try:
del mouthSelector.mouthshapes[self.logo]
except KeyError:
pass
pass
def add_to_dict(self):
#add in only png images / jpg
shape = fd.askopenfilename(defaultextension=".png")
if shape != "":
#open and append to dict
mouthSelector.mouthshapes[self.logo] = shape
#set button color
self.button.config(background="blue", foreground="white")
else:
print(shape)
def process():
global output
global audiopath
#find the codec
mouthData = rhubarb()
if output.find(".mp4") >= 0:
codec = "libx264"
video = True
elif output.find(".avi") >= 0:
codec = "png"
video = True
#add in image sequence format and convert to path + img identifier using the foldername
elif output.find(".folder"):
#make directory
output = output.replace(".folder", "")
mkdir(output)
#make identifier + complete output path
ident = output.split("/")
#complete output
output = output + "\\" + ident[len(ident) - 1] + "%06d.png"
video = False
imageclips = list()
for data in mouthData:
imageclips.append(videoImage(data))
#concatenate all
final = concatenate_videoclips(imageclips, method="compose")
#Render out, if video True = Render video and add audio, else render imagesequence
if video == True:
final = final.set_audio(AudioFileClip(audiopath))
final.write_videofile(output, codec=codec, fps=60)
elif video == False:
final.write_images_sequence(fps=60, withmask=True, nameformat=output)
#startmethod + checker
def start():
#flag int for keeping missing things at minimum
global audiopath
global output
flag = int(0)
#check images (for the important ones)
for shape in ("A","B","C","D","E","F"):
if shape not in mouthSelector.mouthshapes:
flag += 1
break
#check audiopath + output
if output == "":
flag += 1
if audiopath == "":
flag += 1
#endcheck
print(flag)
if flag > 0:
#show error screen
tk.messagebox.showinfo(title="You forgot something!",
message=
'''
You can't start yet!
Parts are incomplete, please correct them and try again
if this Problem persists please open an issue on GitHub"
''' + " " + str(flag))
else:
process()
pass
#Filegrabber for arbitrary files
def get_path(widget, type):
global audiopath
global output
#reset display widget and add in path info
#audio for audio path, output for output path
if type == "audio":
path = fd.askopenfilename(defaultextension=".wav", filetypes=[('WAV-Audio(*.wav)', '*.wav')])
if path != '':
widget.set(path)
audiopath = path
else:
pass
elif type == "output":
path = fd.asksaveasfilename(defaultextension=".avi", filetypes=
[('MP4-Video(*.mp4)', '*.mp4'), ("AVI-Video(*.avi)", "*.avi"), ('Image Sequence(*.*)', '*.folder')]
)
if path != '':
widget.set(path)
output = path
else:
pass
#GUI
def gui(window, stdpath):
#about page
tk.Button(window, text="Credits + Help", command=about).pack(side="top")
#make main Frame for the images
top = tk.Frame(window)
for shape in ("X","H","G","F","E","D","C","B","A"):
frame = mouthSelector(window=top, mouthshape=shape)
frame.create_wid().pack(side="bottom")
top.pack(expand=2, pady=10, fill="both")
#Frame for the start Button and Audio Input
bottom = tk.Frame(window)
#audio Entry box + Stringvar
audiopath = tk.StringVar()
audiopath.set("Path to Audio")
audio = tk.Entry(bottom, state="readonly", textvariable=audiopath)
audio.pack(side="left", expand=1, fill="x")
#outputpath
outputpath = tk.StringVar()
outputpath.set(stdpath)
output = tk.Entry(bottom, state="readonly", textvariable=outputpath)
output.pack(side="left", expand=1, fill="x")
#start button
tk.Button(bottom, text="start", background="green", foreground="white", command=start).pack(side="right", fill="both")
#search file buttons
#Audio File Button
tk.Button(bottom, text="Open Audio File", command=lambda: get_path(audiopath, type="audio")).pack(side="right", fill="both")
#Output Button
tk.Button(bottom, text="Set Output", command=lambda: get_path(outputpath, type="output")).pack(side="bottom", fill="both")
bottom.pack(side="bottom", fill="both", expand=2, pady= 50, padx=50)
#Main Function
def main():
#open config file and fetch and make random name
global config
global output
global window
config = json.loads(open("config.json", mode="r").read())
#standard config
if config["standard-savepath"] != "":
standard = config["standard-savepath"] + "/" + str(round(random()*100000 + 1)) + ".avi"
output = standard
else:
standard = "Outputpath"
#construct main window and call GUI
window = tk.Tk()
window.title("RhubarbBarbara by Christoferis (v1.0)")
gui(window=window, stdpath=standard)
window.mainloop()
pass
try:
main()
except Exception as e:
mb.showwarning(message="A problem occured, check Error message for details, and if issue persists, make an Github Issue \n(https://github.com/christoferis/RhubarbBarbara) \n\n" + str(e))
exit()