ncm2mp3

非原创代码,源代码主要来自于:https://github.com/lianglixin/ncmdump

调整了一下参数,

使用方法:

ncm2mp3 -i <ncm原始文件目录> -o <mp3输出目录>

安装依赖:

pip install pycrypto

源代码:

# coding=utf-8

# Edit:obaby
# Source: Liang Lixin
# Thanks for nondanee
import binascii
import struct
import base64
import json
import os, sys
from Crypto.Cipher import AES
import getopt


def dump(file_path, out_path):
    core_key = binascii.a2b_hex("687A4852416D736F356B496E62617857")
    meta_key = binascii.a2b_hex("2331346C6A6B5F215C5D2630553C2728")
    unpad = lambda s: s[0:-(s[-1] if type(s[-1]) == int else ord(s[-1]))]
    f = open(file_path, 'rb')
    header = f.read(8)
    assert binascii.b2a_hex(header) == b'4354454e4644414d'
    f.seek(2, 1)
    key_length = f.read(4)
    key_length = struct.unpack('<I', bytes(key_length))[0]
    key_data = f.read(key_length)
    key_data_array = bytearray(key_data)
    for i in range(0, len(key_data_array)): key_data_array[i] ^= 0x64
    key_data = bytes(key_data_array)
    cryptor = AES.new(core_key, AES.MODE_ECB)
    key_data = unpad(cryptor.decrypt(key_data))[17:]
    key_length = len(key_data)
    key_data = bytearray(key_data)
    key_box = bytearray(range(256))
    c = 0
    last_byte = 0
    key_offset = 0
    for i in range(256):
        swap = key_box[i]
        c = (swap + last_byte + key_data[key_offset]) & 0xff
        key_offset += 1
        if key_offset >= key_length: key_offset = 0
        key_box[i] = key_box[c]
        key_box[c] = swap
        last_byte = c
    meta_length = f.read(4)
    meta_length = struct.unpack('<I', bytes(meta_length))[0]
    meta_data = f.read(meta_length)
    meta_data_array = bytearray(meta_data)
    for i in range(0, len(meta_data_array)): meta_data_array[i] ^= 0x63
    meta_data = bytes(meta_data_array)
    meta_data = base64.b64decode(meta_data[22:])
    cryptor = AES.new(meta_key, AES.MODE_ECB)
    meta_data = unpad(cryptor.decrypt(meta_data)).decode('utf-8')[6:]
    meta_data = json.loads(meta_data)
    crc32 = f.read(4)
    crc32 = struct.unpack('<I', bytes(crc32))[0]
    f.seek(5, 1)
    image_size = f.read(4)
    image_size = struct.unpack('<I', bytes(image_size))[0]
    image_data = f.read(image_size)
    file_name = meta_data['musicName'] + '.' + meta_data['format']
    # m = open(os.path.join(os.path.split(file_path)[0],file_name),'wb')
    m = open(os.path.join(out_path, file_name), 'wb')
    chunk = bytearray()
    while True:
        chunk = bytearray(f.read(0x8000))
        chunk_length = len(chunk)
        if not chunk:
            break
        for i in range(1, chunk_length + 1):
            j = i & 0xff;
            chunk[i - 1] ^= key_box[(key_box[j] + key_box[(key_box[j] + j) & 0xff]) & 0xff]
        m.write(chunk)
    m.close()
    f.close()


def print_usage():
    print('*' * 100)
    print ('ncm2mp3')
    print('Usage: ncm2mp3 -i <input path(ncm file path)> - o <out path(mp3 file path)>')
    print('------------------------------')
    print('Edit by: obaby')
    print('Souce code: Liang Lixin')
    print('Thanks: nondanee')
    print('http://www.obaby.org.cn')
    print('https://github.com/lianglixin/ncmdump')
    print('https://github.com/nondanee/vsc-netease-music')
    print('*' * 100)


def main(argv):
    inputfile = ''
    outputfile = ''
    try:
        opts, args = getopt.getopt(argv, "hi:o:", ["ipath=", "opath="])
    except getopt.GetoptError:
        print_usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print_usage()
            sys.exit()
        elif opt in ("-i", "--ipath"):
            inputfile = arg
        elif opt in ("-o", "--opath"):
            outputfile = arg

    if inputfile == '' or outputfile == '':
        print_usage()
        sys.exit(2)

    print ('*' * 100)
    print ('[*] start convert ncm to mp3.')
    dst_path = inputfile
    out_path = outputfile
    ext_name = 'ncm'
    for file in os.listdir(dst_path):
        if file.endswith(ext_name):
            # file_path = dst_path + file
            file_path = os.path.join(dst_path, file)
            print ('[*] Convert:' + file_path)
            try:
                # print (file_path)
                # print (out_path)
                dump(file_path, out_path=out_path)
            except Exception as e:
                print e
    print ('[*] all done, have a nice time')
    print ('*' * 100)


if __name__ == '__main__':
    main(sys.argv[1:])

py编译的exe下载(如果没有python环境或者不想安装python可以下载下面编译好的exe,解压后运行ncm2mp3.exe即可):

链接:https://pan.baidu.com/s/1Y8gGcLrkwM90QufDbz8EcQ
提取码:jt61
复制这段内容后打开百度网盘手机App,操作更方便哦

文件哈希:

文件名: F:\PyCharmProjects\ncm2mp3\dist\ncm2mp3.rar
文件大小: 2428281 字节 (2.32 MB)
修改日期: 2020-02-01 20:12
MD5: 2b143d6f29980ea639487c400ddb981f
SHA1: 39e68670262c62a60f3930415755884c232a481c
SHA256: c13d90b06f8207e32bd49e91a80d79f8cc27fbc7a2706a8c141f899d1653ebb6
CRC32: 4a442cbf
☆版权☆

* 网站名称:obaby@mars
* 网址:https://oba.by/
* 个性:https://oba.by/
* 本文标题: 《ncm2mp3》
* 本文链接:https://zhongxiaojie.com/2020/02/6904
* 短链接:https://oba.by/?p=6904
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注