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

How to set Video Duration Same as Audio #15

Closed ivangusev closed 7 months ago

ivangusev commented 7 months ago

I want to stop video recording when mp3 audio is finished (+ some gap). May be i can use video.addAudio() somehow? Another idea is to use pagePrepareFn and "onloadedmetadata" event. But how to update "duration" setting at this moment?

Vinlic commented 7 months ago

@ivangusev Currently WVC supports the following methods to set audio:

  1. wvc.createSingleVideo({ ... }).addAudio({ ... });
  2. Call captureCtx.addAudio({ ... }) on the page
  3. Insert the <audio> element into the DOM tree

If you want to terminate the video capture when the audio playback ends, this may not be easy to control. The most direct method is to set the video duration to be the same as the audio duration, which requires predicting the audio duration.

Another solution is to use captureCtx.currentTime on the page to determine whether the audio duration is currently reached, and when the duration is reached, set captureCtx.stopFlag to true to automatically trigger termination.

ivangusev commented 7 months ago

@Vinlic , "The most direct method is to set the video duration to be the same as the audio duration", yes, that's what i'm talking about. How to do it in pagePrepareFn ? Also if you preload audio in wvc.createSingleVideo({ ... }).addAudio({ ... });, then you probably can return audio duration.

Vinlic commented 7 months ago

@ivangusev You can update to 0.0.32, and then modify this.duration in the pagePrepareFn function to achieve the goal:

const video = wvc.createSingleVideo({
    ...
    pagePrepareFn() {
        this.duration = 10000;
    }
});
ivangusev commented 7 months ago

@Vinlic, i can't confirm. I updated to 0.0.32. Now i got error: Error: Cannot set properties of undefined (setting 'duration')

Also, is it possible to access this.audios this way?

Vinlic commented 7 months ago

@ivangusev Have you confirmed that the "this" pointer is pointing to the correct object? Yes, this.audios can also be accessed.

ivangusev commented 7 months ago

@Vinlic, "this" is undefined inside pagePrepareFn

Vinlic commented 7 months ago

@ivangusev Can you provide the parameters of createSingleVideo? If you use arrow functions you lose the "this" pointer, which should normally point to the SingleVideo instance.

ivangusev commented 7 months ago

@Vinlic , you right, i use arrow function syntax for pagePrepareFn :) So all clear and i confirm it works well. Thanks a lot for your work and support!