Crate ncm_parser

source ·
Expand description

A single file ncm parser. Here, the ncm is an encrypted music file format which is widely used on NeteaseCloudMuic.

For more details see https://www.cnblogs.com/cyx-b/p/13443003.html

一个单文件实现的 .ncm 解析器。

Examples

use ncm_parser::NCMMetadata;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;

fn main() {
    // Open .ncm file
    let ncm_file_name = Path::new("xxx.ncm");
    let mut ncm_file = std::fs::read(ncm_file_name).unwrap();

    // Parse ncm file with `from_iter`
    let mut ncm_file_from_iter =
        ncm_parser::from_iter(ncm_file.into_iter()).unwrap();

    // Directly parse ncm file with `from_reader`
    let mut ncm_file_from_reader =
        ncm_parser::from_reader(File::open(ncm_file_name).unwrap()).unwrap();

    // Both functions get same result.
    assert_eq!(
        ncm_file_from_iter.get_image().unwrap(),
        ncm_file_from_reader.get_image().unwrap()
    );
    assert_eq!(
        ncm_file_from_iter.get_metadata().unwrap(),
        ncm_file_from_reader.get_metadata().unwrap()
    );
    assert_eq!(
        ncm_file_from_iter.get_music().unwrap(),
        ncm_file_from_reader.get_music().unwrap()
    );

    // Parse metadata
    let ncm_meta = ncm_file_from_iter.get_parsed_metadata().unwrap();

    let image = ncm_file_from_iter.get_image_unchecked();
    let metadata = ncm_file_from_iter.get_metadata_unchecked();
    let music = ncm_file_from_iter.get_music_unchecked();

    // Save music
    let music_name = ncm_file_name.with_extension(&ncm_meta.format);
    std::fs::write(music_name, &music).unwrap();

    // Get image format
    let image_ext = ncm_meta.album_pic_url.rsplit_once('.').unwrap().1;

    // Save cover image
    let image_name = ncm_file_name.with_extension(image_ext);
    std::fs::write(image_name, &image).unwrap();

    // Save metadata
    let meta_name = ncm_file_name.with_extension("json");
    std::fs::write(meta_name, &metadata).unwrap();
}

Structs

  • A struct contains all the data parsed from the ncm file.
  • NCMMetadataserde_json
    A struct contains all the JSON values in metadata.

Enums

  • An error type represents all the possible errors.

Functions

  • Parse the ncm file with iterator. Recommended if you have an ncm file stored in Vec or slice.
  • Parse the ncm file with reader. Recommended if you have an ncm file opened from File.