diff --git a/README.md b/README.md index 0430877..edfdb2a 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ ## Fork - [JamieDummy/NCM_dump](https://github.com/JamieDummy/NCM_dump): 增加 GUI - - [mnilzg/ncmdump](https://github.com/mnilzg/ncmdump): 使用 numpy 大幅提高性能 @@ -34,7 +33,7 @@ $ python ncmdump.py ### 更多选项 ``` $ python app.py -h -usage: ncmdump [-h] [-f format] [-o output] [-d] [input [input ...]] +usage: ncmdump [-h] [-f format] [-o output] [-d] [-c | -s] [input [input ...]] positional arguments: input ncm file or folder path @@ -44,6 +43,8 @@ optional arguments: -f format customize naming format -o output customize saving folder -d delete source after conversion + -c overwrite file with the same name + -s skip conversion if file exist ``` > 自定义命名参数: %artist%, %title%, %album% diff --git a/app.py b/app.py index 905b49e..2dfd483 100644 --- a/app.py +++ b/app.py @@ -24,9 +24,18 @@ parser.add_argument( help = 'customize saving folder' ) parser.add_argument( - '-d', dest = 'delete', action='store_true', + '-d', dest = 'delete', action = 'store_true', help = 'delete source after conversion' ) +group = parser.add_mutually_exclusive_group() +group.add_argument( + '-c', dest = 'cover', action = 'store_true', + help = 'overwrite file with the same name' +) +group.add_argument( + '-s', dest = 'skip', action = 'store_true', + help = 'skip conversion if file exist' +) args = parser.parse_args() def validate_name(file_name): @@ -63,7 +72,7 @@ def name_format(path, meta): name += '.' + meta['format'] folder = args.output if args.output else os.path.dirname(path) save = os.path.join(folder, name) - save = validate_collision(save) + if not (args.cover or args.skip): save = validate_collision(save) return save if args.output: @@ -76,26 +85,26 @@ if args.output: exit() files = [] -for item in args.input: - item = os.path.abspath(item) - if not os.path.exists(item): +for path in args.input: + path = os.path.abspath(path) + if not os.path.exists(path): continue - if os.path.isdir(item): - files += [os.path.join(item, _file) for _file in os.listdir(item) if os.path.splitext(_file)[-1] == '.ncm'] + if os.path.isdir(path): + files += [os.path.join(path, name) for name in os.listdir(path) if os.path.splitext(name)[-1] == '.ncm'] else: - files += [item] + files += [path] if sys.version[0] == '2': - files = [file_name.decode(sys.stdin.encoding) for file_name in files] + files = [path.decode(sys.stdin.encoding) for path in files] if not files: print('empty input') exit() -for _file in files: +for path in files: try: - _save = ncmdump.dump(_file, name_format) - print(os.path.split(_save)[-1]) - if args.delete: os.remove(_file) + save = ncmdump.dump(path, name_format, args.skip) + if save: print(os.path.split(save)[-1]) + if args.delete: os.remove(path) except KeyboardInterrupt: exit() \ No newline at end of file diff --git a/ncmdump.py b/ncmdump.py index 2cd9c5d..0b3beb2 100644 --- a/ncmdump.py +++ b/ncmdump.py @@ -13,7 +13,7 @@ import os from Crypto.Cipher import AES from mutagen import mp3, flac, id3 -def dump(input_path, output_path = None): +def dump(input_path, output_path = None, skip = True): output_path = (lambda path, meta : os.path.splitext(path)[0] + '.' + meta['format']) if not output_path else output_path core_key = binascii.a2b_hex('687A4852416D736F356B496E62617857') @@ -71,6 +71,7 @@ def dump(input_path, output_path = None): # media data output_path = output_path(input_path, meta_data) + if skip and os.path.exists(output_path): return m = open(output_path,'wb') data = bytearray(f.read()) @@ -120,17 +121,18 @@ if __name__ == '__main__': if len(sys.argv) > 1: files = sys.argv[1:] else: - files = [file_name for file_name in os.listdir('.') if os.path.splitext(file_name)[-1] == '.ncm'] + files = [name for name in os.listdir('.') if os.path.splitext(name)[-1] == '.ncm'] + if sys.version[0] == '2': - files = [file_name.decode(sys.stdin.encoding) for file_name in files] + files = [path.decode(sys.stdin.encoding) for path in files] if not files: print('please input file path!') - for file_name in files: + for path in files: try: - dump(file_name) - print(os.path.split(file_name)[-1]) + dump(path) + print(os.path.split(path)[-1]) except Exception as e: print(e) pass \ No newline at end of file