Vinlic / WebVideoCreator

🌈 A framework for rendering web animations into videos. It's implemented based on Node.js + Puppeteer + Chrome + FFmpeg, utilizing the latest browser APIs.
Apache License 2.0
94 stars 26 forks source link

util.js "reject is not defined" #4

Closed ivangusev closed 7 months ago

ivangusev commented 7 months ago

If we pass non-mp3 (jpeg for example) we get "ReferenceError: reject is not defined" in checkRemoteResource() Also please handle the case of rejected promise in Audio.js load(), otherwise the whole application is crashed without catching the error. Ideally i should get this error in:

video.on("error", (err) => {
    console.error(err);
});
Vinlic commented 7 months ago

Oh! Yes, this seems to be a legacy code error that has been corrected and will be fixed on 0.0.21.

ivangusev commented 7 months ago

@Vinlic, thanks for the fix, and what about catching the error?

Vinlic commented 7 months ago

Now you can listen for errors through the "error" event. :D

ivangusev commented 7 months ago

@Vinlic , unfortunately i can't confirm. If you just replace reject() with "throw" it's not enough, because in Audio.js load() you return a promise. So you need to handle when it's rejected.

Vinlic commented 7 months ago

@ivangusev This rejected promise will eventually be captured by the global asynchronous method in the Synthesizer.start() function and passed through _emitError(err) thrown to your listening callback function. Synthesizer.js

...
start() {
    ...
    (async () => {
        ...
        await this.#waitForAudiosLoaded();  // Waiting for all audio to load here.
        ...
    })()
    .catch(err => this._emitError(err));  // Will notify you of the error here
}
...
ivangusev commented 7 months ago

@Vinlic , i think it's not the only place where it's called. I'm adding audio like this:

const video = wvc.createSingleVideo({
    content: content,
    pageWaitForOptions: {
      waitUntil:'networkidle0'
    }
    ...
});
video.on("error", (err) => {
  console.error(err);
});
video.addAudio({
      url: req.body.mp3,
));
await video.startAndWait();

And error is not captured. Synthesizer.start() is not even reached to the time of error.

Vinlic commented 7 months ago

@ivangusev I understand now. In the addAudio function, I added audio.load(), which triggers preloading when adding audio. Now I may consider removing it or changing it to audio.load().catch(err => this._emitError(err)) (which will trigger two error events)

ivangusev commented 7 months ago

@Vinlic , yes, that's what i meant: audio.load().catch(err => this._emitError(err))

Vinlic commented 7 months ago

@ivangusev I have made changes and this update will be included in the next release version. :D