def execute(cmd_tokens): with open(HISTORY_PATH, 'a') as history_files: history_files.write(' '.join(cmd_tokens) + os.linesep) if cmd_tokens: cmd_name = cmd_tokens[0] cmd_args = cmd_tokens[1:] if cmd_name in built_in_cmds: return built_in_cmds[cmd_name](cmd_args) signal.signal(signal.SIGINT, handler_kill) exec_cmd(' '.join(cmd_tokens)) return SHELL_STATUS_RUN
def main(command, unit='s'): with timeme(unit=unit) as t: info_lines, err_lines = exec_cmd(command) color.print_info('\n'.join(info_lines)) color.print_err('\n'.join(err_lines)) color.print_info(t)
def fileformat(path): with open(path, 'rb') as binfile: # 必需二制字读取 tl = highBytes_typeDict fformat = UNKNOWN_TYPE for hcode in tl.keys(): numOfBytes = len(hcode) // 2 # 需要读多少字节 binfile.seek(0) # 每次读取都要回到文件头,不然会一直往后读取 hbytes = struct.unpack_from("B" * numOfBytes, binfile.read(numOfBytes)) # 一个 "B"表示一个字节 if each_same(hexStr_bytesIter(hcode), hbytes): fformat = tl[hcode] break if fformat == UNKNOWN_TYPE: # continue to recognise if has_proper_ffprobe(): cmd = 'ffprobe -v quiet -print_format json -show_format -show_streams %s' % path info_lines, _ = exec_cmd(cmd) s = '\n'.join(info_lines) json_obj = json.loads(s) try: normal_name = json_obj['streams'][0]['codec_name'] ext_name = json_obj['format']['format_name'] except KeyError: pass else: fformat = FileTypePair(normal_name, ext_name) return fformat
def check_module(module_name, install_name=''): """ check if the module exist, if not exist try to install by pip (you can provide the install name manually) :param module_name: :param install_name: default equals module name :return: """ try: import_module(module_name) except ImportError: color.print_warn(module_name, 'Not Exists') pip_name = '' if ispython3(): pip_name = 'pip3' elif ispython2(): pip_name = 'pip' color.print_info( 'Now, try to install through {}, wait please...:)'.format( pip_name)) if install_name in ('', None): install_name = module_name info_lines, err_lines = exec_cmd('{0} install {1}'.format( pip_name, install_name)) print('\n'.join(info_lines)) if len(err_lines) != 0: print(''.join(err_lines)) else: pass
def fileformat(path): with open(path, 'rb') as binfile: # 必需二制字读取 tl = highBytes_typeDict fformat = UNKNOWN_TYPE for hcode in tl.keys(): numOfBytes = len(hcode) // 2 # 需要读多少字节 binfile.seek(0) # 每次读取都要回到文件头,不然会一直往后读取 hbytes = struct.unpack_from( "B" * numOfBytes, binfile.read(numOfBytes)) # 一个 "B"表示一个字节 if each_same(hexStr_bytesIter(hcode), hbytes): fformat = tl[hcode] break if fformat == UNKNOWN_TYPE: # continue to recognise if has_proper_ffprobe(): cmd = 'ffprobe -v quiet -print_format json -show_format -show_streams %s' % path info_lines, _ = exec_cmd(cmd) s = '\n'.join(info_lines) json_obj = json.loads(s) try: normal_name = json_obj['streams'][0]['codec_name'] ext_name = json_obj['format']['format_name'] except KeyError: pass else: fformat = FileTypePair(normal_name, ext_name) return fformat
def cut(fn, output, start_time, end_time, debug=False): if not assert_output_has_ext(output): color.print_err('Failed.') return start_time_int = video_time_str2int(start_time) end_time_int = video_time_str2int(end_time) long = end_time_int - start_time_int if long <= 0: color.print_err('end-time:%s is before than start-time:%s' % (end_time, start_time)) return fn_tmp = path2uuid(fn) output_tmp = path2uuid(output, rename=False, quiet=True) try: cmd = 'ffmpeg -ss %d -i "%s" -t %d -c:v copy -c:a copy "%s" ' \ % (start_time_int, fn_tmp, long, output) info_lines, err_lines = exec_cmd(cmd) if debug: print(cmd) print('Info: %s' % '\n'.join(err_lines)) path2uuid(output_tmp, d=True, rename=False) except Exception: raise else: color.print_ok('cut the video %s to %s from %s to %s' % (fn, output, start_time, end_time)) finally: path2uuid(fn_tmp, d=True)
def check_module(module_name,install_name=''): try: import_module(module_name) except ImportError: print(module_name,'Not Exists') pip_name='' if ispython3(): pip_name='pip3' elif ispython2(): pip_name='pip' print('Now, try to install through {}'.format(pip_name)) if install_name in ('',None): install_name=module_name lines=exec_cmd('{0} install {1}'.format(pip_name,install_name)) print(''.join(lines))
def cli(): arguments = docopt(__doc__, version=minghu6.__version__) if arguments['--path'] is None: start_path = os.curdir else: start_path = arguments['--path'] for fn in find(arguments['<pattern>'], start_path, regex_match=arguments['--regex']): if arguments['--exec'] is not None: if os.path.isfile(fn): pprint(fn) # print(arguments['--exec']) exec_cmd_completely = arguments['--exec'] % fn if arguments['--debug']: pprint(exec_cmd_completely) info, err = cmd.exec_cmd(exec_cmd_completely) print('\n'.join(info), '\n'.join(err)) else: pprint(fn)
def tesseract(path, limit_config=None, args=None, session: requests = None): if not has_proper_tesseract(): raise DoNotHaveProperVersion imgObj, image_path = get_image(path, session=session) if args is None: cmd_str = 'tesseract -psm 8 {0} stdout '.format(image_path) else: cmd_str = ' '.join(['tesseract', args, image_path, 'stdout']) print(cmd_str) if limit_config is not None: # like digits cmd_str += ' {0}'.format(limit_config) info_lines, err_lines = exec_cmd(cmd_str, shell=True) try: captchaResponse = info_lines[0].strip() except IndexError: raise Exception(''.join(err_lines)) else: return captchaResponse
def test_captcha(): cmd = '{0} -m minghu6.tools.captcha --help'.format(pypath) info_lines, err_lines = exec_cmd(cmd) assert getitem(err_lines, 0, 'failed') == '' assert info_lines
def test_text_editor(): cmd = '{0} -m minghu6.tools.text_editor --help'.format(pypath) info_lines, err_lines = exec_cmd(cmd) assert getitem(err_lines, 0, 'failed') == '' assert info_lines
def test_youtube(): cmd = '{0} -m minghu6.tools.youtube --help'.format(pypath) info_lines, err_lines = exec_cmd(cmd) assert getitem(err_lines, 0, 'failed') == '' assert info_lines
def test_ffmpeg_fix(): cmd = '{0} -m minghu6.tools.ffmpeg_fix --help'.format(pypath) info_lines, err_lines = exec_cmd(cmd) assert getitem(err_lines, 0, 'failed') == '' assert info_lines
def load_video_info_json(fn): cmd = 'ffprobe -v quiet -print_format json -show_format -show_streams "%s" ' % fn info_lines, _ = exec_cmd(cmd) s = '\n'.join(info_lines) json_obj = json.loads(s) return json_obj
def merge(pattern_list, output, type, **other_kwargs): isprefix = other_kwargs.get('isprefix', False) if not assert_output_has_ext(output): color.print_err('Failed.') return base_dir = os.curdir merge_file_list = [] merge_file_list2 = [] if type in ('vedio', 'audio', 'gif'): for fn in os.listdir(base_dir): if os.path.isdir(fn): continue if fn == '.path2uuid.sqlite3': continue for pattern in pattern_list: if isprefix: if fn.lower().startswith(pattern.lower()): merge_file_list.append(fn) else: if fnmatch.fnmatch(fn, pattern): merge_file_list.append(fn) else: # 'va', 'vs merge_file_list = pattern_list # common_prefix_pattern = r'^(\w)+\+$' if isprefix and len(pattern_list) == 1: def key(fn): base = os.path.splitext(os.path.basename(fn))[0] v = LooseVersion(base.split(pattern_list[0])[1]) return v elif type in ('va', 'vs'): key = lambda x: 0 else: key = lambda fn: fn merge_file_list = sorted(merge_file_list, key=key) color.print_info('The following file will be merged in order') for i, file_to_merge in enumerate(merge_file_list): color.print_info('%3d. %s' % (i, file_to_merge)) if len(merge_file_list) <= 1: color.print_info('Do nothing.') return args = input('press enter to continue, q to quit') if args in ('q', 'Q'): return merge_file_tmp_list = list(map(lambda x: path2uuid(x, quiet=True), merge_file_list)) merge_file_tmp_list2 = [] if type == 'video': # check if the video can be merge FileInfo = namedtuple('FileInfo', ['width', 'height', 'fps']) merge_file_info_list = [] for fn in merge_file_tmp_list: json_obj = load_video_info_json(fn) video_site, audio_site = get_video_audio_info_site_injson(json_obj) codec_name = json_obj['streams'][video_site]['codec_name'] width = int(json_obj['streams'][video_site]['width']) height = int(json_obj['streams'][video_site]['height']) fps = round(load_fps_from_json(json_obj), 3) merge_file_info_list.append(FileInfo(width, height, fps)) if not each_same(merge_file_info_list, key=lambda x: (x.width, x.height, x.fps)): color.print_err('width, height, fps should be same of all video') min_width = sorted(merge_file_info_list, key=lambda x: x.width)[0].width min_height = sorted(merge_file_info_list, key=lambda x: x.height)[0].height min_resolution = '%dx%d' % (min_width, min_height) min_fps = sorted(merge_file_info_list, key=lambda x: x.fps)[0].fps color.print_warn('all_to_resolution: %s' % min_resolution) color.print_warn('all_to_fps: %s' % min_fps) if askyesno('convert to fix?'): merge_file_tmp_list2 = list(map(lambda x: add_postfix(x, 'tmp'), merge_file_tmp_list)) def tmp(fn_tuple): convert(*fn_tuple, size=min_resolution, fps=min_fps) list(map(lambda x: tmp(x), zip(merge_file_tmp_list, merge_file_tmp_list2))) else: return elif type == 'audio': pass elif type == 'va': pass elif type == 'gif': pass output_tmp = path2uuid(output, rename=False, quiet=True) if len(merge_file_tmp_list2) == 0: input_file_list = merge_file_tmp_list else: input_file_list = merge_file_tmp_list2 # only for merge video try: fw = open('.mylist', 'w') for fn in input_file_list: fw.write("file '%s' \n" % fn) fw.close() if type in ('video', 'audio'): merge_cmd = 'ffmpeg -f concat -i %s -c copy %s' % ('.mylist', output_tmp) elif type == 'va': merge_cmd = 'ffmpeg -i %s -i %s -vcodec copy -acodec copy %s ' \ % (input_file_list[0], input_file_list[1], output_tmp) elif type == 'vs': with open(input_file_list[1]) as f_subtitle: encoding = guess_charset(f_subtitle)['encoding'] if encoding.lower() not in ('utf-8', 'ascii'): info, err = exec_cmd('%s -m minghu6.tools.text convert %s utf-8' % (sys.executable, input_file_list[1])) if len(err) > 1 or err[0] != '': # exec failed color.print_err('error codec of the subtitle %s (need utf-8)') merge_cmd = 'ffmpeg -i %s -vf subtitles=%s %s' \ % (input_file_list[0], input_file_list[1], output_tmp) elif type == 'gif': framerate = other_kwargs['framerate'] merge_cmd = 'ffmpeg -f image2 -framerate %d -i %s %s' \ % (int(framerate), '.mylist', output_tmp) for line in CommandRunner.run(merge_cmd): print(line) path2uuid(output_tmp, d=True) except Exception: raise else: color.print_ok('Done.') finally: try: os.remove('.mylist') except: pass for fn in input_file_list: path2uuid(fn, d=True)