melchor629 / node-flac-bindings

Nodejs bindings to libFLAC
ISC License
17 stars 1 forks source link

Decoder initialization failed: Error_opening_file #37

Closed zhushenwudi closed 2 years ago

zhushenwudi commented 2 years ago

Hi. I'm working on a desktop application using Electron. Flac to WAV format is used. I had a tricky problem. It worked on either an M1 chip or an Intel chip on a MAC, but when I used Windows, I had a problem.

QQ截图20220620130344

When I first saw that the file could not be opened, after checking at the code level, I replaced the delimiter of the FLAC file path with the symbol of different platforms through path.sep. But problems remain

QQ截图20220620130329

My Node version is V16.13.0. after I use "yarn install". Copy flac-bindings.node to /Release after going to node_modules/flac-bindings/build/Release

QQ截图20220620130550

QQ截图20220620130602

At present, this problem is bothering me. Please be sure to reply when you have a solution, 3Q

zhushenwudi commented 2 years ago

By the way, both MAC and Windows convert the same file directory structure

zhushenwudi commented 2 years ago
const decoder = new FileDecoder({
          file: musicPath,
      })
      decoder.once('data', (chunk) => {
          const encoder = new wav.Writer({
              channels: decoder.getChannels(),
              bitDepth: decoder.getBitsPerSample(),
              sampleRate: decoder.getSampleRate(),
          })

          encoder.write(chunk)

          decoder
              .pipe(encoder)
              .pipe(fs.createWriteStream(musicPath.replace(".flac", ".wav")))
              .on('error', (e) => {
                  return reject(e.message)
              })
      })

      decoder.on('end', () => {
          return resolve(path.parse(musicPath).name)
      })
zhushenwudi commented 2 years ago

I think I know what the problem is. Files can be accessed in the main process through Node, but not in the UI process

melchor629 commented 2 years ago

Hi, sorry for late response.

The FileDecoder and FileEncoder classes uses C APIs to read and write files. Maybe in Windows, those APIs are not available (or they do not work as expected) on the UI process of Electron.

A bit surprised to see that you managed to get the UI process to load the package. Some time ago I wanted to use my library in an experiment with Electron and I had to write all code in the node process. Nevertheless, happy to see at least it is working somehow in Electron.

Back to the issue, I would suggest to use const inputStream = fs.createReadStream(musicPath) and pipe it to a StreamDecoder. If you can read files using node APIs on Windows, then this trick may work for you:

      // note: write this from memory, maybe there is something wrong :/
      const inputStream = fs.createReadStream(musicPath)
      const decoder = new StreamDecoder()
      inputStream.pipe(decoder)
      decoder.once('data', (chunk) => {
          const encoder = new wav.Writer({
              channels: decoder.getChannels(),
              bitDepth: decoder.getBitsPerSample(),
              sampleRate: decoder.getSampleRate(),
          })

          encoder.write(chunk)

          decoder
              .pipe(encoder)
              .pipe(fs.createWriteStream(musicPath.replace(".flac", ".wav")))
              .on('error', (e) => {
                  return reject(e.message)
              })
      })

      decoder.on('end', () => {
          return resolve(path.parse(musicPath).name)
      })

Hope it helps :)

zhushenwudi commented 2 years ago

3Q, i will try it recently, and report my result

zhushenwudi commented 2 years ago

Hello, I tried streamdecoder on Windows and it can be converted. But so far I've found that when I recursively convert a lot of songs. Conversion callbacks do not return 100%. So if you do this recursively it will terminate. In addition FileDecoder after many tests does not exist this problem.

zhushenwudi commented 2 years ago
function flacToWav(musicPath: string, isWindows: boolean) {
    return new Promise(function (resolve, _) {
        console.log(musicPath)
        let decoder: any
        if (isWindows) {
            const inputStream = fs.createReadStream(musicPath)
            decoder = new StreamDecoder({})
            inputStream.pipe(decoder)
        } else {
            decoder = new FileDecoder({
                file: musicPath,
            })
        }

        decoder.once('data', (chunk: any) => {
            const encoder = new wav.Writer({
                channels: decoder.getChannels(),
                bitDepth: decoder.getBitsPerSample(),
                sampleRate: decoder.getSampleRate(),
            })

            encoder.write(chunk)

            decoder
                .pipe(encoder)
                .pipe(fs.createWriteStream(musicPath.replace(".flac", ".wav")))
                .on('error', (e: any) => {
                    console.log(e)
                    return Promise.resolve({musicPath: musicPath, reason: "convert fail"})
                })
        })

        decoder.on('end', () => {
            const name = path.parse(musicPath).name
            console.log("convert over: " + name)
            return resolve({musicPath: musicPath, reason: undefined})
        })
    })
}
melchor629 commented 2 years ago

I'm not sure how it can get stuck with a lot of conversions... Are they run one by one, or all at the same time?

Also notice that in your code, when it fails in the on error, you don't call to the resolve method. You will know it failed because of the console.log but the code will get stuck because the Promise of flacToWav is not resolved (nor rejected).

zhushenwudi commented 2 years ago

yes, they are one by one. I don't see any printout for the error, which means console.log is not executing you can see my code through github, and url is

https://github.com/zhushenwudi/LoveLiveMusicPlayer/blob/dev/src/main/util.ts

If you have any ideas, you can write some pseudo-code for reference

melchor629 commented 2 years ago

Oh no errors then. Okey so. When it gets stuck, it does with any specific flac file? Maybe the issue is the StreamDecoder has something wrong handling that file (or a set of files).

Also, you can try to "debug" a bit the code using the DEBUG=flac:* environment variable. But not sure if you will get anything if the code runs on the Chromium process of Electron. This flag tells another library called debug to print messages to the console. If you manage to find the files that makes get stuck or fail, and run your code in a simple node script with the variable, we may find the issue.

Hope it helps you to find something.

Edit: I see that the conversion code runs on the main process (the node one). The DEBUG trick may work then, on the console should appear a lot of debug text (and a lot I mean A LOT).

zhushenwudi commented 2 years ago

Okay, let me try it, reply late

zhushenwudi commented 2 years ago

Hello, after two days of testing, I found that there is no format conversion problem in debug mode. Hundreds of songs can be successfully converted one by one. On the other hand, there are still no callbacks when not in Debug mode

zhushenwudi commented 2 years ago

end of debug message

2022-06-24T14:10:37.069Z flac:decoder:stream Read 8192 bytes from stored chunks to be decoded 2022-06-24T14:10:37.070Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.070Z flac:decoder:stream Processing data 2022-06-24T14:10:37.070Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.071Z flac:decoder:stream Processing data 2022-06-24T14:10:37.071Z flac:decoder:stream Read 8192 bytes from stored chunks to be decoded 2022-06-24T14:10:37.071Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.072Z flac:decoder:stream Processing data 2022-06-24T14:10:37.072Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.073Z flac:decoder:stream Processing data 2022-06-24T14:10:37.073Z flac:decoder:stream Read 8192 bytes from stored chunks to be decoded 2022-06-24T14:10:37.073Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.073Z flac:decoder:stream Processing data 2022-06-24T14:10:37.074Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.074Z flac:decoder:stream Processing data 2022-06-24T14:10:37.074Z flac:decoder:stream Read 8192 bytes from stored chunks to be decoded 2022-06-24T14:10:37.076Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.077Z flac:decoder:stream Processing data 2022-06-24T14:10:37.078Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.079Z flac:decoder:stream Processing data 2022-06-24T14:10:37.080Z flac:decoder:stream Read 8192 bytes from stored chunks to be decoded 2022-06-24T14:10:37.084Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.085Z flac:decoder:stream Processing data 2022-06-24T14:10:37.085Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.085Z flac:decoder:stream Processing data 2022-06-24T14:10:37.086Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.086Z flac:decoder:stream Processing data 2022-06-24T14:10:37.086Z flac:decoder:stream Read 8192 bytes from stored chunks to be decoded 2022-06-24T14:10:37.087Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.087Z flac:decoder:stream Processing data 2022-06-24T14:10:37.087Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.088Z flac:decoder:stream Processing data 2022-06-24T14:10:37.088Z flac:decoder:stream Read 8192 bytes from stored chunks to be decoded 2022-06-24T14:10:37.089Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.089Z flac:decoder:stream Processing data 2022-06-24T14:10:37.089Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.089Z flac:decoder:stream Processing data 2022-06-24T14:10:37.090Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.090Z flac:decoder:stream Processing data 2022-06-24T14:10:37.090Z flac:decoder:stream Read 8192 bytes from stored chunks to be decoded 2022-06-24T14:10:37.091Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.091Z flac:decoder:stream Received 13186 bytes to process 2022-06-24T14:10:37.091Z flac:decoder:stream Processing data 2022-06-24T14:10:37.092Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.092Z flac:decoder:stream Processing data 2022-06-24T14:10:37.092Z flac:decoder:stream Read 8192 bytes from stored chunks to be decoded 2022-06-24T14:10:37.093Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.093Z flac:decoder:stream Processing final chunks of data 2022-06-24T14:10:37.094Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.095Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.095Z flac:decoder:stream Read 8192 bytes from stored chunks to be decoded 2022-06-24T14:10:37.096Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.096Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.097Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.097Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.098Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.099Z flac:decoder:stream Read 4994 bytes from stored chunks to be decoded 2022-06-24T14:10:37.099Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.100Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.100Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.100Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.101Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.101Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.102Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.102Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.102Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.103Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.103Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.104Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.104Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.104Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.105Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.105Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.106Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.106Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.106Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.107Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.107Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.108Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.108Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.108Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.109Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.109Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.111Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.111Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.111Z flac:decoder:stream Received 4096 samples (16384 bytes) of decoded data 2022-06-24T14:10:37.112Z flac:decoder:stream Received 3156 samples (12624 bytes) of decoded data 2022-06-24T14:10:37.112Z flac:decoder:stream Flushing decoder

melchor629 commented 2 years ago

Hello! Now I have some time to investigate more. I found that in FLAC documentation, the function I use to initialize the FLAC Decoder in FileDecoder, on Windows, it does not support paths with non-ascii characters. An example:

image

The first uses a path with non ascii characters (ñ and ç), the second one does not use any of these. I will investigate if I can workaround this limitation on Windows for the version you have (and if I succeed I'll publish a version for it).

For the other issue with StreamDecoder and no callbacks being called, I'm trying to replicate somehow the issue with the code of your project (adapted for a smaller usage) on Windows. But for now, I always get all files converted fine and without DEBUG enabled.

For now, I will focus on the Unicode path support on Windows which should be better for your use case. I'll report back soon.

zhushenwudi commented 2 years ago

that's fine, thanks.

After I let my code run by my friend, streamdecoder does have an interrupt. But it doesn't matter if filedecoder can be done well

melchor629 commented 2 years ago

H!, good to hear that it works (in other machine tho) :)

From my side, I found that the official FLAC library fixed this issue with Windows and non-ascii characters in paths, so I'm preparing the v2.7.1 release with the updated flac library updated.

zhushenwudi commented 2 years ago

that's very good!! There is another question, please take a look at it~

melchor629 commented 2 years ago

Published 2.7.1! Try to use FileDecoder now on Windows and tell me if it is working now.

I'm looking the other question, and left an answer there. I will update the other one soon...

zhushenwudi commented 2 years ago

Thank you. I will test it in the local environment and give feedback. Another problem will bother you~ ^_^

zhushenwudi commented 2 years ago

It has been tested on the windows platform, and there is no conversion problem. However, there is still an interrupt problem at present. It may be my code problem. I will try another way to write it later

melchor629 commented 2 years ago

Good news at last! Happy to see now it is working (more or less), at least the paths are no an issue on Windows :)

If you think this is solved, feel free to close the issue. But if you prefer to wait until you check the "interrupt problem", for me, it is fine 👍

Thanks for your patience and the help provided for resolving the issue.

zhushenwudi commented 2 years ago

Another question, is there a 32-bit windows node file?

melchor629 commented 2 years ago

Currently I do not provide 32 bit binaries of the compiled package, but I could try to create them. I'm not very expert on the Windows side but I surely will investigate it.

I planned to add support for Apple M1/M2 chips, so Windows 32-bit is something I can look at as well.

Will add an answer soon (hopefully today).

melchor629 commented 2 years ago

So I managed to compile the code for windows 32-bit (not for Apple new chips, but nobody asked for them so...).

I uploaded the precompiled binaries for the 2.7.1 version :)

zhushenwudi commented 2 years ago

i love you boy! I'm crazy for you~

melchor629 commented 2 years ago

I'm supper happy to see all your problems are fixed :D

Thanks for raising the issues, they improved the library!

If there is anything else I can help you with related to the library, don't hesitate to raise a new issue. I'm closing this issue, but feel free to reopen if I'm missing something else.