-
Notifications
You must be signed in to change notification settings - Fork 0
/
mq.py
277 lines (235 loc) · 10.7 KB
/
mq.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
#!/usr/bin/env python3
#
# Movie Quantizer
# © Fabio Marzocca - 2017
#
# Notes: requires ffmpeg
import tkinter
from tkinter import ttk, filedialog, StringVar, Spinbox, Frame, Message, Toplevel
import tkinter.messagebox as mbox
import sys
import subprocess
import os
import threading
from get_ffmpeg import get_ffmpeg
VERSION = '1.0.0'
FFMPEG_BIN = "ffmpeg"
class MQ(ttk.Frame):
# The gui and functions.
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
self.movieLabeltext = StringVar()
self.moviefile = ""
self.outfolder = ""
self.fileloaded = False
self.folderloaded = False
self.movieLabeltext.set("<none>")
self.movieDuration = 0.0
self.root = parent
userDir = os.path.expanduser('~')
self.path_OSX = os.path.join(
userDir, 'Library', 'Application Support', 'Movie Quantizer')
self.init_gui()
def on_quit(self):
# Exits program.
sys.exit()
def go_nogo(self):
# check if ffmpeg is installed
if (self.checkifffmpeg() == False):
answer = mbox.askyesno("ERROR", "This application needs ffmpeg "
"installed in the system!\n\n"
"Do you want to install it?")
if answer == True:
root.update()
self.tryGettingffmpeg()
return
self.on_quit()
return
# Get the movie file
def loadmovie(self):
opts = {}
opts['filetypes'] = [('Supported types', ("*.mov", "*.mp4", "*.mpg", "*.avi",
"*.h264", "*.mpeg", "*.mkv", "*.m4a",
"*.3gp", "*.ogg", "*.wmv", "*.vob"))]
self.moviefile = filedialog.askopenfilename(**opts)
if self.moviefile != "":
self.movie_button['text'] = os.path.basename(self.moviefile)
self.fileloaded = True
self.checkDuration()
self.interval_spin['state'] = 'normal'
self.calculate_images()
if (self.fileloaded and self.folderloaded) == True:
self.action_button['state'] = 'normal'
# Get informations and duration of the movie
def checkDuration(self):
cmd = "'"+FFMPEG_BIN + "' -i " + "'" + self.moviefile + "'" + " 2>&1 | grep Duration"
c = os.popen(cmd)
out = c.read()
self.answer_label['text'] = "Movie info:" + out
c.close()
dur = out.index("Duration:")
duration = out[dur + 10:dur + out[dur:].index(",")]
hh, mm, ss = map(float, duration.split(":"))
self.movieDuration = (hh * 60 + mm) * 60 + ss
def selectFolder(self):
self.outfolder = filedialog.askdirectory()
if self.outfolder != "":
self.folder_button['text'] = os.path.basename(self.outfolder)
self.folderloaded = True
if (self.fileloaded and self.folderloaded) == True:
self.action_button['state'] = 'normal'
def checkifffmpeg(self):
global FFMPEG_BIN
try:
devnull = open(os.devnull)
subprocess.call(['ffmpeg', '-version'], stderr=subprocess.STDOUT,
stdout=devnull)
FFMPEG_BIN = "ffmpeg"
except (OSError, ValueError, subprocess.CalledProcessError):
try:
subprocess.call([self.path_OSX + '/ffmpeg.osx', '-version'],
stderr=subprocess.STDOUT, stdout=devnull)
FFMPEG_BIN = self.path_OSX + "/ffmpeg.osx"
except (OSError, ValueError, subprocess.CalledProcessError):
return False
self.set_ffmpegversion()
return True
def tryGettingffmpeg(self):
global FFMPEG_BIN
self.progLabelText = "Downloading ffmpeg. Please wait.."
self.progress_win()
root.update()
t1 = threading.Thread(target=get_ffmpeg, args=(self,))
t1.start()
FFMPEG_BIN = self.path_OSX + "/ffmpeg.osx"
def set_ffmpegversion(self):
cmd = [FFMPEG_BIN, " -version"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out, err = p.communicate()
lines = out.splitlines()
ver = lines[0].decode('utf8').split(
'version', 1)[-1].split('Copyright')[0]
self.answer_label['text'] = "Movie Quantizer v." + \
VERSION + " - using ffmpeg v." + ver
def calculate_images(self):
images = int(float(self.movieDuration) /
float(self.interval_spin.get()))
self.images_nrs['state'] = 'normal'
self.images_nrs.delete(0, 'end')
self.images_nrs.insert(0, images)
self.images_nrs['state'] = 'disabled'
def execute(self):
# Grab the images
format = self.image_formatbox.get()
result = (os.system('ls "' + self.outfolder + '"/img*.' + format))
if result == 0:
answer = mbox.askyesno(
"Warning", "Folder already contains image files, that will be overwritten. Proceed?")
if answer == False:
return
root.update()
self.progLabelText = "Processing images. Please wait.."
self.progress_win()
t1 = threading.Thread(target=self.exec_thread)
t1.start()
def exec_thread(self):
format = self.image_formatbox.get()
if format == "tiff":
pixformat = " -pix_fmt rgb565 "
else:
pixformat = " "
fps = float(1 / float(self.interval_spin.get()))
cmd = "'"+FFMPEG_BIN + "' -i '" + self.moviefile + "'" + pixformat + "-vf fps=" +\
str(fps) + " '" + self.outfolder + "'/img%04d." + format + " 2>&1"
try:
subprocess.check_call(cmd, shell=True)
self.popup.destroy()
cnt = subprocess.check_output(
'ls "' + self.outfolder + '"/img*.' + format + ' | wc -l', shell=True)
self.answer_label['text'] = "Result: " +\
(cnt.decode('ascii').strip()) +\
" image files have been successfully created in " + self.outfolder
return
except subprocess.CalledProcessError as e:
self.answer_label['text'] = e.output
self.popup.destroy()
return
def progress_win(self):
self.popup = Toplevel(self)
progress_bar = ttk.Progressbar(self.popup, orient='horizontal',
mode='determinate', takefocus=True, length=400)
progress_bar.grid(row=1, column=0, sticky="we")
self.progLabel = ttk.Label(self.popup, text=self.progLabelText).grid(column=0, row=0,
sticky="we")
self.popup.attributes('-topmost', True)
progress_bar.start(50)
def init_gui(self):
# Builds GUI.
self.root.title('Movie Quantizer')
self.root.option_add('*tearOff', 'FALSE')
self.grid(column=0, row=0, sticky='nsew')
self.menubar = tkinter.Menu(self.root)
self.menu_file = tkinter.Menu(self.menubar)
self.menu_file.add_command(label='Open Movie File...',
command=self.loadmovie)
self.menu_file.add_command(label='Select Output Folder...',
command=self.selectFolder)
self.menu_file.add_command(label='Exit', command=self.on_quit)
self.menubar.add_cascade(menu=self.menu_file, label='File')
self.root.config(menu=self.menubar)
self.movie_button = ttk.Button(self, text='Select',
command=self.loadmovie)
self.movie_button.grid(column=1, row=2)
self.interval_spin = Spinbox(self, from_=0.5, to=20.0,
increment=0.5, width=4, fg="red", command=self.calculate_images,
state='disabled')
self.interval_spin.grid(column=1, row=3, sticky="e")
self.folder_button = ttk.Button(self, text='Select',
command=self.selectFolder)
self.folder_button.grid(column=3, row=2)
self.images_nrs = ttk.Entry(self, text='', width=5,
state='disabled')
self.images_nrs.grid(column=3, row=3, sticky="e")
self.box_value = StringVar()
self.image_formatbox = ttk.Combobox(self, textvariable=self.box_value,
state='readonly', width=4, justify='center')
self.image_formatbox['values'] = ("jpg", "tiff")
self.image_formatbox.current(0)
self.image_formatbox.grid(column=1, row=4, sticky="w")
self.action_button = ttk.Button(self, text='Grab Images',
command=self.execute, state='disabled')
self.action_button.grid(column=0, row=5, columnspan=4)
self.answer_frame = Frame(self, height=300, background="white")
self.answer_frame.grid(column=0, row=6, columnspan=4, sticky='nesw')
self.answer_label = Message(
self.answer_frame, text='', justify='center', width=600)
self.answer_label.grid(column=0, row=0)
# Labels that remain constant throughout execution.
ttk.Label(self, text=' ').grid(column=0, row=0,
columnspan=4)
ttk.Label(self, text='Movie File:').grid(column=0, row=2,
sticky='w')
ttk.Label(self, text='Output Folder:').grid(column=2, row=2,
sticky='w')
ttk.Label(self, text="Grab a frame each (sec.):").grid(column=0, row=3,
sticky='w')
ttk.Label(self, text="Nr. of images:").grid(column=2, row=3,
sticky='w')
ttk.Label(self, text="Images format:").grid(column=0, row=4,
sticky='w')
for child in self.winfo_children():
child.grid_configure(padx=7, pady=7)
def about_dialog():
mbox.showinfo("About Movie Quatizer",
"A movie Frame Grabber\n©2017 - Fabio Marzocca\n\nVersion: " + VERSION, parent=root)
if __name__ == '__main__':
root = tkinter.Tk()
app = MQ(root)
root.resizable(False, False)
root.createcommand('tkAboutDialog', about_dialog)
#os.environ['PATH'] += '/usr/local/bin'
os.environ[
'PATH'] = '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin' + os.environ['PATH']
root.after(0, app.go_nogo)
root.mainloop()