-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
216 lines (189 loc) · 7.82 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
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
from PyQt4 import QtGui, QtCore
import sys
import subprocess
import design
import os
import filecmp
import hashlib
from glob import glob
ffmpeg = 'ffmpeg'
config = os.path.dirname(os.path.abspath(sys.argv[0])) + '/config.txt'
override = 'n'
output = ''
class ExampleApp(QtGui.QMainWindow, design.Ui_MainWindow):
def __init__(self):
global ffmpeg
super(self.__class__, self).__init__()
self.setupUi(self) # This is defined in design.py file automatically
if os.path.isfile(config):
with open(config, "r") as fo:
ffmpeg = fo.readline()
try:
subprocess.call([ffmpeg, '-v', '0'])
except OSError:
print ('ffmpeg is not installed -'
'please set ffmpeg path in settings!')
msgBox = QtGui.QMessageBox()
msgBox.setText('FFmpeg is not installed - '
'Please select the ffmpeg executable')
ret = msgBox.exec_()
ffmpeg = QtGui.QFileDialog.getOpenFileName(self, "FFMPEG EXECUTABLE")
if not os.path.isfile(config):
with open(config, "w") as fo:
fo.write(ffmpeg)
global container
container = '.mkv' # default - find out a better way of doing this, like 'if not x'
self.batch_button.setEnabled(False)
self.radioButton.setChecked(True)
self.radioButton_2.toggled.connect(self.enable_button)
self.radioButton.toggled.connect(self.enable_inputButton)
self.checkBox_2.setChecked(True)
directory = self.btnBrowse.clicked.connect(self.browse_folder)
directory = self.batch_button.clicked.connect(self.browse_batch)
self.btnBrowse.clicked.connect(self.update_dir)
self.pushButton.clicked.connect(self.encode)
output = self.pushButton_3.clicked.connect(self.override_output)
self.pushButton_3.clicked.connect(self.update_output)
self.container_selection.activated[str].connect(self.on_combo_activated)
def enable_button(self):
self.batch_button.setEnabled(True)
self.btnBrowse.setEnabled(False)
def enable_inputButton(self):
self.batch_button.setEnabled(False)
self.btnBrowse.setEnabled(True)
def browse_folder(self):
global directory
directory = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
'',"Video files (*.mov *.mov *.mxf *.avi *.mkv *.mp4 *.wmv *.webm *.ogg *.ogm) ;; All Files(*)")
return directory
def browse_batch(self):
global directory
directory = QtGui.QFileDialog.getExistingDirectory(self, 'Open Directory')
return directory
def override_output(self):
global output
global override
output = QtGui.QFileDialog.getExistingDirectory(self, "Pick a file")
override = 'y'
return output
def on_combo_activated(self):
global container
container = self.container_selection.currentText()
def encode(self):
counter = 0
error = 0
success = 0
error_list = []
global output
if os.path.isfile(directory):
print os.path.isfile(directory)
print "single file found"
video_files = []
video_files.append(directory)
print video_files
elif os.path.isdir(directory):
os.chdir(directory)
video_files = glob('*.mov') + glob('*.mp4') + glob('*.mxf') + glob('*.mkv') + glob('*.avi')
print video_files
for video in video_files:
# Change this so that output will default if an entry isn't in override_output
if override == 'n':
output = str(video) + container
elif override == 'y':
print override
source_framemd5 = video + '.framemd5'
cmd = [str(ffmpeg),
'-i', str(video),
'-c:v', 'ffv1',
'-g', '1',
'-level', '3',
'-c:a', 'copy',
'-map', '0',
'-dn',
'-report',
'-slicecrc', '1',
'-slices', '16']
if override == 'n':
out = ''
out += str(video) + container
cmd += [str(out)]
elif override == 'y':
out = ''
out += output + '/' + str(os.path.basename(str(directory))) + container
cmd += [str(out)]
if not self.checkBox.isChecked():
cmd += ['-f', 'framemd5', '-an', str(source_framemd5)]
print cmd
try:
if directory:
subprocess.call(cmd)
except NameError:
msgBox = QtGui.QMessageBox()
msgBox.setText('Please select an input before encoding')
ret = msgBox.exec_()
if not self.checkBox.isChecked():
fmd5 = [str(ffmpeg), '-i', str(out), '-f', 'framemd5', '-an']
if override == 'n':
fmd5output = ''
fmd5output += str(output) + '.framemd5'
print fmd5output
fmd5 += [fmd5output]
elif override == 'y':
fmd5output = ''
fmd5output += str(output) + '/' + str(os.path.basename(str(out))) + '.framemd5'
fmd5 += [fmd5output]
print fmd5
subprocess.call(fmd5)
global output_parent_dir
global normpath
global relative_path
global dirname
global manifest_destination
output_parent_dir = os.path.dirname(str(out))
normpath = os.path.normpath(str(out))
relative_path = normpath.split(os.sep)[-1]
dirname = os.path.split(os.path.basename(str(out)))[1]
manifest_destination = '%s_manifest.md5' % dirname
if self.checkBox_2.isChecked():
m = hashlib.md5()
with open(str(out), 'rb') as f:
while True:
buf = f.read(2**20)
if not buf:
break
m.update(buf)
md5_output = m.hexdigest()
with open(manifest_destination, "wb") as fo:
fo.write(md5_output + ' ' + normpath.split(os.sep)[-1])
if filecmp.cmp(source_framemd5, fmd5output, shallow=False):
print ('YOUR FILES ARE LOSSLESS'
' YOU SHOULD BE SO HAPPY!!!')
counter += 1
success+= 1
else:
counter += 1
error += 1
error_list.append(video)
print 'Encode process completed'
msgBox = QtGui.QMessageBox()
msgBox.setText('You processed %d files \n Success:%d \n Failures:%d' % (counter, success, error))
ret = msgBox.exec_()
error_string = '\n'
print error
if error > 0:
for errors in error_list:
error_string += errors + '\n'
msgBox = QtGui.QMessageBox()
msgBox.setText('The following files are not lossless: %s ' % error_string)
ret = msgBox.exec_()
def update_dir(self):
self.filename_text.setText(directory)
def update_output(self):
self.lineEdit_2.setText(output)
def main():
app = QtGui.QApplication(sys.argv) # A new instance of QApplication
form = ExampleApp() # We set the form to be our ExampleApp (design)
form.show() # Show the form
app.exec_() # and execute the app
if __name__ == '__main__':
main() # run the main function