def __init__(self, emoji=None): """ This class allows you to add() video files and convert them to gifs. Output files are going to be in the same folder as the input files. """ super(Handbrake, self).__init__() self.tp = TasksPool() self.tp.return_signal.connect(lambda x: self.return_signal.emit(x)) # If we supply Emoji object, then if isinstance(emoji, Emoji): video_file = abspath(join(emoji.folder, emoji.name + Config()()['name_delimiter'] + emoji.version + '.mov')) if not exists(video_file): video_file = abspath(join(emoji.folder, emoji.name + '-' + emoji.version + '.mov')) if exists(video_file): self.add(video_file) self.run() else: error_msg = 'Failed to locate {}, based on {}.'.format(video_file, emoji.name_no_ext) self.return_signal.emit(__name__ + error_msg) logger.warning(error_msg) error_box = QMessageBox() error_box.setStyleSheet(stylesheet.houdini) error_box.setWindowTitle('Handbrake mov to mp4 conversion: File error') error_box.setText(error_msg) error_box.exec_() elif isinstance(emoji, str): video_file = emoji if exists(video_file): self.add(video_file) self.run()
def showMessageBox(self, msg): warning = QMessageBox(self) warning.setFont(View.labelsFont()) warning.setStyleSheet('QMessageBox {background: white}') warning.setWindowTitle("Error") warning.setText(msg) warning.setIcon(QMessageBox.Warning) warning.addButton("Ok", QMessageBox.AcceptRole).setFont(View.editsFont()) warning.exec_()
def __submit(self): _res = dict() for t_id in self.widgets: _res[t_id] = self.get_data(t_id) # Guarantee input is not null if self.widgets[t_id]["ESSENTIAL"] and is_null(_res[t_id]): _message = u"%s不可以为空" % self.widgets[t_id]["NAME"] _msg_box = QMessageBox(QMessageBox.Warning, "Alert", _message) _msg_box.setStyleSheet(get_theme("Buttons")) _msg_box.exec_() return self.sig_submit[dict].emit(_res) self.close()
def to_damaged(self, emoji, lossy_factor, color_map): # Create filesize dict filesizes = {} l_filesizes = [] # If there is a lossy gif, then: if exists(emoji.lossy_path): # If lossy gif is over the damaged_filesize limit, then: lossy_filesize = getsize(emoji.lossy_path) if lossy_filesize > conf_damaged_filesize: # Make first damaged lossy_factor = 400 self.add(input_file=emoji.gif_path, lossy_factor=lossy_factor, color_map=color_map, delay=emoji.delay, output_file=emoji.damaged_path) self.run() damaged_filesize = getsize(emoji.damaged_path) if damaged_filesize > conf_damaged_filesize: logger.info( 'Lossy factor of 400 is still not enough. Drop file') else: filesizes[lossy_factor] = damaged_filesize lossy_factor = 0 self.tp = TasksPool() self.add(input_file=emoji.gif_path, lossy_factor=lossy_factor, color_map=color_map, delay=emoji.delay, output_file=emoji.damaged_path) self.run() damaged_filesize = getsize(emoji.damaged_path) filesizes[lossy_factor] = damaged_filesize f_list = sorted( list(zip(filesizes.keys(), filesizes.values()))) x_distance = f_list[1][0] - f_list[0][0] # Change in lossy y_distance = f_list[1][1] - f_list[0][ 1] # Change in filesize delta_x = y_distance / x_distance # Change in y over one x # Lossy prediction = 500Kb-filesize of first point / delta_x(fsize decline over one lossy factor) + # x offset of first point x = ((conf_damaged_filesize - f_list[0][1]) / delta_x) + f_list[0][0] lossy_factor = int(x) endless_loop_check = {'+': 0, '-': 0} # How precise do we want to get to conf_damaged_filesize (500kb) filesize_headroom = -1024 * 5 while damaged_filesize > conf_damaged_filesize: self.loop(emoji, lossy_factor, color_map) # Get new filesize and difference damaged_filesize = getsize(emoji.damaged_path) filesize_difference = damaged_filesize - conf_damaged_filesize logger.info( 'Lossy: {}, Current filesize: {} bytes, ' 'Needed filesize {} bytes, Difference {} bytes'. format(lossy_factor, damaged_filesize, conf_damaged_filesize, filesize_difference)) # If filesize is 10Kb too less if lossy_factor > 400: logger.info( 'Lossy factor of 400 is still not enough. Drop file' ) error_box = QMessageBox() error_box.setStyleSheet(stylesheet.houdini) error_box.setWindowTitle('Lossy number error') error_box.setText( 'While exporting DAMAGED version we have hit 400, try exporting with 0-100' ) error_box.exec_() break # Start the loop of processing if filesize_difference < filesize_headroom: # Do more quality and more size lossy_factor -= 2 # Make the while pass the size test damaged_filesize = conf_damaged_filesize + 1 # Count how much have we decreased lossy factor endless_loop_check['-'] += 1 else: # Do less quality and less size lossy_factor += 1 # Count how much have we increased lossy factor endless_loop_check['+'] += 1 # If endless loop detected, than double the headroom if endless_loop_check['+'] >= 3 and endless_loop_check[ '-'] >= 3: # Reset the counter endless_loop_check = {'+': 0, '-': 0} # Double the headroom filesize_headroom *= 2 logger.debug( 'Endless loop detected, new headroom is: {} bytes' .format(filesize_headroom)) else: logger.info( 'Lossy file is {} Kb, there is no need for damaged file'. format(lossy_filesize))