ncmdump

repository·master·Indexed 41 days ago

https://github.com/iqiziqi/ncmdump.rs

A Rust tool and library for decrypting and converting .ncm and .qmc music files into raw audio formats such as .flac and .mp3. It provides a command-line interface for batch processing and a library for programmatic access to audio data, metadata (via NcmInfo), and album art.

Tokens
4.3K
Snippets
23
Records
26
Agent score
49%

What's inside ncmdump

  1. Use the ncmdump CLI to dump encrypted music files

    master

    The ncmdump binary is a command-line tool used to decrypt and dump encrypted music files (such as .ncm or .qmc formats) into standard audio formats like .mp3 or .flac. It supports multi-threaded processing to handle multiple files efficiently.

    To use the tool, you typically provide input files and specify an output directory. The tool automatically detects the underlying audio format (MP3 or FLAC) and injects the original metadata and album art into the decrypted file.

    # Example conceptual usage (actual CLI flags are defined in the Command module)
    ncmdump <input_files> --output <output_dir>
  2. Use the ncmdump library in Rust

    master

    To extract music data from an .ncm file using the library, use Ncmdump::from_reader to parse the file and get_data() to retrieve the raw music bytes.

    use std::fs::File;
    use std::path::Path;
    
    use anyhow::Result;
    use ncmdump::Ncmdump;
    
    fn main() -> Result<()> {
        use std::io::Write;
        let file = File::open("res/test.ncm")?;
        let mut ncm = Ncmdump::from_reader(file)?;
        let music = ncm.get_data()?;
        let mut target = File::options()
            .create(true)
            .write(true)
            .open("res/test.flac")?;
        target.write_all(&music)?;
        Ok(())
    }
  3. Use ncmdump in your Rust code

    master

    To extract music data from an .ncm file, use Ncmdump::from_reader to parse the file and get_data() to retrieve the raw music bytes.

    use std::fs::File;
    use std::path::Path;
    
    use anyhow::Result;
    use ncmdump::Ncmdump;
    
    fn main() -> Result<()> {
        use std::io::Write;
        let file = File::open("res/test.ncm")?;
        let mut ncm = Ncmdump::from_reader(file)?;
        let music = ncm.get_data()?;
        let mut target = File::options()
            .create(true)
            .write(true)
            .open("res/test.flac")?;
        target.write_all(&music)?;
        Ok(())
    }
  4. Reference ncmdump CLI options

    master

    The following options are available when using the ncmdump binary:

    Usage: ncmdump [OPTIONS] [FILES]...
    
    Arguments:
      [FILES]...  Specified the files to convert
    
    Options:
      -o, --output <OUTPUT>  Specified the output directory. Default it's the same directory with input file
      -v, --verbose          Verbosely list files processing
      -h, --help             Print help
      -V, --version          Print version
  5. Decrypt NCM files using Ncmdump

    master

    To decrypt an .ncm file, use the Ncmdump::from_reader method to create an Ncmdump instance from a file reader. You can then retrieve the decrypted audio data using the get_data() method. The resulting bytes can be written to a new file (e.g., a .flac file).

    use std::fs::File;
    use std::io::{Error, Write};
    use std::path::Path;
    
    use ncmdump::Ncmdump;
    
    fn main() -> Result<(), Error> {
        let file = File::open("res/test.ncm")?;
        let mut ncm = Ncmdump::from_reader(file).expect("Can't create dump");
        let music = ncm.get_data().expect("Can't get data");
        let mut target = File::options()
            .create(true)
            .write(true)
            .open("res/test.flac")?;
        target.write_all(&music)?;
        Ok(())
    }
  6. Use the ncmdump CLI

    master

    The ncmdump binary is used to convert .ncm files to raw music data (e.g., .flac).

    Basic Command: ncmdump [FILES]...

    Options:

    • -o, --output <OUTPUT>: Specify the output directory. If not provided, it defaults to the same directory as the input file.
    • -v, --verbose: Verbosely list files being processed.
    • -h, --help: Print help information.
    • -V, --version: Print version information.
    ncmdump [FILES]...
  7. Extract decrypted music data with get_data()

    master

    The get_data() method extracts and decrypts the actual audio stream from the NCM file. It returns the raw audio bytes (e.g., FLAC or MP3 data) as a Vec<u8>}.

    # use std::fs::File;
    # use std::io::Write;
    # use ncmdump::Ncmdump;
    # use anyhow::Result;
    
    fn main() -> Result<()> {
        let file = File::open("res/test.ncm")?;
        let mut ncm = Ncmdump::from_reader(file)?;
        let music = ncm.get_data()?;
    
        let mut target = File::options()
            .create(true)
            .write(true)
            .open("res/test.flac")?;
        target.write_all(&music)?;
        Ok(())
    }
  8. Use QmcDump to extract music data from QMC files

    master

    The QmcDump struct provides a wrapper around a reader to decrypt and extract music data from QMC-formatted files (e.g., .qmcflac). It implements the std::io::Read and std::io::Seek traits, meaning it can be used like any other standard Rust reader. To extract the entire decrypted payload into a byte vector, use the get_data() method.

    use std::fs::File;
    use ncmdump::QmcDump;
    
    fn main() -> anyhow::Result<()> {
        // 1. Open the encrypted QMC file
        let file = File::open("res/test.qmcflac")?;
        
        // 2. Wrap it in QmcDump
        let mut qmc = QmcDump::from_reader(file)?;
        
        // 3. Extract the decrypted music data
        let music = qmc.get_data()?;
    
        // 4. Save to a new file (e.g., .flac)
        let mut target = File::create("res/test.flac")?;
        target.write_all(&music)?;
        Ok(())
    }