Browse Source

add skip & overwrite

pull/16/head
Nzix 6 years ago
parent
commit
244c73e69f
  1. 5
      README.md
  2. 35
      app.py
  3. 14
      ncmdump.py

5
README.md

@ -9,7 +9,6 @@
## Fork ## Fork
- [JamieDummy/NCM_dump](https://github.com/JamieDummy/NCM_dump): 增加 GUI - [JamieDummy/NCM_dump](https://github.com/JamieDummy/NCM_dump): 增加 GUI
- [mnilzg/ncmdump](https://github.com/mnilzg/ncmdump): 使用 numpy 大幅提高性能 - [mnilzg/ncmdump](https://github.com/mnilzg/ncmdump): 使用 numpy 大幅提高性能
@ -34,7 +33,7 @@ $ python ncmdump.py
### 更多选项 ### 更多选项
``` ```
$ python app.py -h $ 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: positional arguments:
input ncm file or folder path input ncm file or folder path
@ -44,6 +43,8 @@ optional arguments:
-f format customize naming format -f format customize naming format
-o output customize saving folder -o output customize saving folder
-d delete source after conversion -d delete source after conversion
-c overwrite file with the same name
-s skip conversion if file exist
``` ```
> 自定义命名参数: %artist%, %title%, %album% > 自定义命名参数: %artist%, %title%, %album%

35
app.py

@ -24,9 +24,18 @@ parser.add_argument(
help = 'customize saving folder' help = 'customize saving folder'
) )
parser.add_argument( parser.add_argument(
'-d', dest = 'delete', action='store_true', '-d', dest = 'delete', action = 'store_true',
help = 'delete source after conversion' 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() args = parser.parse_args()
def validate_name(file_name): def validate_name(file_name):
@ -63,7 +72,7 @@ def name_format(path, meta):
name += '.' + meta['format'] name += '.' + meta['format']
folder = args.output if args.output else os.path.dirname(path) folder = args.output if args.output else os.path.dirname(path)
save = os.path.join(folder, name) save = os.path.join(folder, name)
save = validate_collision(save) if not (args.cover or args.skip): save = validate_collision(save)
return save return save
if args.output: if args.output:
@ -76,26 +85,26 @@ if args.output:
exit() exit()
files = [] files = []
for item in args.input: for path in args.input:
item = os.path.abspath(item) path = os.path.abspath(path)
if not os.path.exists(item): if not os.path.exists(path):
continue continue
if os.path.isdir(item): if os.path.isdir(path):
files += [os.path.join(item, _file) for _file in os.listdir(item) if os.path.splitext(_file)[-1] == '.ncm'] files += [os.path.join(path, name) for name in os.listdir(path) if os.path.splitext(name)[-1] == '.ncm']
else: else:
files += [item] files += [path]
if sys.version[0] == '2': 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: if not files:
print('empty input') print('empty input')
exit() exit()
for _file in files: for path in files:
try: try:
_save = ncmdump.dump(_file, name_format) save = ncmdump.dump(path, name_format, args.skip)
print(os.path.split(_save)[-1]) if save: print(os.path.split(save)[-1])
if args.delete: os.remove(_file) if args.delete: os.remove(path)
except KeyboardInterrupt: except KeyboardInterrupt:
exit() exit()

14
ncmdump.py

@ -13,7 +13,7 @@ import os
from Crypto.Cipher import AES from Crypto.Cipher import AES
from mutagen import mp3, flac, id3 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 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') core_key = binascii.a2b_hex('687A4852416D736F356B496E62617857')
@ -71,6 +71,7 @@ def dump(input_path, output_path = None):
# media data # media data
output_path = output_path(input_path, meta_data) output_path = output_path(input_path, meta_data)
if skip and os.path.exists(output_path): return
m = open(output_path,'wb') m = open(output_path,'wb')
data = bytearray(f.read()) data = bytearray(f.read())
@ -120,17 +121,18 @@ if __name__ == '__main__':
if len(sys.argv) > 1: if len(sys.argv) > 1:
files = sys.argv[1:] files = sys.argv[1:]
else: 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': 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: if not files:
print('please input file path!') print('please input file path!')
for file_name in files: for path in files:
try: try:
dump(file_name) dump(path)
print(os.path.split(file_name)[-1]) print(os.path.split(path)[-1])
except Exception as e: except Exception as e:
print(e) print(e)
pass pass
Loading…
Cancel
Save